views:

608

answers:

3

I need to check if a certain value "SheetNumber" is in my mysql database dynamically. I have seen it done on popular web sites and I am trying to find a good tutorial to outline it for me. I am fairly new to Javascript but i know a lot of PHP.

I am anxious to learn how to do this.

  • Chris
+2  A: 

It's pretty simple; check the jQuery docs on the AJAX functions: jQuery.get()

You'll need to set up a simple PHP service that returns raw data, XML, or JSON-formatted data that you can then interpret with your jQuery callback function.

Example:

function checkData(userInputValue) {
  jQuery.get('ajax-backend.php?value='+userInputValue, false, function(data) { alert('Response from server: ' + data)});
}

Instead of using alert() to display the response from the server, you'd probably want to interpret this value and change something on the page indicating whether the operation was successful.

Here's a more detailed example from a previous question: Beginning jQuery help

pix0r
+1  A: 

I assume that by "dynamically" you mean "without leaving the page", suggesting a "AJAX" solution.

You cannot the access your database using client-side JavaScript, I recommend using a JavaScript library/framework like JQuery to call a PHP script "behind the scenes" that does what you want.

Ferdinand Beyer
+4  A: 

You will need to do this using Ajax. I recommend the Jquery library. Install it using the Jquery documentation, and then use something like the following:

Javascript:

function makeAjaxRequest()
{
   var url="script-that-checks-db.php";
   $.get(url,{},verifyDb);
}

function verifyDb(response)
{
    if (response==1)
    {
       //The value exists, do what you want to do here
    }

    else
    {
      //The value doesn't exist
    }
}

You can have makeAjaxRequest() invoked when someone clicks a link, clicks a button, or anything else, e.g:

<a href="#" onclick="makeAjaxRequest();">Check database</a>

The php code of the file script-that-checks-db.php (of course, name it something different) will be responsible for checking the db. The code would look something like this.

PHP:

<?php

//Do the mysql query and find out if the value exists or not.

if ($exists==true)
   echo "1"; //1 will indicate to javascript that the value exists.
else
   echo "0";
?>

You could also use JSON here rather than the 0/1 method, but because you are new I think this will be simple enough for you.

Hope this helps, if you have any questions feel free to ask. Also, feel free to change the function and file names.

Click Upvote
Thank you! very helpful!
Chris B.
How would I retrieve the value entered into the field in the PHP Script?
Chris B.
Use $_POST['field_name'] or $_GET['field_name'] if you used $.get for the ajax
Click Upvote
Do i need to edit the {} portion of the jQuery get? Or will it be sent automatically? Thanks!
Chris B.
yes, you will edit it like this: {fieldName: fieldvalue}. Use commas to seperate the values. fieldValue will need to be a variable with the value you want to send of course
Click Upvote
if you have any other questions you can just make another question here on SO :)
Click Upvote
Thank you! I researched it and then asked a technical question and got the answer. You would need to call the value as such;{fieldname: $("input#fieldname").val()}
Chris B.
Here is the link: http://stackoverflow.com/questions/719550/sending-values-with-jquerys-get-function
Chris B.