Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2011
    Posts
    49
    Thanks
    8
    Thanked 2 Times in 2 Posts

    AJAX Undefined Error problem

    Hi, I currently keep receiving this error message in my AJAX code. Basically I have a HTML page with a drop down list, where when you pick a football club an AJAX call is made to the database to retrieve information and display it.

    However I keep getting this error -

    Code:
    Undefined index: q in AJAX_PHP\getuser.php on line 3
    But when I select my items it from the list, the information from the database is displayed.

    HTML/JavaScript Code:
    PHP Code:
    <!DOCTYPE HTML>
    <
    html>
    <
    head>
    <
    meta charset="utf-8">
    <!--<
    script type="text/javascript" src="jscript_club.js"></script>-->
    <title>AJAX, PHP, JavaScript</title>
        <script type="text/javascript">
        function showUser(str){
            
        if(str=="")
        {
            document.getElementById("hint").innerHTML="";
            return;
        }
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }
        else
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readystatechange==4 && xmlhttp.status==200)
            {
                document.getElementById("hint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
        }
        </script>
    </head>

    <body>
    <form>
    <select name="users" onChange="showUser(this.value)">
    <option value="">Select a Club</option>
    <option value="1">Arsenal</option>
    <option value="2">Manchester United</option>
    <option value="3">Chelsea</option>
    </select>
    </form>
    <div id="hint"></div>
    </body>
    </html> 
    PHP Code:
    PHP Code:
    <?php

        $q
    =$_GET["q"];
        
        require_once 
    "login.php";
        
    $db_server mysql_connect($db_hostname$db_username$db_password);
        if(!
    $db_server) die("Unable to connect to server: " mysql_error());
        
    mysql_select_db($db_database$db_server) or die("Unable to select database: " mysql_error());
        
        
        
    $query "SELECT * FROM premier WHERE id = '".$q."'";
        
    $result mysql_query($query);
        if(!
    $result) die('Unable to execute query: ' mysql_error());
        
        echo 
    "<table border='1'>
        <tr>
        <th>Club Name</th>
        <th>Stadium</th>
        </tr>"
    ;
        
        while(
    $row mysql_fetch_array($result))
        {
            echo 
    "<tr>";
            echo 
    "<td>" $row['name'] . "</td>";
            echo 
    "<td>" $row['stadium'] . "</td>";
            echo 
    "</tr>";
        }
        echo 
    "</table>";
        
        
    mysql_close($db_server);

    ?>
    Last edited by RiskyShenanigan; 10-25-2011 at 08:31 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This PHP needs to be rewritten to handle 'q' only if it exists:
    PHP Code:
    <?php

    require_once "login.php";

    if (isset(
    $_GET['q']))
    {
        
    $q=$_GET["q"];
        
        
    $db_server mysql_connect($db_hostname$db_username$db_password);
        if(!
    $db_server) die("Unable to connect to server: " mysql_error());
        
    mysql_select_db($db_database$db_server) or die("Unable to select database: " mysql_error());
        
        
        
    $query "SELECT * FROM premier WHERE id = '".$q."'";
        
    $result mysql_query($query);
        if(!
    $result) die('Unable to execute query: ' mysql_error());
        
        echo 
    "<table border='1'>
        <tr>
        <th>Club Name</th>
        <th>Stadium</th>
        </tr>"
    ;
        
        while(
    $row mysql_fetch_array($result))
        {
            echo 
    "<tr>";
            echo 
    "<td>" $row['name'] . "</td>";
            echo 
    "<td>" $row['stadium'] . "</td>";
            echo 
    "</tr>";
        }
        echo 
    "</table>";
        
        
    mysql_close($db_server);
    }
    ?>
    Simple as that.
    The rest is a js issue. I wouldn't expect that you can use this.value on a select tag as a select tag cannot have a value. I believe you need to fetch the selected option index from the possible options.
    Code:
    function showUser(sel)
        var str = sel.options[sel.selectedIndex].value;
    ...
    I think thats how you do it in JS.

    Edit:
    Oh, the options onchange will need to be passed just 'this', not 'this.value'.

  • #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,300
    Thanks
    4
    Thanked 203 Times in 200 Posts
    Asked and answered here also.
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    RiskyShenanigan (10-25-2011)

  • #4
    New Coder
    Join Date
    Jun 2011
    Posts
    49
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by djm0219 View Post
    Asked and answered here also.
    Cheers, I was told to come here since it was a PHP error.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •