I’m trying to get data from a specific table from my database on PhpMyAdmin with the help of AJAX
. To specify the table the data shall be fetched from, I implemented a selection with the table’s names as values.
How do I use AJAX to pass this value and fetch the data from the specific table?
So far I used $("#loadData").load("getData.php");
to load data, but this one doesn’t pass the category you can see in the beginning of the php file.
My PHP:
<?php
include_once('db.php');
$category = $_POST["category"];
$sql = "SELECT ID, model, power, fuel, color, age, price FROM $category";
$result = mysqli_query($conn, $sql);
$data = array();
...
2
Answers
Try this,
In jQuery, add an object as the second argument to
.load()
. The object should have properties that you want to retrieve via the php$_POST
keys:This should pass on the category “myCategory” into php’s
$_POST["category"]
;However, you should heed @Dharman’s warning that you mustn’t just run mysql searches based on input from the internet. You need to assume such inputs from the internet as untrustworthy.
At the VERY least, you need to validate that the
$_POST["category"]
value is a valid, approved category from a predetermined list before you run your mysql query.