I need to get a value inside a div content. After a button click and doing stuff on the server side, my PHP function does:
我需要在div内容中获取一个值。在按钮单击并在服务器端执行操作后,我的PHP函数执行:
echo "0";
or
要么
echo "1";
depending on what my function does. So let's say if it's 0
, the AJAX response will be $("div#divResult").html(data);
where I put the 0 in the div divResult
.
取决于我的功能。所以,假设它为0,则AJAX响应为$(“div#divResult”)。html(data);我把div放在div divResult中。
What I am trying to do now is I want to execute a js function to read whether it's 0
or 1
in divResult
.
我现在要做的是我想执行一个js函数来读取divResult中的0或1。
This is how I execute it:
这是我执行它的方式:
<div id="divResult"><script>getDivResult();</script></div>
And my js function:
我的js功能:
function getDivResult()
{
var result = $("div#divResult").text();
if(result === "0")
{
alert("Badge Number already exists, please check again.");
}
else if(result === "1")
{
alert("Your details have been entered!")
ADD_USER_POPUP.close;
}
}
Somehow the getDivResult
function is not executing. The 0 and 1 does display on in the div though. Any help on this? I've tried .html
too by the way.
不知怎的,getDivResult函数没有执行。然而,0和1显示在div中。对此有何帮助?顺便说一下,我也试过.html。
EDIT:
编辑:
Here's the AJAX that I use for the button click and return the response from PHP which is either 1 or 0:
这是我用于按钮单击的AJAX,并返回PHP的响应,即1或0:
$.post(page, {
name : name,
badge_number : badge_number,
category : category,
priviledge : priviledge,
action : "insert"
}, function(data) {
$("div#divResult").html(data);
});
2nd EDIT:
第二次编辑:
function insertRow($name, $badge_number, $priviledge, $category)
{
$table_info = "TBL_USER_LOGIN";
$query_string = "select badge_number from $table_info where badge_number = $badge_number";
$result = @mysql_query($query_string) or die (mysql_error());
$checkBadge = mysql_num_rows($result);
if($checkBadge>0)
{
//echo "Badge Number $badge_number already exists. Please check again.";
echo "0";
}
else
{
$query_string = "insert into $table_info(name, badge_number, priviledge, category) values('$name', '$badge_number', '$priviledge', '$category')";
$result = @mysql_query($query_string) or die (mysql_error());
//echo "Your details have been entered! Please click on 'View Users' to display all users.";
echo "1";
}
?>
<?php
$action = rtrim($_REQUEST['action']);
if($action=="delete")
{
$id = rtrim($_REQUEST['id']);
$order = $_REQUEST['order'];
echo deleteRow($id);
echo selectAll($order);
}
elseif($action=="insert")
{
$name = $_REQUEST['name'];
$badge_number = $_REQUEST['badge_number'];
$priviledge = $_REQUEST['priviledge'];
$category = $_REQUEST['category'];
echo insertRow($name, $badge_number, $priviledge, $category);
}
elseif($action=="update")
{
$order = $_REQUEST['order'];
echo selectAll($order);
}
?>
7 个解决方案
#1
0
You shouldn't need to append the return data to the page at all. Why don't you run your function immediately after the AJAX request completes, like so:
您根本不需要将返回数据附加到页面。为什么不在AJAX请求完成后立即运行您的函数,如下所示:
$.ajax({
success: function(data) {
if(data === "0") {
alert("Badge Number already exists, please check again.");
}
else if(data === "1") {
alert("Your details have been entered!")
ADD_USER_POPUP.close();
}
}
});
#2
0
place getDivResult() to onclick in which button you click like
将getDivResult()放在onclick上,点击你点击的按钮
< button onclick="getDivResult()">Click me< /button>"
i think it will be work with you.
我认为这将与你合作。
#3
0
enclose the echo with a div then trying getting the value by the id.
用div包围echo,然后尝试通过id获取值。
or
要么
try echoing via json enconde json_encode then fetch the value by using AJAX
尝试通过json enconde json_encode回显,然后使用AJAX获取值
#4
0
i think, this script <script>getDivResult();</script>
was replaced the content of #divResult
by ajax code $("div#divResult").html(data);
. Instead of that, place the script inside head section rather than inside #divResult
to execute that.
我认为,这个脚本
#5
0
Where is your ajax? How do you do it?
你的阿贾克斯在哪里?你怎么做呢?
It looks like you're using jQuery. Try reading the documentation https://api.jquery.com/jquery.get/
看起来你正在使用jQuery。请尝试阅读文档https://api.jquery.com/jquery.get/
You can try something like this:
你可以尝试这样的事情:
$.get( "ajax/test.html", function( data ) {
if(data === "0")
{
alert("Badge Number already exists, please check again.");
}
else if(data === "1")
{
alert("Your details have been entered!")
ADD_USER_POPUP.close;
}
});
data should be your 0 or 1
数据应为0或1
#6
0
When you do .html(data)
all the existing elements wipedoff and replaced by new content:
当您执行.html(数据)时,所有现有元素都被擦除并被新内容替换:
$("div#divResult").html(data);
I guess you should do this:
我想你应该这样做:
$("div#divResult").html(data);
getDivResult(); // call after it. and put the function globally.
#7
0
Run your function
运行你的功能
getDivResult();
after
后
$("div#divResult").html(data);
in ajax
在ajax
#1
0
You shouldn't need to append the return data to the page at all. Why don't you run your function immediately after the AJAX request completes, like so:
您根本不需要将返回数据附加到页面。为什么不在AJAX请求完成后立即运行您的函数,如下所示:
$.ajax({
success: function(data) {
if(data === "0") {
alert("Badge Number already exists, please check again.");
}
else if(data === "1") {
alert("Your details have been entered!")
ADD_USER_POPUP.close();
}
}
});
#2
0
place getDivResult() to onclick in which button you click like
将getDivResult()放在onclick上,点击你点击的按钮
< button onclick="getDivResult()">Click me< /button>"
i think it will be work with you.
我认为这将与你合作。
#3
0
enclose the echo with a div then trying getting the value by the id.
用div包围echo,然后尝试通过id获取值。
or
要么
try echoing via json enconde json_encode then fetch the value by using AJAX
尝试通过json enconde json_encode回显,然后使用AJAX获取值
#4
0
i think, this script <script>getDivResult();</script>
was replaced the content of #divResult
by ajax code $("div#divResult").html(data);
. Instead of that, place the script inside head section rather than inside #divResult
to execute that.
我认为,这个脚本
#5
0
Where is your ajax? How do you do it?
你的阿贾克斯在哪里?你怎么做呢?
It looks like you're using jQuery. Try reading the documentation https://api.jquery.com/jquery.get/
看起来你正在使用jQuery。请尝试阅读文档https://api.jquery.com/jquery.get/
You can try something like this:
你可以尝试这样的事情:
$.get( "ajax/test.html", function( data ) {
if(data === "0")
{
alert("Badge Number already exists, please check again.");
}
else if(data === "1")
{
alert("Your details have been entered!")
ADD_USER_POPUP.close;
}
});
data should be your 0 or 1
数据应为0或1
#6
0
When you do .html(data)
all the existing elements wipedoff and replaced by new content:
当您执行.html(数据)时,所有现有元素都被擦除并被新内容替换:
$("div#divResult").html(data);
I guess you should do this:
我想你应该这样做:
$("div#divResult").html(data);
getDivResult(); // call after it. and put the function globally.
#7
0
Run your function
运行你的功能
getDivResult();
after
后
$("div#divResult").html(data);
in ajax
在ajax