我试图在PHP中选择选项值,我想比较选定的值和数据库值,例如PHP中的seat id。但是,它会导致未定义的错误。你能查一下我的代码吗?
<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>","\n");
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);
?>
答案 0 :(得分:0)
这是一个猜测,但它看起来有2个GET
请求,参数和另一个没有 - 回调函数似乎正在使用没有参数的调用所以它可能看起来更多像:
$.get("p.php?pix="+ sel, function( dataFromtheServer ) {
$("#result").html( dataFromtheServer );
});
我不知道以下内容是否会有所帮助,但我不禁觉得ajax功能看起来很奇怪。
<html>
<head>
<title></title>
<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();
console.info( 'send ajax request: p.php?pix=%s', sel );
$.get("p.php?pix="+ sel, function(r) {
console.info('response: %s',r );
$("#result").html( r );
});
}
</script>
</head>
<body>
<div id="a" style="text-align:center;">
<?php
$servername = "";
$username = "";
$password = "";
$dbname = "";
$conn=new mysqli($servername, $username, $password, $dbname);
$sql='select * from `seat`;';
$result = $conn->query( $sql );
?>
<form name="abc" method="get">
<select name="select" id="pix">
<?php
if( $result ){
while( $rs=$result->fetch_object() ){
echo "<option value='{$rs->seat_id}'>" . $rs->seatNumber;
}
}
$conn->close();
?>
</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>
</body>
</html>
/* p.php */
$servername = "ec2-52-201-212-193.compute-1.amazonaws.com";
$username = "s2016b_user21";
$password = "s2016b_user21";
$dbname = "s2016b_user21";
/* alternative sql */
$conn = new mysqli( $servername, $username, $password, $dbname );
$sql='select `seatStatus` from `seat` where `seat_id`=?;';
$stmt=$conn->prepare( $sql );
$stmt->bind_param( 'i', $_GET['pix'] );
if( $stmt ){
$stmt->execute();
$stmt->bind_result( $status );
$stmt->fetch();
echo $status==1 ? 'available' : 'unavailable';
}
$conn->close();
答案 1 :(得分:0)
我不太了解JQuery
您可以尝试在javascript使用console.log(sel)
,在php使用var_dump($_GET['pix'])
以确保已获取“sel”的值。
//javascript
var pix = $("#pix option:selected").val();
console.log(pix);
//PHP
var_dump($_GET['pix']);
如果var_dump($_GET['pix'])
的结果为NULL,那么您的javascript代码看起来应该更像:
<script>
function ajaxWay() {
var sel = $("#pix option:selected").val();//check the value of sel
var url = "p.php";//check the path of p.php
$.get(url,{'pix':sel},function(dataFromtheServer) {
$("#result").html(dataFromtheServer);
});
}
</script>