franksprings
franksprings

Reputation: 55

No SQL data returned through jQuery ajax post

I am having some issues returning data from on page, using jQuery, PHP and MySQL. I would like to show the query results on the index.php page. The results are fetched from the database using getResults.php.

When I manually run the getResults.php?id=123 file, all works fine. In this case i'm seeing the results I would like to. However, when I want to post the 'id' 123 in a submit form on index.php, I don't see any results returned by jQuery / getResults.php. Only thing that changed is the URL: index.php?id=123. However, I'm not seeing any results or an error message...

Any one an idea?

getResults.php file

$search = mysql_real_escape_string( isset ($_POST['id']));

if ($search) {
$friend = mysql_query(  " SELECT * FROM reviews WHERE fbuserid = '$search' ORDER BY datumtijd DESC" );

if ( $friend )  {   
    while ($row = mysql_fetch_array ($friend) ) {
            echo "Show some results...";
    }
} else {
    echo "No matches found";
}
} else {
echo "Query not succesfull";
} 

index.php file

<script type="text/javascript" charset="utf-8">

        $(document).ready(function() {

        $("#submit").click(function) {
            event.preventDefault();
            $.ajax({
                url:"getResults.php",
                type:"GET",
                data: "id="+this.value,
                success: function(data) {
                    $("#results").html(data);
                }
            });
        }
        return false;
    });

    </script>    

    <div>
        <form>
            <input type="text" name="id">
            <input type="submit" value="submit" id="submit">
        </form>
    </div>        
    <div id="results"></div>

EDIT: Thanks, all for your input. Nevertheless, I'm still not there... As you might notice, I'm not quite experienced with jQuery and PHP. See changes in scripts above. In short: - I added the false statement if ($query) is not succesfull; - Changed the $.post method into $.ajax; - Changed the GET into POST in the PHP file;

Any other suggestions?

Upvotes: 2

Views: 1646

Answers (4)

Pranit Mhatre
Pranit Mhatre

Reputation: 11

In the first line:

$search = mysql_real_escape_string( isset ($_POST['id'])); 

Try changing $_POST to $_GET:

$search = mysql_real_escape_string( isset ($_GET['id']));
                                           ^^^^^

Upvotes: 1

Quentin
Quentin

Reputation: 943579

You are POSTing data but your script is looking for it in GET.

Edit in response to massive rewrite of the code in the question:

  • The first argument of $.ajax should be the URL.
  • You are missing the { from your settings object.
  • You are using a = instead of : for your type parameter.
  • You aren't doing anything to stop the normal submission of the form, so the form gets submitted and a new page loaded before the HTTP request sent by the Ajax returns.

Upvotes: 1

TeAmEr
TeAmEr

Reputation: 4773

in if ($query) { is $query !== false ???

Upvotes: 0

Akhilesh Sharma
Akhilesh Sharma

Reputation: 1628

Its much better to use the jQuery ajax method to implement the ajax functionality in the script. As it is more generalized rather than specific to one METHOD. Below is the sample code to use the ajax method.

$(document).ready(function()
{
$("#submit").click(function)
{
$.ajax(
type="GET",
url:"getResults.php",
data: "id="+this.value,
success: function(msg)
{
$("#results").html(msg);
}
);
}
});

To check out more examples please refer to the JQUERY API Reference.

J

Upvotes: 0

Related Questions