0

I'm trying to create a page that lists some notifications according to the user id.

I have this Javascript function to get the results as JSON

  var url="http://localhost/app/demo/getNotifications.php";
    $$.getJSON(url, function (result) {
      $$.each(result, function (i, field) {

        var nID = field.id;
        var nDonor = field.donor_id;

        // APPENDS DATA TO LISTFVIEW
        $$("#notlistview").append("HTML HERE");
      });

    });

And this is the php file:

<?php
include "db1.php";

$USERID = " ";

$data=array();
$q=mysqli_query($con,"SELECT * FROM blood_donations WHERE requester_id='USERID'  ORDER BY `id` DESC");
while ($row=mysqli_fetch_object($q)){
 $data[]=$row;
}
echo json_encode($data);
?>

My question is how do I pass a value from javascript to fill in the $USERID in the php file with the sent Value?

I only need to retrieve the results related to the USERID, so if you have another way, that's fine too.

1
  • your code is vulnerable to sql injection attacks; Commented Mar 22, 2017 at 21:40

2 Answers 2

1

You use the builtin global $_GET to retrieve parameters:

<?php
if (isset($_GET['userid'])) {
    $USERID = $_GET['userid'];
} else {
    $USERID = " ";
}
?>

And to pass the value from jQuery:

$.getJSON(url,
{
    userid: myUserID 
},
function (result) {
        // your result function
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! let me give it a try and get back to you.
1

Change this...

$$.getJSON(url, function (result) {

To this...

$$.getJSON(url, {userID:123}, function (result) {

Set 123 to whatever value you want.

Then in your PHP get your variable from $_GET['userID']

Be careful. Your script is vulnerable to SQL injection.

3 Comments

Thank you for your answer, same as first answer which I already tried! But I really appreciate you taking the time to help me.
No problem. I'm new to stack overflow. I find it frustrating when I have taken time to answer a question and half way though, writing the answer, someone else comes in with the same answer before me and steals my thunder! Ah well!
Haha, yeah it happened to me before! this is how it is, first correct answer and you're obligated to mark it as complete, lol. Thanks and sorry again.

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.