2

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?

3
  • 2
    Helloooooo SQL injection security hole. bobby-tables.com Commented Feb 13, 2011 at 17:01
  • No it isn't, Little Bobby Tables can sill wander in and trash your database. Commented Feb 13, 2011 at 18:40
  • Forgot to add mysql_escape_string(). I'm not sure if this is sufficient. I guess not...I didn't know about bobby-tables.com. I'm not too familiar with sql injections, so I will dive into this later on. Commented Feb 13, 2011 at 19:25

4 Answers 4

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

David, thanks for your input. I added your suggestion. However, I'm not sure if this is cause that I don't see any results returned. Do you have additional suggestions?
Thanks. I added some JS. Among which event.preventDefault() to the stop the normal submission. Still struggling though. Keep u posted.
1

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']));
                                           ^^^^^

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.