Jump to content

twilitegxa

Members
  • Posts

    1,020
  • Joined

  • Last visited

Posts posted by twilitegxa

  1. Well, I added a group by name section to my where statement, and now I realize that some of the names are coming into the select option list, bt not all of them are the right ones. Some of them are from other locations, so I guess this has something to do with the JS that adds a random number to prevent the server from using a cached file. On one of the location options, it generates no options, and instead seems to be putting them with another location for some reason. My guess is that it's the JS, so I will try posting a question there about it, but if anyone knows what I's doing wrong here, please let me know. It's driving me crazy!

  2. No, I'm not exactly good with JS yet. I found this example online and used it, modifying the parts I needed. But the part I'm really confused about it that this JS worked on another example similar, so I figured it should work on this one, too. I have two select lists that should pop up and they do. The first one is on the page from the beginning, but the second one appears on the change of the first one, with the correct data according to what was selected from the list. The second select list should, on change, also populate another select list, but the data is wrong in this instance for some reason. When I check to see what value $q is getting, it appears to be that option that was selected from the second select list, which is right, because the where statement should get all the records from the table where the location field matches the value that they selected from the select list. But my other statement should be asking for the name field value, to display according to whatever location they chose from the last select list, but it won't display anything. For some reason, the name field can't be retrieved when I use the where statement. But, if I take the where statement out, the select list will populate with all the name field values from the table. So I need the where statement to make it only display all the name field values according to what location they picked. Can anyone see what I have done wrong? I think it must be in my php, since the JS seemed to be getting the right value. Like I said before, my other select list and JS works just fine. Here is the previous select list's JS and PHP:

     

    JS:

    //code to display locations
    
    var xmlhttp2;
    
    function showMap(str)
    {
    xmlhttp2=GetXmlHttpObject();
    if (xmlhttp2==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url2="getlocations.php";
    url2=url2+"?q="+str;
    url2=url2+"&sid="+Math.random();
    xmlhttp2.onreadystatechange=stateChanged2;
    xmlhttp2.open("GET",url2,true);
    xmlhttp2.send(null);
    }
    
    function stateChanged2()
    {
    if (xmlhttp2.readyState==4)
    {
    document.getElementById("txtHint2").innerHTML=xmlhttp2.responseText;
    }
    }
    
    function GetXmlHttpObject2()
    {
    if (window.XMLHttpRequest2)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest2();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    PHP:

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM monsters1 WHERE map = '".$q."' group by location";
    
    $result = mysql_query($sql);
    
    echo "<table border='0'>";
    echo "Select a location: <select name=\"locations\" onchange=\"showMonsters(this.value)\">
    <option selected=selected>None Selected</option>";
    
    while($row = mysql_fetch_array($result))
      {
      $location = $row['location'];
       echo "<option value=\"$location\">" . $row['location'] . "</option>";
      }
    echo "</select>";
    
    mysql_close($con);
    ?> 
    

     

    This code gets all the records from the same table where the map value has been chosen and then displays all the locations that have that map value in the map field. I just figured, if this one is working properly, the other one should be, too, but I can't get it to work. :-(

  3. I can't get my script to work. I have a js that runs a php script and displays the data in the div tag. The php script populates the select option list. But I can't get select list to populate with the right data. I want the previous list to determine which values display in the next select list. If I take out the where statement, all the values display from the table, but when I put in the where statement, none of the values display. What am I doing wrong?

     

    HTML:

    <?php
    $get_map = "select * from monsters1 group by map";
    $get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
    echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
    <option selected=\"selected\">None Selected</option>";
    while ($list_maps = mysql_fetch_array($get_map_res)) {
    $map_id = $list_maps['id'];
    $map = $list_maps['map'];
    echo "<option value=\"$map\">$map</option>";
    }
    echo "</select>";
    ?>
    <div id="txtHint2"></div>
    <br />
    <div id="txtHint3"></div>
    <br />
    <div id="txtHint"></div>
    

     

    The second JS section, where it says to display the monsters, is the part I'm having trouble with. As in, it associates with the part I'm havign trouble with.

     

    JavaScript:

    //code to display locations
    
    var xmlhttp2;
    
    function showMap(str)
    {
    xmlhttp2=GetXmlHttpObject();
    if (xmlhttp2==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url2="getlocations.php";
    url2=url2+"?q="+str;
    url2=url2+"&sid="+Math.random();
    xmlhttp2.onreadystatechange=stateChanged2;
    xmlhttp2.open("GET",url2,true);
    xmlhttp2.send(null);
    }
    
    function stateChanged2()
    {
    if (xmlhttp2.readyState==4)
    {
    document.getElementById("txtHint2").innerHTML=xmlhttp2.responseText;
    }
    }
    
    function GetXmlHttpObject2()
    {
    if (window.XMLHttpRequest2)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest2();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    
    //code to display monsters
    
    var xmlhttp3;
    
    function showMonsters(str)
    {
    xmlhttp3=GetXmlHttpObject();
    if (xmlhttp3==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url3="getmonsters.php";
    url3=url3+"?q="+str;
    url3=url3+"&sid="+Math.random();
    xmlhttp3.onreadystatechange=stateChanged3;
    xmlhttp3.open("GET",url3,true);
    xmlhttp3.send(null);
    }
    
    function stateChanged3()
    {
    if (xmlhttp3.readyState==4)
    {
    document.getElementById("txtHint3").innerHTML=xmlhttp3.responseText;
    }
    }
    
    function GetXmlHttpObject3()
    {
    if (window.XMLHttpRequest3)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest3();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    I can't figure out why my location is not being pushed through when the select list is changed.

     

    PHP:

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM monsters1 WHERE location = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='0'>";
    echo "Select a monster: <select name=\"monsters\">
    <option selected=selected>None Selected</option>";
    
    while($row = mysql_fetch_array($result))
      {
       echo "<option>" . $row['location'] . "</option>";
      }
    echo "</select>";
    
    mysql_close($con);
    ?>
    

     

    Can anyone help? I need the monsters select list to be populated with the name field values according to which location is chosen from the locations select list. Can anyone help me figure out why?

  4. What is PBBG? I wanted to make it a game that has a timer and gives a score after complete that saves the score into a table in a database as well, but I kind of thought flash would require me to make all the pieces myself first. I need to make a program that makes the pieces from the images.

  5. I figured it out. I changed the value of the options on the html page, then changed the where statement on the php page, and got it working perfectly:

     

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM monsters1 WHERE map = '".$q."' group by location";
    
    $result = mysql_query($sql);
    
    echo "<table border='0'>";
    echo "Select a location: <select>";
    
    while($row = mysql_fetch_array($result))
      {
       echo "<option>" . $row['location'] . "</option>";
      }
    echo "</select>";
    
    mysql_close($con);
    ?> 
    

     

    <?php
    $get_map = "select * from monsters1 group by map";
    $get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
    echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
    <option selected=\"selected\">None Selected</option>";
    while ($list_maps = mysql_fetch_array($get_map_res)) {
    $map_id = $list_maps['id'];
    $map = $list_maps['map'];
    echo "<option value=\"$map\">$map</option>";
    }
    echo "</select>";
    ?>
    <div id="txtHint2"></div>
    

  6. I have the following HTML page:

     

    <?php
    $get_map = "select * from monsters1 group by map";
    $get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
    echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
    <option selected=\"selected\">None Selected</option>";
    while ($list_maps = mysql_fetch_array($get_map_res)) {
    $map_id = $list_maps['id'];
    $map = $list_maps['map'];
    echo "<option value=\"$map_id\">$map</option>";
    }
    echo "</select>";
    ?>
    <div id="txtHint2"></div>
    

     

    This page, when the user chooses an option, the js is called that populates the div tag:

     

    js:

    var xmlhttp2;
    
    function showMap(str)
    {
    xmlhttp2=GetXmlHttpObject();
    if (xmlhttp2==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url2="getlocations.php";
    url2=url2+"?q="+str;
    url2=url2+"&sid="+Math.random();
    xmlhttp2.onreadystatechange=stateChanged2;
    xmlhttp2.open("GET",url2,true);
    xmlhttp2.send(null);
    }
    
    function stateChanged2()
    {
    if (xmlhttp2.readyState==4)
    {
    document.getElementById("txtHint2").innerHTML=xmlhttp2.responseText;
    }
    }
    
    function GetXmlHttpObject2()
    {
    if (window.XMLHttpRequest2)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest2();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    This php page is what the js uses to populate the div tag on the HTML page:

     

    php:

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM monsters1 WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='0'>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "Select a location: <select>";
      echo "<option>" . $row['location'] . "</option>";
      }
    echo "</select>";
    
    mysql_close($con);
    ?> 
    

     

    But right now, it's sending the id, but what I want it to do is when the user chooses from the select list, the div tag is populated by another select option list that fills in all the unique values from the location field based on what they chose from the previous select option list (which was from the map field). Can anyone help me out with how to do that? Do I need to send the location field through the js instead of the id, and how can I do that?

  7. I have the following script:

     

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM monsters1 WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='0'>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "Select a location: <select>";
      echo "<option>" . $row['location'] . "</option>";
      }
    echo "</select>";
    
    mysql_close($con);
    ?> 
    

     

    This code starts with an HTML page that uses a js and the above php script to fill the div area:

     

    HTML:

    <?php
    $get_map = "select * from monsters1 group by map";
    $get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
    echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
    <option selected=\"selected\">None Selected</option>";
    while ($list_maps = mysql_fetch_array($get_map_res)) {
    $map_id = $list_maps['id'];
    $map = $list_maps['map'];
    echo "<option value=\"$map_id\">$map</option>";
    }
    echo "</select>";
    ?>
    <div id="txtHint2"></div>
    

     

    js script:

    var xmlhttp2;
    
    function showMap(str)
    {
    xmlhttp2=GetXmlHttpObject();
    if (xmlhttp2==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url2="getlocations.php";
    url2=url2+"?q="+str;
    url2=url2+"&sid="+Math.random();
    xmlhttp2.onreadystatechange=stateChanged2;
    xmlhttp2.open("GET",url2,true);
    xmlhttp2.send(null);
    }
    
    function stateChanged2()
    {
    if (xmlhttp2.readyState==4)
    {
    document.getElementById("txtHint2").innerHTML=xmlhttp2.responseText;
    }
    }
    
    function GetXmlHttpObject2()
    {
    if (window.XMLHttpRequest2)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest2();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    But right now, $q is the id, so whenever the user selects an option from the first select list, the second select list only populates with one record. What I want it to do is populate with all unique values according to the map field, because there are multiple records that have the same value in map, so I want all field with the same value in map to show those records in the location field. Location also has many fields with the same value, so I want this select list to populate with all unique values. Can anyone help? What am I doing wrong? I don't know how to set the where statement to display the values from location based on the map field and not the id field. Can anyone help?

  8. I have this code for a select option list:

     

    <?php
    $get_map = "select * from monsters1";
    $get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
    echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
    <option selected=\"selected\">None Selected</option>";
    while ($list_maps = mysql_fetch_array($get_map_res)) {
    $map_id = $list_maps['id'];
    $map = $list_maps['map'];
    echo "<option value=\"$map_id\">$map</option>";
    }
    echo "</select>";
    ?>
    

     

    But I have several records with the same value. How can I make it to where each value only displays once in my list?

  9. How can I set my table to where it's always the same size? The way i have it now, when an identity is longer, the table gets bigger. I want the table to always look the same length.

     

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM derived_values WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>Identity</th>
    <th>Health</th>
    <th>Energy</th>
    <th>Attack</th>
    <th>Defense</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['identity'] . "</td>";
      echo "<td>" . $row['health'] . "</td>";
      echo "<td>" . $row['energy'] . "</td>";
      echo "<td>" . $row['acv1'] . "</td>";
      echo "<td>" . $row['dcv1'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    

     

    How can i do that?

  10. I have the following HTML page:

     

    <form>
    Select a character:
    
    <?php
    $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
    $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
    echo "<select name=\"users\" onchange=\"showUser(this.value)\">
    <option selected=\"selected\">None Selected</option>";
        while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
        $identity = ucwords($list_scouts['identity']);
        $topic_id = $list_scouts['id'];
    echo "<OPTION value=\"$topic_id\">$identity</OPTION>";
    }
    echo "</select>";
    ?>
    
    </form>
    <br />
    <div id="txtHint"></div>
    </div>
    

     

    And then a JavaScript gets this PHP script to display the information once selected:

     

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM derived_values WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>Identity</th>
    <th>Health</th>
    <th>Energy</th>
    <th>Attack</th>
    <th>Defense</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['identity'] . "</td>";
      echo "<td>" . $row['health'] . "</td>";
      echo "<td>" . $row['energy'] . "</td>";
      echo "<td>" . $row['acv1'] . "</td>";
      echo "<td>" . $row['dcv1'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    

     

    I want to know, how can I make an if statement that hides the table if the "None Selected" value is chosen from the select option list? I know it would be like:

     

    if (something == "None Selected") {

    div id "txtHint" display: none}

    else {

    execute the code I already have }

     

    I just don't know exactly how to write it. Can anyone help?

  11. Wow, I figured it out on my own guys! Here is what I did:

     

    <?php
    $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
    $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
    echo "<select name=\"users\" onchange=\"showUser(this.value)\">";
        while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
        $identity = ucwords($list_scouts['identity']);
        $topic_id = $list_scouts['id'];
    echo "<OPTION value=\"$topic_id\">$identity</OPTION>";
    }
    echo "</select>";
    ?>
    

     

    All I did was add the value as the id, and I got it. Yay!

  12. I have the following php page:

     

    <form>
    
    </select>
    
    <?php
    $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
    $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
    echo "<select name=\"users\" onchange=\"showUser(this.value)\">";
        while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
        $identity = ucwords($list_scouts['identity']);
        $topic_id = $list_scouts['id'];
    echo "<OPTION>$identity</OPTION>";
    }
    echo "</select>";
    ?>
    
    </form>
    <br />
    <div id="txtHint"></div>
    </div>
    

     

    And it accesses this js:

     

    var xmlhttp;
    
    function showUser(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="getuser.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

     

    But I am having some trouble because I want to send the identity field when it accesses the next php script:

     

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("smrpg", $con);
    
    $sql="SELECT * FROM ajax_demo WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['FirstName'] . "</td>";
      echo "<td>" . $row['LastName'] . "</td>";
      echo "<td>" . $row['Age'] . "</td>";
      echo "<td>" . $row['Hometown'] . "</td>";
      echo "<td>" . $row['Job'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    

     

    How can I send the identity field through the js to the php script? I am using this script from a tutorial I found online, but I don't understand the js enough to understand how to get the identity field and send it. Can anyone help?

  13. Oh, sorry guys, I figured it out. I had my code a little out of order:

     

    <?php
    $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
    $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
    echo "<select>";
        while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
        $identity = ucwords($list_scouts['identity']);
        $topic_id = $list_scouts['id'];
    echo "<OPTION>$identity</OPTION>";
    }
    echo "</select>";
    ?>
    

  14. I have the following code:

     

    <?php
    $get_scouts = "select * from scouts where username = '".$_SESSION['userName']."'";
    $get_scouts_res = mysql_query($get_scouts, $conn) or die(mysql_error());
        while ($list_scouts = mysql_fetch_array($get_scouts_res)) {
        $identity = ucwords($list_scouts['identity']);
        $topic_id = $list_scouts['id'];
    echo "<select><OPTION>$identity</OPTION></select>";
    }
    ?>
    

     

    How can I make each option appear in the select list? Currently, each record is in its own select list, side by side. I know I am doing something wrong, but I don't know how to fix it. Can anyone help? I think it's something with num_rows or something like that. can anyone help?

  15. I have the following page:

     

    getuser.php

    <?php
    $q=$_GET["q"];
    
    $con = mysql_connect('localhost', 'root', '');
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("ajax_demo", $con);
    
    $sql="SELECT * FROM user WHERE id = '".$q."'";
    
    $result = mysql_query($sql);
    
    echo "<table border='1'>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    
    while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['FirstName'] . "</td>";
      echo "<td>" . $row['LastName'] . "</td>";
      echo "<td>" . $row['Age'] . "</td>";
      echo "<td>" . $row['Hometown'] . "</td>";
      echo "<td>" . $row['Job'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    
    mysql_close($con);
    ?> 
    

     

    But I am getting this error:

     

    Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\getuser.php on line 25

     

    I can't figure out what I am doing wrong. I got it from a tutorial for Ajax and PHP with MySQL. Here are the other two pages:

     

    html page:

    <html>
    <head>
    <script type="text/javascript" src="selectuser.js"></script>
    </head>
    <body>
    
    <form>
    Select a User:
    <select name="users" onchange="showUser(this.value)">
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
    </select>
    </form>
    <br />
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    
    </body>
    </html>
    

     

    selectuser.js:

    var xmlhttp;
    
    function showUser(str)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="getuser.php";
    url=url+"?q="+str;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
    if (xmlhttp.readyState==4)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    
    function GetXmlHttpObject()
    {
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      return new XMLHttpRequest();
      }
    if (window.ActiveXObject)
      {
      // code for IE6, IE5
      return new ActiveXObject("Microsoft.XMLHTTP");
      }
    return null;
    }
    

    I got it from this page:

     

    http://www.w3schools.com/PHP/php_ajax_database.asp

     

    Can anyone help me with why I am getting this error?Here is the table SQL statement:

     

    CREATE TABLE IF NOT EXISTS `ajax_demo` (

      `id` int(11) NOT NULL,

      `FirstName` varchar(150) NOT NULL,

      `LastName` varchar(150) NOT NULL,

      `Age` int(11) NOT NULL,

      `Hometown` varchar(150) NOT NULL,

      `Job` varchar(150) NOT NULL

    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

     

    INSERT INTO `ajax_demo` (`id`, `FirstName`, `LastName`, `Age`, `Hometown`, `Job`) VALUES

    (1, 'Peter', 'Griffin', 41, 'Quahog', 'Brewery'),

    (2, 'Lois', 'Griffin', 40, 'Newport', 'Piano Teacher'),

    (3, 'Joseph', 'Swanson', 39, 'Quahog', 'Police Officer'),

    (4, 'Glenn', 'Quagmire', 41, 'Quahog', 'Pilot');

  16. I have the following section written in PHP:

     

    <?php
    
    $display_block .= "<input type=button value=\"Generate Random Monster\">";
    
    $get_monsters = "select * from monsters1 order by rand() limit 1";
    $get_monsters_res = mysql_query($get_monsters, $conn) or die(mysql_error());
    
    $display_block .= "<table class=monster>";
    
    while ($monsters_info = mysql_fetch_array($get_monsters_res)) {
    $monster_id = $monsters_info['id'];
    $monster_name = $monsters_info['name'];
    $monster_map = $monsters_info['map'];
    $monster_location = $monsters_info['location'];
    $monster_hit_points = $monsters_info['hit_points'];
    $monster_attack = $monsters_info['attack'];
    $monster_defense = $monsters_info['defense'];
    $monster_agility = $monsters_info['agility'];
    $monster_wisdom = $monsters_info['wisdom'];
    $monster_magic_endurance = $monsters_info['magic_endurance'];
    $monster_number_of_attacks = $monsters_info['number_of_attacks'];
    $monster_range = $monsters_info['range'];
    $monster_experience_points = $monsters_info['experience_points'];
    $monster_silver = $monsters_info['silver'];
    
    $display_block .= "<tr><td>$monster_name</td>";
    
    }
    
    $display_block .= "</tr></table>";
    
    ?>
    

     

    How do I set the table to be invisible until the user clicks the the button? I'd also like the button to run the code that generates the random record from my table. Currently I just have the table displaying the random record. Can anyone help?

  17. I found the way to display a random record:

     

    $get_monsters = "select * from monsters1 order by rand() limit 1";

     

    But can anyone help me with how to set the button to display the random record and to make the table that displays the record invisible until the user clicks the button?

     

    I think that it maybe a JavaScript function, so I will close this post and write one in the JavaScript section.

  18. I have the following code:

     

    <?php 
    $display_block .= "<input type=button value=\"Generate Random Monster\">";
    
    $get_monsters = "select * from monsters1";
    $get_monsters_res = mysql_query($get_monsters, $conn) or die(mysql_error());
    
    $display_block .= "<table class=monster>";
    
    while ($monsters_info = mysql_fetch_array($get_monsters_res)) {
    $monster_id = $monsters_info['id'];
    $monster_name = $monsters_info['name'];
    $monster_map = $monsters_info['map'];
    $monster_location = $monsters_info['location'];
    $monster_hit_points = $monsters_info['hit_points'];
    $monster_attack = $monsters_info['attack'];
    $monster_defense = $monsters_info['defense'];
    $monster_agility = $monsters_info['agility'];
    $monster_wisdom = $monsters_info['wisdom'];
    $monster_magic_endurance = $monsters_info['magic_endurance'];
    $monster_number_of_attacks = $monsters_info['number_of_attacks'];
    $monster_range = $monsters_info['range'];
    $monster_experience_points = $monsters_info['experience_points'];
    $monster_silver = $monsters_info['silver'];
    
    $display_block .= "<tr><td>$monster_name</td>";
    
    }
    ?>
    

     

    And what I want to do is when they click the button (Generate Random Monster), that it displays a random record from the table. All I want the user to see is the "monster_name". Can anyone tell me how to make the button show a random record from the table in my database? I also would like the table information to be invisible to the user until they click the button. How can I do that?

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.