0

I'm trying to retrieve information from a PHP page which queries a database based on the ?id= parameter given. Doing so via AJAX should give the user information on the specific item etc. If the id parameter does not exist within the database, the user will be redirected to the lookup page.

When looking at AJAX examples and following the same steps it would seem that the parameter isn't being used for the MySQL query - it's using the header information to redirect me and retrieve the lookup page as if an incorrect value was given however the ID exists.

Any ideas on what's going wrong?

var id = $('#code').val; is the input where users supply the ID they wish to lookup.

$(document).ready(function() {
$('#check').click(function() {
    var id = $('#code').val;
    if (id=="")
      {
      document.getElementById("result").innerHTML="";
      return;
      } 
    if (window.XMLHttpRequest)
      {
      xmlhttp=new XMLHttpRequest();
      }
    else
      {
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("result").innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open("GET","result.php?id="+id,true);
    xmlhttp.send();
});
});
JoshMc
  • 669
  • 5
  • 15
  • 1
    Why are you not using `$.ajax()` or `$.get()` if you're using jQuery? – rink.attendant.6 Jul 31 '13 at 17:34
  • I'm using examples found to retrieve it, I've never used AJAX before for this use. – JoshMc Jul 31 '13 at 17:36
  • 2
    $('#code').val; it is wrong .use **$('#code').val();**. because on this line var id = $('#code').val; id is undefined so if (id=="") { document.getElementById("result").innerHTML=""; return; } this code will not execute and make a ajax call without id. For more check ajax request header for what parameters are passing. – developerCK Jul 31 '13 at 17:43
  • Retrieved the data using this: $(document).ready(function() { $('#check').click(function() { var id = $('#code').val(); $.ajax({url:"result.php?id=" + id,success:function(result){ $("#result").html(result); }}); }); }); Works after correcting the error with the id variable. – JoshMc Jul 31 '13 at 17:45

2 Answers2

0

Further to what developerCK mentioned regarding the jQuery syntax error in this line:

var id = $('#code').val;  // Should be: var id = $('#code').val();

You can save yourself a lot of typing by switching to the jQuery syntax for the AJAX requests. Here are some posts for getting started with jQuery/AJAX:

A simple example

More complicated example

Populate dropdown 2 based on selection in dropdown 1

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
0

Try this code:

$(document).ready(function() {
    'use strict';

    $('#check').click(function() {
        var id = $('#code').val();

        if (id==="") {
            $("#result").html('');
            return;
        } 

        $.ajax({
            type: 'GET',
            url: 'result.php',
            data: {
                id: id
            }
        }).done(function(data){
            $("#result").html(data);
        });
    });
});
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156