[英]How to get div content that was echoed by PHP
I need to get a value inside a div content. 我需要在div内容中获取一个值。 After a button click and doing stuff on the server side, my PHP function does: 单击按钮并在服务器端执行操作后,我的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);
因此,假设它为0
,则AJAX响应将为$("div#divResult").html(data);
where I put the 0 in the div divResult
. 我将0放在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. getDivResult
函数无法执行。 The 0 and 1 does display on in the div though. 0和1确实会在div中显示。 Any help on this? 有什么帮助吗? I've tried .html
too by the way. 我也尝试过.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: 这是我用于按钮单击并从PHP返回1或0的响应的AJAX:
$.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);
}
?>
place getDivResult() to onclick in which button you click like 将getDivResult()置于onclick所在的按钮上
< button onclick="getDivResult()">Click me< /button>" <button onclick =“ getDivResult()”>点击我</ button>“
i think it will be work with you. 我认为它将与您合作。
When you do .html(data)
all the existing elements wipedoff and replaced by new content: 当您执行.html(data)
所有现有元素都会被擦除并替换为新内容:
$("div#divResult").html(data);
I guess you should do this: 我想你应该这样做:
$("div#divResult").html(data);
getDivResult(); // call after it. and put the function globally.
enclose the echo with a div then trying getting the value by the id. 用div将回声括起来,然后尝试通过id获取值。
or 要么
try echoing via json enconde json_encode then fetch the value by using AJAX 尝试通过json enconde json_encode进行回显,然后使用AJAX获取值
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();
}
}
});
i think, this script <script>getDivResult();</script>
was replaced the content of #divResult
by ajax code $("div#divResult").html(data);
我认为,此脚本<script>getDivResult();</script>
被ajax代码$("div#divResult").html(data);
替换了#divResult
的内容$("div#divResult").html(data);
. 。 Instead of that, place the script inside head section rather than inside #divResult
to execute that. 取而代之的是,将脚本放置在head部分而不是#divResult
内部以执行该脚本。
Where is your ajax? 你的ajax在哪里? How do you do it? 你怎么做呢?
It looks like you're using jQuery. 看起来您正在使用jQuery。 Try reading the documentation https://api.jquery.com/jquery.get/ 尝试阅读文档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
Run your function 运行你的功能
getDivResult();
after 后
$("div#divResult").html(data);
in ajax
在ajax
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.