0

I know how to send data from PHP to the react component but how do i send an id selected in a menu from the react component to the PHP file?

$.ajax({url: '/file.php', type: 'POST',
          dataType: 'json',
      success: response => {
        console.log(response)
      }});

And in the PHP file:

$testid= //how do i send a variable here
$results = $db->statement("SELECT * FROM tablename where id='testid");

echo json_encode($results);
Stephen King
  • 581
  • 5
  • 18
  • 31
  • That's not a react component it looks like a jQuery ajax call. What does your component look like? – George Aug 09 '17 at 11:45

2 Answers2

0
$.ajax(
{
    url: '/file.php',
    type: 'POST',
    dataType: 'json',
    data: {
        'name': 'john'
    },
    success: response => {
        console.log(response)
    }
});

PHP file:

$testid= $_POST["name"];
$results = $db->statement("SELECT * FROM tablename where id = $testid");

echo json_encode($results);

WARNING: YOUR SQL QUERY WILL BE VULNERABLE TO SQL INJECTION. LEARN HOW TO PREVENT SQL INJECTION.

Anis Alibegić
  • 2,941
  • 3
  • 13
  • 28
0

you need to send the id via JavaScript

$.ajax({url: '/file.php', data : {userId : ?} , type: 'POST', dataType: 'json',
           success: response => {
              console.log(response)
           }});

and then you need to get the value from $_POST and concat it with the query

$testid= $_POST["userId"];
$results = $db->statement("SELECT * FROM tablename where id = " . $testid);

but this could cause SQL Injection

Ali Faris
  • 17,754
  • 10
  • 45
  • 70