0

The Question is pretty direct. I have two variables in JS. I want to pass these to a php file first to update a database, and then return back to the original file, a true or false value based on the query. How do i do this?

4
  • I want to do this with pure Javascript. Possible? Commented Jun 8, 2012 at 7:12
  • Ajax is pure Javascript, although wrappers like the jQuery library make Ajax calls a lot easier Commented Jun 8, 2012 at 7:12
  • Ajax is a technique to load pages into your current page without refreshing and this is what you want Commented Jun 8, 2012 at 7:18
  • Updated the answer with html too! :) Commented Jun 8, 2012 at 7:21

2 Answers 2

1

Consider the PHP file for checking existing username as:

<?php
    if(mysql_num_rows(mysql_query($query)) === 1)
        die('true');
    else
        die('false');
?>

In your HTML File, you can call this way (using jQuery):

<script type="text/javascript">
function checkUsername()
{
    $.ajax({
      url: 'checkuser.php?username' = $('#username').val(),
      success: function(data) {
        if(data == 'true')
          alert('Username Available!'); // User Exists!
        else
          alert('Username not Available!'); // User not Exist!
      }
    });
}
</script>

And in HTML:

<input type="text" name="username" id="username" />
<a href="#" onclick="checkUsername(); return false;">Check</a>

Hope this helps! :)

Sign up to request clarification or add additional context in comments.

Comments

0

HTML <'input type = "TEXT" Id="TEXTTOPARSE"> <'Span id="ResultVAlue> On Load of the Page

  $(document).ready(function(){
   $('#TEXTTOPARSE').bind('onChange', function(){
       var val = $(this).val();
       updatePage(val);

   });
 });


function updatePage(value){

$.ajax({
  method:"POST",
  url:"YourPageLink"
  data:{"textValue" : value},
   success: function(data){
   $('#ResultVAlue').html(data);
  }

  });

  }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.