PHP, MySQL & ajax with data on default page
default page is empty when page first load.
how can i show data on default page? i mean when page first load i want to show all data then do the ajax after user select to filter

Example is based off of:
http://www.daniweb.com/web-development/php/threads/372465/multiple-input-filtering-php-mysql-ajax#post1817022
using code posted by vibhaJ
http://www.w3schools.com/php/php_ajax_database.asp

    <html>
    <head>
    <script type="text/javascript">
    function showUser()
    {
    var users = document.getElementById('users').value;
    var sex = document.getElementById('sex').value;
    if (users=="" && sex=="")
    {
    document.getElementById("txtHint").innerHTML="";
    return;
    }
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","getuser.php?users="+users+"&sex="+sex,true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>
    <form>
    <select name="users" id="users" onChange="showUser()">
    <option value="">Select a person:</option>
    <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>
    <select name="sex" id="sex" onchange="showUser()">
    <option value="">Male/Female:</option>
    <option value="1">All</option>
    <option value="2">Male</option>
    <option value="3">Female</option>
    </select>
    </form>
    <div id="txtHint"><b>Person info will be listed here.</b></div>
    </body>
    </html>

Recommended Answers

All 2 Replies

could use:

<script type='text/javascript'>showUser();</script>

Just before body closes, you will need to set peter griffin and the all options selected for it to run as well(<option value='1' SELECTED>Peter griffin</option>). Or you could just put a mysql query into the php to pull the data you want to show on page load.

i tried putting value

 <select name="users" id="users" onChange="showUser(<option value='1' SELECTED>Peter griffin</option>)">

      <option value='1' SELECTED>Peter griffin</option>

doestnt work

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.