我有2个php文件,第一个是BAConsult.php
,它是主文件,另一个是BAConsultRecordsAJAX.php
。这行代码位于BAConsultRecordsAJAX.php
:
while($row = mysqli_fetch_array($consultresult)) {
$skincareinuse=explode(",",$row['skincarecurrentlyinuse']);
}
在BAConsult.php
我有这个:
echo $skincareinuse;
是否可以将$skincareinuse
页面的BAConsultRecordsAJAX.php
值转移到$skincareinuse
页面上的BAConsult.php
变量?
这样,假设每当我点击表格行时,$skincareinuse
的值将被更新并由回显显示,而不会刷新页面吗?
[已编辑以显示更多我的代码]
这是BAConsult.php文件,我的主要代码是。
<?php echo $jsonvariable; ?>//Lets say i want to store the JSON data into this variable
function showconsultationdata(str) { //face e.g and checkboxes for that date selected.
if (str == "") {
document.getElementById("txtHint2").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
var a = JSON.parse($(xmlhttp.responseText).filter('#arrayoutput').html());
$("textarea#skinconditionremarks").val(a.skinconditionremarks);
$("textarea#skincareremarks").val(a.skincareremarks);
var test = a.skincareinuse;
//I want to store this ^^ into the php variable of this page.
}
};
xmlhttp.open("GET","BAConsultRecordsAJAX.php?q="+str,true);
xmlhttp.send();
}
}
这是我的BAConsultRecordsAJAX.php页面。
$q = $_GET['q']; //get dateconsulted value
$consult="SELECT * FROM Counsel where nric='$_SESSION[nric]' and dateconsulted='$q'";
$consultresult = mysqli_query($dbconn,$consult);
while($row = mysqli_fetch_array($consultresult)) {
$skincareinuse=explode(",",$row['skincarecurrentlyinuse']);
$skincondition=explode(",",$row['skincondition']);
$queryResult[] = $row['skincareremarks'];
$queryResult[] = $row['skinconditionremarks'];
}
$skincareremarks = $queryResult[0];
$skinconditionremarks = $queryResult[1];
echo "<div id='arrayoutput'>";
echo json_encode(array('skincareremarks'=>$skincareremarks
'skinconditionremarks'=>$skinconditionremarks
'skincareinuse'=>$skincareinuse,
'skincondition'=>$skincondition));
echo "</div>";
答案 0 :(得分:2)
您实际上并不想将变量从BAConsultRecordsAJAX.php
转移到BAConsult.php
,而是转移到BAConsult.js
(我想这是名称你的JS文件)。
实际上,即使这就是你想做的事情,也是不可能的,因为你的主要PHP在页面加载之前就被处理了。但是,你可以做的是使用JavaScript覆盖渲染值。
要做到这一点,发送一个AJAX请求到BAConsultRecordsAJAX.php
请求变量的值,如下所示:
在JavaScript中:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
// Check if the AJAX request was successful
if (xhttp.readyState === 4 && xhttp.status === 200) {
var td = document.getElementById("your table td value's id");
var td.innerHTML = xhttp.responseText; // gets the value echoed in the PHP file
}
xhttp.open("POST", "BAConsultRecordsAJAX.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(""); // You don't need to send anything to the PHP file
在PHP中:
<?php
echo $skincareinuse;
?>
<强> [编辑] 强>:
如果您使用的是内联JavaScript,请使用以下代码:
<script type = "application/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
// Check if the AJAX request was successful
if (xhttp.readyState === 4 && xhttp.status === 200) {
var td = document.getElementById("your table td value's id");
td.innerHTML = xhttp.responseText; // gets the value echoed in the PHP file
}
xhttp.open("POST", "BAConsultRecordsAJAX.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(""); // You don't need to send anything to the PHP file
</script>
[编辑2] :
要通过AJAX请求将JSON对象传递给PHP文件,您必须将其作为字符串。这需要以下代码:
var JSONstring = JSON.stringify(yourJSONobject);
然后你可以将它放在你的PHP文件中,并将它放在send()
内,如下所示:
xhttp.send("json_object=" + JSONstring);
然后在PHP中,为了使用它,你必须解码它:
<?php
$json_object = json_decode($_POST["json_object"]);
// Now it's ready to use
?>
关于第一个文件的说明:
首先,您已将纯JavaScript代码放在PHP文件中,因此无效。 您必须将其包裹在<script type = "application/javascript></script>
我不知道你在这里想做什么:
代码:
var a = JSON.parse($(xmlhttp.responseText).filter('#arrayoutput').html());
innerHTML
的{{1}}。我将如何编写代码 :
#arrayoutput
关于第二个文件的说明:
function showconsultationdata(str) {
var xmlhttp;
if (!str) {
$("#txtHint2").empty();
} else {
xmlhttp = new XMLHttpRequest();
// Providing support for a 15-year-old browser in 2016 is unnecessary
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
$("#txtHint2").html(xmlhttp.responseText);
var a = JSON.parse(xmlhttp.responseText);
$("textarea#skinconditionremarks").val(a.skinconditionremarks);
$("textarea#skincareremarks").val(a.skincareremarks);
var test = a.skincareinuse; // The variable you want
// Its saved in "text" and can use something like:
// $("#element").html(test); to have it replace your current text
}
};
xmlhttp.open("GET","BAConsultRecordsAJAX.php?q="+str,true);
xmlhttp.send();
}
}
是一个关联数组,您传递$_SESSION
,而它应该是nric
。"nric"
是一个数组,因此将其值插入查询的正确方法是:$_SESSION
。{$_SESSION["nric"]}
相对容易入侵。稍后查看改进的代码,了解如何执行此操作。我将如何编写代码 :
GET
答案 1 :(得分:0)
使用include
很可能会有效。来自docs:
当包含文件时,它包含的代码将继承发生包含的行的变量范围。从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。
<强> BAConsultRecordsAJAX.php 强>
while($row = mysqli_fetch_array($consultresult)) {
$skincareinuse[] = explode(",",$row['skincarecurrentlyinuse']);
}
include "BAConsult.php";
答案 2 :(得分:0)
显然,您可以更改值以满足您的要求,您必须安装jquery,时间值可以是您想要的任何内容
$.ajaxSetup({ cache: false });
setInterval(function() {
var url = "pagethatrequiresrefreshing.php";
$('#divtorefresh').load(url);
}, 4000);