Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

index.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="my.css">

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>


<script type="text/javascript">

$(function() {

    $(".search_button").click(function() {
        // getting the value that user typed
        var searchString    = $("#search_box").val();
        // forming the queryString
        var data            = 'search='+ searchString;

        // if searchString is not empty
        if(searchString) {
            // ajax call
            $.ajax({
                type: "POST",
                url: "query.php",
                data: data,
                beforeSend: function(html) { // this happens before actual 
                    $("#results").html(''); 
                    $("#searchresults").show();
                    $(".word").html(searchString);
               },
               success: function(html)
               { // this happens after we get results
                    $("#results").show();
                    $("#results").append(html);
               }
            });    
        }
        return false;
    });

    $(document).ready(function{
    $.ajax({
    url: "query.php"
    }).done(function(data) {
    $('body').html(data);
    });
        });
   });
  </script>


        <script type="text/javascript">

    $(document).ready(function() {


 $.ajax({
    type: "POST",
    url: "query.php",
    dataType: "text",
    data: dataString,
    success: function (response)
    {
      alert(response);    //alert responce from  query.php
    },
    error:function (xhr, ajaxOptions, thrownError)
   {
      alert(xhr);

    }
   });

  });

  </script>

</head>
<body >

<div id="container">
<div style="margin:20px auto; text-align: center;">
<form method="post" action="query.php"  >
    <input type="text" name="search" id="search_box" class='search_box'/>
    <input type="submit" value="Search" class="search_button" /><br />
</form>

</div> 


<div>

<div id="searchresults"></div>
<ul id="results" class="update">
</ul>

</div>
</div>

</body>
</html>



 query.php

<?php

        $user_name = "root";
        $password = "";
        $database = "my_db2";
        $server = "127.0.0.1";

        $db_handle = mysql_connect($server, $user_name, $password);
        $db_found = mysql_select_db($database, $db_handle);




        $SQL = "SELECT * FROM user WHERE Hometown = 'Quahog'";

        //searching for what was entered into the search box
        if (isset($_POST['search']))
        {           
            $search_term = mysql_real_escape_string($_POST['search']);

            //concatenating the $SQL variable above
            $SQL .= "AND id = '{$search_term}'";
            $SQL .= "OR Hometown = 'Quahog' AND FirstName = '{$search_term}'";
            $SQL .= "OR Hometown = 'Quahog' AND LastName = '{$search_term}'";
            $SQL .= "OR Hometown = 'Quahog' AND Age = '{$search_term}'";
            $SQL .= "OR Hometown = 'Quahog' AND Job = '{$search_term}'";

        }

        $result = mysql_query($SQL) or die(mysql_error());  
?>
<html>

        </head>

            <body>

            <h1> Persons</h1>

                <table border = "1"   width = "100%"    cellpadding = "5"   cellspacing = "2">

                <tr>
                    <td><strong>ID</strong></td>
                    <td><strong>First Name</strong></td>
                    <td><strong>Last Name</strong></td>
                    <td><strong>Age</strong></td>
                    <td><strong>Hometown</strong></td>
                    <td><strong>Job</strong></td>
                </tr>

                <?php                   
                            while ($row = mysql_fetch_array($result)) { 
                ?>

                <tr>
                    <td><?php echo $row['id']; ?></td>
                    <td><?php echo $row['FirstName']; ?></td>
                    <td><?php echo $row['LastName']; ?></td>
                    <td><?php echo $row['Age']; ?></td>
                    <td><?php echo $row['Hometown']; ?></td>
                    <td><?php echo $row['Job']; ?></td>


                </tr>
                <?php } ?>              

</table>    


            </body>

</html> 

I am new to javascript, jquery and ajax and I would like some help on modifying the above code so that when the page loads, I can view the results of a php/mysql query named 'query.php' into the index file of my html page. Any help will be greatly appreciated.

share|improve this question

2 Answers 2

$(document).ready(function() {

jQuery.ajax({
        type: "POST",  //  this is post request u can also do get request
        url: "query.php", 
        dataType: "text",

        success: function (response)  // this is the response from  url: "query.php",
        {
          alert(response);    //alert responce from  query.php and here you can do 
                              //                   whatever u like with response.
        },
        error:function (xhr, ajaxOptions, thrownError)
       {
          alert(xhr); // if any error function.

       }
});

});
share|improve this answer
    
i don't want to pass any values to the php file. the way it is supposed to work is that on loading the index.html file, the results of the query would be displayed and then a search can be performed to find what the user enters in the searchbox. –  user3251568 Jan 30 at 4:53
    
i will edit the answer –  ghost Jan 30 at 4:56
    
ok thanks a lot. can you also please put some comments so that i can understand the procedure? thank you –  user3251568 Jan 30 at 4:58

Use AJAX in yout index.html like this:

$(document).ready(function{
    $.ajax({
      url: "query.php"
    }).done(function(data) {
      $('body').html(data);
    });
});

More info on AJAX.

Make sure that you have included jQuery in your code.

share|improve this answer
    
Thank you for your quick response. I have included jQuery in the code already but the results of the query in the 'query.php' file is still not being loaded into the index.html file. –  user3251568 Jan 30 at 4:39
    
@user3251568 Make sure that query.php is in same folder as in index.html. Also, can you post your PHP and HTML code here? –  Rahul Desai Jan 30 at 4:40
    
yes both files are in the same folder –  user3251568 Jan 30 at 4:56
    
@user3251568 I see that you have put in the code in the question. Please put the code for $.ajax into $(function() .. ), (same as $(".search_button").click) –  Rahul Desai Jan 30 at 5:04
    
it's not working –  user3251568 Jan 30 at 5:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.