如何将变量从jquery传递到php
我搜索了又搜索,似乎无法从现有的帖子中找到答案。我有一些特定的代码,我不知道如何操作它来获得我需要的。 //======================== 以下是HTML:如何将变量从jquery传递到php,php,jquery,mysql,ajax,post,Php,Jquery,Mysql,Ajax,Post,我搜索了又搜索,似乎无法从现有的帖子中找到答案。我有一些特定的代码,我不知道如何操作它来获得我需要的。 //======================== 以下是HTML: $q_id = get_question_id($holder_id, $pointer); echo "HERE: ". $q_id; // This line works! I get an id number here. ?> <html> <
$q_id = get_question_id($holder_id, $pointer);
echo "HERE: ". $q_id; // This line works! I get an id number here.
?>
<html>
<head><title>jQuery + JSON + PHP + AJAX</title></head>
<body>
<div class="container">
<div class="col-md-6">
<form id="myForm" class="form-signin" action="react.php" method="post">
<input type='hidden' name='holder_id' value='<?php echo $_POST['holder_id']; ?>'/>
<input type='hidden' name='pointer' value='<?php echo $_POST['pointer']; ?>'/>
<input type='hidden' name='q_id' value='<?php echo $q_id; ?>'/>
<h3>Question</h3>
<ul></ul>
<button id="sub" class="btn btn-lg btn-primary btn-block" type="submit">Create</button>
</form>
<h3 id="result"></h3>
$q\u id=get\u question\u id($holder\u id,$pointer);
回声“这里:”$q_id;//这条线行得通!我这里有身份证号码。
?>
jQuery+JSON+PHP+AJAX
我对学习这类东西还不熟悉,在“fetch.php”文件中的sql语句之前提取$_POST['id']时遇到了问题。通常,我可以将print_r($_POST)放在任何php页面的顶部附近,并查看列出的所有变量。在这种情况下,我没有看到任何变量到达php文件。如果我将print_r($_POST)放在html上,我会从上一页获取值,但是fetch.php文件似乎没有获取任何POST变量
我想我的困惑的答案可能很简单,但我需要帮助,阅读所有这些非常相似但与我的问题不太相似的帖子并不会让我走得很远。事实上,我已经走到了这一步,知道的不多,只是通过阅读帖子…所以我正在尝试,我希望没有一篇帖子是我在发布之前没有看到或学习过的。谢谢 您没有将id变量发送到
fetch.php
文件。您需要查看$.post()
而不是$.getJSON
。并使用准备好的语句,现在您可以将标题设置为Mysql注入测试工具
,因为Xofifelse会提出一些问题来避免注入。在这里检查一些例子。或者,您可以这样做,$.getJSON(“fetch.php?id=”,function(data){
并将$\u POST
更改为$\u GET
。但要验证该id,请使用is\u numeric()确保它只是一个整数。
function reads() {
$.getJSON("fetch.php", function(data) {
$("ul").empty();
$.each(data.result, function(){
var output = $("").val();
output = "<li><h4>"+ this['id'] +": " + this['question'] +"</h4></li>";
output += "<div class=\"checkbox\">";
output += "<label><input name=\"a\" type=\"checkbox\">"+ this['a'] +"</label><br/>";
output += "<label><input name=\"b\" type=\"checkbox\">"+ this['b'] +"</label><br/>";
output += "<label><input name=\"c\" type=\"checkbox\">"+ this['c'] +"</label><br/>";
output += "<label><input name=\"d\" type=\"checkbox\">"+ this['d'] +"</label><br/>";
output += "</div>";
$("ul").append(output);
});
});
}
<?php
include_once('db.php');
$q_id = $_POST['id'];
$sql = "SELECT * FROM questions WHERE id = {$q_id} LIMIT 1";
$res = mysqli_query($connection, $sql);
$result = array();
while( $row = mysqli_fetch_array($res) )
array_push($result, array( 'id' => $row['id'],
'question' => $row['question'],
'a' => $row['a'],
'b' => $row['b'],
'c' => $row['c'],
'd' => $row['d']
));
echo json_encode(array("result" => $result));
?>