Javascript 变量数目未知的AJAX post
我的网站上有一张表格。问题存储在数据库中,一些变量决定您看到的问题。因此,问题的数量是未知的。问题代码如下所示: HTMLJavascript 变量数目未知的AJAX post,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我的网站上有一张表格。问题存储在数据库中,一些变量决定您看到的问题。因此,问题的数量是未知的。问题代码如下所示: HTML <ol class="questions"> <?php $number = 1; while($row = mysql_fetch_assoc($result)) { echo '<li>'; echo '<span><label for
<ol class="questions">
<?php
$number = 1;
while($row = mysql_fetch_assoc($result)) {
echo '<li>';
echo '<span><label for="q'.$number.'">'.$row['question'].'</label></span>';
echo '<input id="q'.$number.'" name="q'.$number.'" type="text" value="'.$row['description'].'" onclick="this.value="";this.onclick="test";this.style.color="#000000"; setAttribute("type", "text");" onfocus="if (this.value == "'.$row['description'].'") {this.value = ""; setAttribute("type", "text");}" onblur="if (this.value == "") {this.value = "'.$row['description'].'";setAttribute("type", "text");}"/>';
echo '</li>';
$number++;
};?>
</ol>
您可以像这样通过ajax提交表单
$("#myForm").submit(function() {
$.post('post.php',$(this).serialize(), function(data) {
alert(data);
});
return false;
});
正如前一位作者所说,使用serialize()
函数。他没有提到什么,我现在要澄清一下
JavaScript代码:
$("#button").on("click", function() {
$.ajax({
url: "someurl.php",
method: "post",
data: $("#myform").serialize()
});
});
HTML代码必须如此:
<form id="myForm">
<input type='text' name='field1' />
<input type='text' name='field2' />
<input type='text' name='etc' />
</form>
<a href='#' id="button">Send ajax</a>
现在,当您单击发送ajax
按钮时,表单将被序列化(这意味着创建字符串,其中将包含有效的url查询,因此可以随请求一起发送)。请在此处阅读:
希望这会有所帮助
询问我是否有不清楚的地方。ajax代码在哪里?谢谢!它成功了,第二个问题是;我如何在PHP中捕捉它?@user2755352您创建了新问题还是什么?还是你现在问我?我现在问你是否也知道第二个问题的答案;B最好提出一个新问题:)我会回答的,当然。但这里面没什么难事。Ajax请求是一个简单的请求(但是是的,您可以在php中检测到它并做一些技巧,但是……相同)。所以您必须将ajax请求作为简单的请求来处理。