我有两个问题。第一个问题是它提出了一个通知,说明roomChosen未定义,这是一个php错误,其次,它没有;如果文本框中的房间号与数据库中的房间号不匹配,则显示javascript错误。 / p>
请在此处查看应用程序:application
在courseId文本框中输入“info101”,然后单击“提交”。您将在页面中看到所有功能。在底部,您将看到“准备问题”按钮,单击它,您将看到所有验证消息,包括下面的空房间文本框。现在输入一个房间号码,例如42,这是一个incorrext,因为它不在数据库中,如果你再点击“准备问题”按钮,你会看到没有消息说明房间无效。
为什么它不起作用?为什么我会收到你会看到的通知?
下面按正确顺序显示相关代码:
房间文本框的Javascript验证:
function validation() {
var isDataValid = true;
var roomTextO = document.getElementById("room");
var errRoomMsgO = document.getElementById("roomAlert");
if (roomTextO.value == "") {
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
} else if (!trimmedRoomText.length) {
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
//above is if room textbox is empty
} else if(roomTextO.getAttribute("roomthere") == false) {
errRoomMsgO.innerHTML = "This Room is Invalid";
//above is if room number in textbox does not match database, which at moment it isn't working
} else {
errRoomMsgO.innerHTML = "";
}
return isDataValid;
}
试图查看房间文本框中的值是否与数据库中的值匹配的PHP代码
<?php
$username="xxx";
$password="xxx";
$database="mobile_app";
$room_there = true;
$roomresult = mysql_query( "SELECT Room FROM Room WHERE Room = " . (int) $_POST['roomChosen']);
if (isset($_POST['roomChosen'])) {
$roomresult = mysql_query( "SELECT Room FROM Room WHERE Room = " . (int) $_POST['roomChosen']);
} else {
$room_there = false;
}
if( mysql_num_rows( $roomresult ) == 0 ) $room_there = false;
?>
下面是Room Textbox的Html和提交标签
的按钮<p><input type="text" id="room" name="roomChosen" roomthere="<?php echo $room_there; ?>" />
//message displayed if room number is in database or not
<br/><span id="roomAlert"></span></p>
// submit button
<p><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p>
最后,下面是点击后提交按钮的javascript处理程序:
function myClickHandler(){
if(validation()){
showConfirm(); // this is a function for a confirmation box
}
}
下面是关于我如何使用isset的答案之一的编辑:
if ($_POST['roomChosen'] [isset()]){
$roomresult = mysql_query( "SELECT Room FROM Room WHERE Room = " . (int) $_POST['roomChosen']);
if( mysql_num_rows( $roomresult ) == 0 ) $room_there = false;
}
答案 0 :(得分:0)
答案 1 :(得分:0)
首先添加isset()
检查$_POST['roomChosen']
。
if (isset($_POST['roomChosen']))
{
$roomresult = mysql_query( "SELECT Room FROM Room WHERE Room = " . (int) $_POST['roomChosen']);
}
else
{
$room_there = false;
}
答案 2 :(得分:0)
使用php获取数据库中的所有房间号码, 把它们放在一个JS数组中。 例如:
<?php
$query = ....
while($data = mysql_fetch_array($query))
$js_var = $data.",";
?>
<script>
var str = <?=$js_var?>;
var myRooms =new Array();
myRooms = str.split(",");
</script>
现在您可以使用js来检查数组中是否存在已发布的值。