Javascript $skincareinuse=分解(“,”,$row['skincarecurrentlyinuse']); $skincondition=分解(“,”,$row['skincondition']); $queryResult[]=$row['SkinCareComments']; $queryResult[]=$row['SkinConditionComments']; } $SkinCareComments=$queryResult[0]; $SkinConditionComments=$queryResult[1]; $array=array('SkinCareMarkets'=>$SkinCareMarkets 'SkinConditionComments'=>$SkinConditionComments “skincareinuse”=>$skincareinuse, “skincondition”=>$skincondition); 回声“; echo json_编码($array); 回声“; mysqli_stmt_close($stmt); mysqli_close($dbconn); 出口 } } }

Javascript $skincareinuse=分解(“,”,$row['skincarecurrentlyinuse']); $skincondition=分解(“,”,$row['skincondition']); $queryResult[]=$row['SkinCareComments']; $queryResult[]=$row['SkinConditionComments']; } $SkinCareComments=$queryResult[0]; $SkinConditionComments=$queryResult[1]; $array=array('SkinCareMarkets'=>$SkinCareMarkets 'SkinConditionComments'=>$SkinConditionComments “skincareinuse”=>$skincareinuse, “skincondition”=>$skincondition); 回声“; echo json_编码($array); 回声“; mysqli_stmt_close($stmt); mysqli_close($dbconn); 出口 } } },javascript,php,json,ajax,Javascript,Php,Json,Ajax,显然,您可以更改值以满足您的需求,您必须安装jquery,时间值可以是您想要的任何值 $.ajaxSetup({ cache: false }); setInterval(function() { var url = "pagethatrequiresrefreshing.php"; $('#divtorefresh').load(url); }, 4000); 一旦页面加载完毕,php部分就结束了,因此您不能将ajax页面中的值带到当前页面并将其分配给当前页面上的任何php变量。您

显然,您可以更改值以满足您的需求,您必须安装jquery,时间值可以是您想要的任何值

$.ajaxSetup({ cache: false }); 
setInterval(function() { 

var url = "pagethatrequiresrefreshing.php";
$('#divtorefresh').load(url);
 }, 4000); 

一旦页面加载完毕,php部分就结束了,因此您不能将ajax页面中的值带到当前页面并将其分配给当前页面上的任何php变量。您只能使用从ajax页面返回的数据/html更新当前文档视图和/或分配/更新持久值(cookie或会话)。我实际上想将其传输到BAConsult.php文件,而不是js文件。它是一个php文件,但也包含Java脚本。是否可以同时运行2 xhttp.open?例如,xhttp.open(“POST”,“BAConsultRecordsAJAX.php”,true);setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);和xmlhttp.open(“GET”,“BAConsultRecordsAJAX.php?q=“+str,true”);如果在主PHP中使用内联JavaScript(而不是外部文件),只需包装我在
中提供的JavaScript代码即可。关于您的问题,不可以。您不能同时执行两个AJAX请求。第二个将等待第一个完成,然后它将开始,但在您的情况下,没有理由使用两个AJAX请求。嗯,我没有在这里发布完整的代码,但实际上,当我生成表时,我已经在使用AJAX脚本,但看起来我无法同时执行两个xhttp.open。。但是,谢谢你,你知道如何在javascript中使用JSON解析值并将其存储到php变量中吗?我的xhttp.open必须是“POST”吗?对不起,我对使用AJAX还很陌生。因为现在我的$json_对象总是未定义的。我当前的脚本执行xhttp.open,但不一定使用“GET”。我只使用
POST
,因为它更健壮,可以传输更多数据。只有当我想有这样的链接时,我才使用
GET
www.example.com/?ref=123
。未定义的$json_对象与发送请求的方法无关,除非该对象大于
GET
可以处理的范围。
echo $skincareinuse;
<?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();        
    }  
}
$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>";
while($row = mysqli_fetch_array($consultresult)) {
    $skincareinuse[] = explode(",",$row['skincarecurrentlyinuse']);
}

include "BAConsult.php";
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
     echo $skincareinuse;
?>
<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>
var JSONstring = JSON.stringify(yourJSONobject);
xhttp.send("json_object=" + JSONstring);
<?php
    $json_object = json_decode($_POST["json_object"]);
    // Now it's ready to use
?>
var a = JSON.parse($(xmlhttp.responseText).filter('#arrayoutput').html());
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();        
    }
}
$q = $_GET['q']; //get dateconsulted value

$dbconn = mysqli_connect("localhost", "root", "") or die("Error!");
mysqli_select_db($dbconn, "database") or die("Error!");

$consult = "SELECT * FROM Counsel where nric='{$_SESSION["nric"]}' and dateconsulted = ?";

if ($stmt = mysqli_prepare($dbconn, $consult)) { // By using a prepared statement
    mysqli_stmt_bind_param($stmt, "s", $q);      // you eliminate the chances to have
    if (mysqli_stmt_execute($stmt)) {            // an SQL Injection break your database
        $consultresult = mysqli_stmt_get_result($stmt);            
        if (mysqli_num_rows($consultresult) > 0) {
            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];
            $array = array('skincareremarks'=>$skincareremarks
                           'skinconditionremarks'=>$skinconditionremarks
                           'skincareinuse'=>$skincareinuse,
                           'skincondition'=>$skincondition);
            echo "<div id='arrayoutput'>";
            echo json_encode($array);
            echo "</div>";
            mysqli_stmt_close($stmt);
            mysqli_close($dbconn);
            exit;
        }
    }
}
$.ajaxSetup({ cache: false }); 
setInterval(function() { 

var url = "pagethatrequiresrefreshing.php";
$('#divtorefresh').load(url);
 }, 4000);