Chase Price
Chase Price

Reputation: 69

Search MySQL using PHP and a variable located in HTML

I am trying to make php search my database of parts by a part number that is generated in the html page and then output the price into a cell.
Here is my Ajax script and variable

var Row = document.getElementById("test2");
var Cells = Row.getElementsByTagName("td");
$myPartNumber = Cells[1].innerText;

$.ajax({
type: 'POST',
url: "http://localhost/filenamehere.php",
data: { Part_Number : $myPartNumber },
dataType: 'html',
async: true,
success: function(data) {
    $('#price').html(data);
}
});
}

Here is my PHP code

$result = mysqli_query($con,"SELECT * FROM nipple_list where Part_Number='$myPartNumber' ");

echo "<table border='1'>
<tr>
<th>LDS Price $</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['LDS_Price_$'] . "</td>";
echo "</tr>";
}
echo "</table>";

I keep getting errors and warnings like

Notice: Undefined variable: myPartNumber in C:\Apache24\htdocs\filenamehere.php on line 10

Upvotes: 0

Views: 40

Answers (1)

bassxzero
bassxzero

Reputation: 5041

Since you are using post in your ajax your values will be in the $_POST global in your PHP script.

change

"SELECT * FROM nipple_list where Part_Number='$myPartNumber'"

to

"SELECT * FROM nipple_list where Part_Number='{$_POST['Part_Number']}'"

Also you're at risk of a sql injection, you should be using prepared queries. How can I prevent SQL injection in PHP?

Upvotes: 2

Related Questions