KaushikTD
KaushikTD

Reputation: 277

How to pass a javascript variable to a php file, to run a db query and return true or false

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?

Upvotes: 0

Views: 521

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

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! :)

Upvotes: 1

Alex Kremer
Alex Kremer

Reputation: 128

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);
  }

  });

  }  

Upvotes: 0

Related Questions