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.

I have a database of schools, I would like to make it so that when you search the database for existing data, it outputs right next the the input box. So without going to a new page. Here's what a have:

<form action=" " method="post">
School's name: <input type="text" name="schoolname"> <br/>
<input type="submit" name="button" value="Search">
</form>

<?php
$school      = $_POST['schoolname'];

$conn = mysql_connect("localhost", "root");
mysql_select_db("finalproject");

$sql = "select * from presentations where school like '%$school%'";

$result = mysql_query($sql, $conn) or die(mysql_error());

if ( mysql_num_rows($result) >0)
    {
    while ($newArray = mysql_fetch_array($result))
        {
        $school  = $newArray['school'];
        $date = $newArray['date'];
        $place  = $newArray['place'];
        $time = $newArray['time'];


        echo $school . ", " . $place . ", " . $date . ", " . $time . "<br />" ;
        }
    }
    else 
        {
        echo "Record not found";
        }



mysql_close($conn);
?>

This is code that I have used previously to link to another page, outputting there. but now I just want to output it on the same page. I did move some code over from the other page which no longer seems to be working. The PHP bit just outputs: "0) { while ($newArray = mysql_fetch_array($result)) { $school = $newArray['school']; $date = $newArray['date']; $place = $newArray['place']; $time = $newArray['time']; echo $school . ", " . $place . ", " . $date . ", " . $time . " " ; } } else { echo "Record not found"; } mysql_close($conn); ?>" onto my page below the input. I'm really new to this, so anyones help would be greatly appreciated. :D

share|improve this question
 
what problem you are facing to display in a same page? –  Suresh Kamrushi Jan 7 at 18:56
 
Just make sure that $_POST['schoolname'] isset before searching the database. –  aziz punjani Jan 7 at 18:57
 
I don't understand your question. –  Stephen Sparks Jan 7 at 18:58
 
And @azizpunjani isn't set already? Through the form? –  Stephen Sparks Jan 7 at 19:00
1  
It needs to be .php so that apache knows it needs to be passed onto the php interpreter. Unless you modified settings explicitly telling apache to parse .html files. –  aziz punjani Jan 7 at 19:17
show 13 more comments

3 Answers

up vote 0 down vote accepted

Make sure that your file extension is .php. Also check if $_POST['schooname'] isset and if it is then continue with php code. A few other pointers, the use of the mysql extension is not recommended since it's deprecated. Use either mysqli or PDO, also santize your input if you must use it. I left the sanitation bit to you.

<form action=" " method="post">
School's name: <input type="text" name="schoolname"> <br/>
<input type="submit" name="button" value="Search">
</form>

<?php

if( isset( $_POST['schoolname'] ) && strlen( trim( $_POST['schoolname'] ) ) > 0  )
{
        $school      = $_POST['schoolname'];

        $conn = mysql_connect("localhost", "root");
        mysql_select_db("finalproject");

        $sql = "select * from presentations where school like '%$school%'";

        $result = mysql_query($sql, $conn) or die(mysql_error());

        if ( mysql_num_rows($result) >0)
        {
            while ($newArray = mysql_fetch_array($result))
            {
                $school  = $newArray['school'];
                $date = $newArray['date'];
                $place  = $newArray['place'];
                $time = $newArray['time'];


                echo $school . ", " . $place . ", " . $date . ", " . $time . "<br />" ;
            }
        }
        else 
        {
            echo "Record not found";
        }



        mysql_close($conn);
}
?>
share|improve this answer
add comment

You must use Ajax to make requests to server without page reloading.

share|improve this answer
add comment

You could modify this little snippet for the ajax part...It's using jQuery, so you'll need to include the library into your page.

$('#submitButtonID').click(function(){

var data = {
    schoolName: $('#schoolName').val()
};
$.ajax({
    url: "PhpPageWithQuery.php",
    type: "post",
    data: data,
    success: function(msg) {
        $('#resultsDiv).html(msg);

    }
});
share|improve this answer
add comment

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.