我尝试了一切,但我却迷失了方向
function submitItem(){
textarea = $("#textarea").val();
status = $("#status").val();
category = $("#categories").val();
date = $("#date").val();
var ajaxReq = new XMLHttpRequest();
ajaxReq.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("message").innerHTML = this.responseText;
}
}
ajaxReq.open("POST","../php/addItem.php",true);
ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajaxReq.send("textarea="+textarea+"&category="+category+"&status="+status+"&date="+date);
}
AJAX REQUEST
<?php
session_start();
require("server.php");
$cnx = new mysqli($server_name,$server_username,$server_password,$db);
$category = $_POST["category"];
$item = $_POST["textarea"];
$date = $_POST["date"];
$status = $_POST["status"];
$userID = $_SESSION["userID"];
$searchForCategory = "SELECT * FROM categories where categoryname = \"".$category."\"";
$result = $cnx->query($searchForStatus);
$row = $result->num_rows;
echo $row;
?>
我尝试了所有操作,当我输入实际名称时,一切正常。我回显了$ category,以查看它是否正常工作。我不知道错误可能是什么
最佳答案
文本区域可能包含一些特殊字符,您没有正确编码。使用$.post
,它将正确编码数据。
function submitItem() {
$.post("../php/addItem.php", {
textarea: $("#textarea").val(),
category: $("#categories").val(),
status: $("#status").val(),
date: $("#date").val()
}, function(response) {
$("#message").html(response);
});
}
如果出于某些原因您不想这样做,请在每个参数周围使用
encodeURIComponent()
函数,例如"textarea=" + encodeURIComponent(textarea) + "&category=" + encodeURIComponent(category) + ...
关于php - 尝试从mysql数据库中获取信息时,SQL语句无法在php中工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47361423/