使用javascript从php文件发送和接收信息时遇到问题
我正在向一个运行查询的php文件发送一些信息,但同时还想从该php文件中检索一些信息。php文件执行良好,但我无法获取json_编码的对象 向php文件发送字符串和数字的Javascript函数:使用javascript从php文件发送和接收信息时遇到问题,javascript,php,Javascript,Php,我正在向一个运行查询的php文件发送一些信息,但同时还想从该php文件中检索一些信息。php文件执行良好,但我无法获取json_编码的对象 向php文件发送字符串和数字的Javascript函数: function open_close(){ var status = encodeURIComponent(SelectedTicket["Status"]); var ticketNum = encodeURIComponent(SelectedTicket["TicketNum"
function open_close(){
var status = encodeURIComponent(SelectedTicket["Status"]);
var ticketNum = encodeURIComponent(SelectedTicket["TicketNum"]);
var info = "Status="+status+"&TicketNum="+ticketNum;
var http3 = createAjaxRequestObject();
if (http3.readyState == 4) {
if (http3.status == 200){
alert("Ticket Updated!"); //This never gets hit
getUpdatedTicket(JSON.parse(http3.responseText));
}
}
http3.open("POST", "openClose.php", true);
http3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http3.send(info);
}
获取字符串和数字并更新表的PHP文件
<?php
include("config.php");
session_start();
$status = $_POST["Status"];
$num = $_POST["TicketNum"];
$newStatus = " ";
if(strcmp($status, "Open") == 0){
$newStatus = "Closed";
}
elseif(strcmp($status, "Closed") == 0){
$newStatus = "Open";
}
$sql = "UPDATE tickets SET Status = \"$newStatus\" where TicketNum = $num ";
$r = $conn ->query($sql) or trigger_error($conn->error."[$sql]");
$sql = "SELECT * FROM tickets where TicketNum = $num";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
$data[] = $row;
}
echo json_encode($data);
?>
如何在同一javascript函数中检索json_编码的对象?您需要一个
readyState
侦听器来知道请求何时完成,然后从responseText
function open_close() {
var status = encodeURIComponent(SelectedTicket["Status"]);
var ticketNum = encodeURIComponent(SelectedTicket["TicketNum"]);
var info = "Status=" + status + "&TicketNum=" + ticketNum;
var http3 = createAjaxRequestObject();
http3.onreadystatechange = function () {
if (http3.readyState == 4) {
if (http3.status == 200) {
console.log(http3.responseText); // <- it's there
}
}
}
http3.open("POST", "openClose.php", true);
http3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http3.send(info);
}
函数打开\关闭(){
var status=encodeURIComponent(SelectedTicket[“status”]);
var ticketNum=encodeURIComponent(SelectedTicket[“ticketNum”]);
var info=“Status=“+Status+”&TicketNum=“+TicketNum;
var http3=createAjaxRequestObject();
http3.onreadystatechange=函数(){
如果(http3.readyState==4){
如果(http3.status==200){
console.log(http3.responseText);//服务器的响应在http3.responseText
中。关于AJAX的任何教程都应该解释这一点。