我有下面的ajax函数,它是从一个表单按钮调用的,该按钮获取下面php循环中使用的数字。php文件加载,但代码在"需要带红色星号*的字段"之后停止工作任何帮助都将是伟大的!
function loadMulti() {
var num_to_enter = $('#num_to_enter').val();
$.ajax({
type: "POST",
url: "myphpfile.php",
data: "num_to_enter=" + num_to_enter,
success: function(){
$("#multi").load('myphpfile.php')
}
});
return false;
}
和php:
<?php
$num_to_enter = $_POST["num_to_enter"];
echo $num_to_enter;
$i=1;
?>
<form class="my_form" name="addReg" id="addReg" method="post" />
<span class="red">Fields With Red Asterisks * Are Required</span>
<?php
while($i <= $num_to_enter){
?>
The html form here repeated by $num_to_enter
<?php
$i++;
}
?>
对于初学者来说,您可以稍微清理一下代码。看看这是否有助于(测试和工作)
JS文件
function loadMulti ()
{
var num_to_enter = 2;//$('#num_to_enter').val();
$.ajax({
type: "POST",
url: "temp.php",
data: "num_to_enter=" + num_to_enter,
}).done (function (data){ //success is deprecated
$("#test").html (data);
});
return false;
}
$(document).ready (function (){
loadMulti ();
});
或者你想要一个js帖子??
function loadMulti ()
{
var num_to_enter = 2;//$('#num_to_enter').val();
$ ("#check").on ("click", function (){
$.ajax({
type: "POST",
url: "temp.php",
data: "num_to_enter=" + num_to_enter,
}).done (function (data){ //success is deprecated
$("#test").html (data);
});
});
return false;
}
$(document).ready (function (){
loadMulti ();
});
PHP文件
<?php
$num_to_enter = $_POST["num_to_enter"];
$string = "";
echo $num_to_enter;
$i=1;
while ($i <= $num_to_enter)
{
$string .= "The html form here repeated by {$num_to_enter}<br/>";
$i++;
}
?>
<span class="red">Fields With Red Asterisks * Are Required</span>
<?php echo $string; ?>
进行调用的PHP文件。
<!doctype html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src='test.js'></script>
</head>
<body>
<div id="test">test</div>
</body>
</html>
或使用后
<!doctype html>
<html>
<head>
<title></title>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script src='test.js'></script>
</head>
<body>
<div id="test">results will show here.</div>
<form class="my_form" name="addReg" id="addReg" method="post" />
<input id="check" type="button" name="sendpost" value="Get Data">
</form>
</body>
</html>
EDIT:添加了进行调用的php文件,我将.load()更改为.html()其备受尊敬的选择器。
此外,我不确定您是否希望消息打印多次,所以如果您需要以这种方式打印,只需将$string更改为数组即可。
您的PHP脚本"运行良好",但由于从未更新$i
,您最终会陷入一个没有结束的while循环。您需要在while
循环中递增它:
<?php
$num_to_enter = $_POST["num_to_enter"];
echo $num_to_enter;
$i = 1;
?>
<form class="my_form" name="addReg" id="addReg" method="post" />
<span class="red">Fields With Red Asterisks * Are Required</span>
<?php
while ($i <= $num_to_enter) {
?>The html form here repeated by $num_to_enter <?php
// You need to increment this so the while loop stops when $i hits
// the same amount as $num_to_enter.
$i++;
}
?>
更改此项:
data: "num_to_enter=" + num_to_enter,
到此:
data: {num_to_enter: num_to_enter},
$.ajax()
需要一个对象,而不是字符串。文档确实说它可以接受一个字符串,但在这种情况下你必须自己序列化它;让jQuery来处理它会更容易。
关于PHP:请确保在while循环中增加$i
,否则它将永远不会结束,正如@putvande所指出的,并确保包含一个结束的</form>
标记。
最后,将this:$num_to_enter = $_POST["num_to_enter"];
更改为this:$num_to_enter = intval($_POST["num_to_enter"]);
,以强制PHP将其视为整数。