如何使用javascript根据房间可用性进行提示?
我是编程新手,我需要有关我的代码的帮助。我希望我的页面能够提示我是否还有可用房间。我正在使用管理页面上的 onload 功能。
到目前为止,这是我的代码
function prompt()
{
< ?php
include("dbconfig.php");
$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
?>
if(< ?php $result <= 14 ?>){
alert("Rooms left: < ?php echo $result ?>");
}
else{
alert("Welcome Admin.");
}
}
window.onload=prompt;
编辑:
代码现在工作正常,但它显示“资源 id#4”,而不是计数的值。
I'm new to programming and I need help on my code. I want my page to prompt me if there will be available rooms left. I'm using the onload function on the admin page.
so far here is my code
function prompt()
{
< ?php
include("dbconfig.php");
$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
?>
if(< ?php $result <= 14 ?>){
alert("Rooms left: < ?php echo $result ?>");
}
else{
alert("Welcome Admin.");
}
}
window.onload=prompt;
edit:
The code worked fine now but it displays "Resource id#4", not the value of the count.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我觉得你不能将 php 与 js 代码混合在一起。
php主要是服务端,js是客户端
从您提供的代码片段来看,也许您应该使用纯粹的 php,如下所示:
这应该在请求时首先运行
I feel you can't mix php with js codes.
php is mainly on server side , while the js is client side
from the snippet you provide, maybe you should use purely php as follows:
This should be run at the first when request
我认为您对 PHP 处理位置与 Javascript 处理位置感到困惑。
PHP 在服务器端处理,而Javascript 在客户端处理。可以这样想……
正如你现在所拥有的,你会得到一些有趣的输出......特别是因为你缺乏 echo 语句。以下是您可能在浏览器页面源代码中看到的内容:
注意空的 if 语句(还有开始标记中的空格:
这应该使您的 Javascript 评估布尔值 true/false。不要忘记 Javascript 需要用 < 包裹起来。脚本>也有标签!
回答您的 MySQL 问题...
试试这样:
I think you are confused about where PHP processes vs. where Javascript processes.
PHP is processed on the server side, while Javascript is processed on the client side. Think of it like this...
As you have it now, you'd be getting some funny output... especially because of your lack of echo statements. Here is what you'd probably be seeing in your browser page source:
Notice the empty if statement (also the space in the start tags:
This should make your Javascript evaluate a boolean true/false. Don't forget that Javascript needs to be wrapped in a < script > tag too!
To answer your MySQL question...
Try it like this:
mysql_query
返回资源,而不是结果。尝试使用:
mysql_query
returns resource, not a result.Try to use:
php 标签中不应有空格:
应该是:
您还缺少 fetching 函数,以下是如何获取变量中的行数:
稍后您可以使用
$count<
if
条件中的 /code> 变量。There should be no space in php tags:
Should be:
You are also missing a fetching function, here is how you can get row count in a variable:
Later you can use the
$count
variable in theif
condition.使用 mysql_fetch_row ,然后在条件中进行比较到$row[0]
use mysql_fetch_row , and after that in the condition , compare to $row[0]