I tried to get selected option value in PHP, and I wanted to compare between selected value and database value such as seat id in PHP. However, it causes undefined error. Can you check my code?
<script type="text/javascript">
function transfer(){
var pix = $("#pix option:selected").val();
document.abc.test.value =pix;
}
</script>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">
</script>
<script>
function ajaxWay() {
var sel = $("#pix option:selected").val();
$.get("p.php?pix="+ sel );
// syntax: $.post(URL,data,callback);
$.get("p.php", function(dataFromtheServer) {
$("#result").html(dataFromtheServer);
});
}
</script>
<body>
<div id="a" style="text-align:center;">
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM seat" ;
$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
?>
<form name="abc" method="get">
<select name="select" id="pix">
<?php
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
echo "<option value='".$row[seat_id]. "'>" .$row[seatNumber]. "</option>";
}
}
mysqli_close($conn);
?>
</select>
<input type="button" value="click" onclick="transfer();">
<input type="button" value="click" onclick="ajaxWay()">
<input type="text" name="test" id="text">
<aside id="result">i will put the empty space from database</aside>
</form>
p.php
<?php
$servername = "***.compute-1.amazonaws.com";
$username = "***";
$password = "***";
$dbname = "***";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM seat";
$result = mysqli_query($conn, $sql) or die("Query: ($sql) [problem]");
$fields = mysqli_num_fields($result);
if (mysqli_num_rows($result) > 0) {
// output data of each row
// display("<table border=1><th>firstname</th><th>lastname</th><th>items</th>","
");
while($row = mysqli_fetch_assoc($result)) {
if($_GET['pix'] == $row['seat_id']){
if($row['seatStatus'] == 1)
{
echo "available" ;
}
else
{
echo "unavailable";
}
}
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>