Php 如何递归调用Ajax函数
我想知道如何递归调用Ajax函数。 我的ajax代码是这样的Php 如何递归调用Ajax函数,php,javascript,ajax,Php,Javascript,Ajax,我想知道如何递归调用Ajax函数。 我的ajax代码是这样的 <html> <head> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> var step='1'; $.ajax ( { url: 'test1.php', data: {step: step},
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var step='1';
$.ajax
(
{
url: 'test1.php',
data: {step: step},
success: function(output)
{
step=eval(output);
alert(step);
}
}
);
</script>
</head>
</html>
var步骤='1';
$.ajax
(
{
url:'test1.php',
数据:{step:step},
成功:功能(输出)
{
步骤=评估(输出);
警报(步骤);
}
}
);
php代码是这样的
<?php
function writeName()
{
echo "step=2";
}
function ReadName()
{
echo "In Read Name Function";
}
if(isset($_REQUEST['step']) && !empty($_REQUEST['step']))
{
$step = $_REQUEST['step'];
switch($step)
{
case '1' :
writeName();
break;
case '2' :
ReadName();
break;
default :
echo "Empty String";
}
}
?>
第一次调用此函数时,step变量的值等于1,函数Write name将其修改为2。现在我想用Step变量值等于2来调用这个Ajax函数。这样就可以调用第二个php函数。您可以创建一个函数来进行Ajax调用,并接受步骤作为参数:
function getStep(step) {
$.ajax
(
{
url: 'test1.php',
data: {step: step},
success: function(output)
{
step=parseInt(output);
alert(step);
if (step < 2) {
getStep(step);
}
}
}
);
}
函数getStep(步骤){
$.ajax
(
{
url:'test1.php',
数据:{step:step},
成功:功能(输出)
{
步骤=parseInt(输出);
警报(步骤);
如果(步骤<2){
获取步骤(步骤);
}
}
}
);
}
您可以创建一个函数来进行Ajax调用,并接受步骤作为参数:
function getStep(step) {
$.ajax
(
{
url: 'test1.php',
data: {step: step},
success: function(output)
{
step=parseInt(output);
alert(step);
if (step < 2) {
getStep(step);
}
}
}
);
}
函数getStep(步骤){
$.ajax
(
{
url:'test1.php',
数据:{step:step},
成功:功能(输出)
{
步骤=parseInt(输出);
警报(步骤);
如果(步骤<2){
获取步骤(步骤);
}
}
}
);
}
您需要在服务器端PHP脚本中使用“json_encode()”函数,而不是简单的“echo”。
Javascript中的“eval()”方法尝试运行返回的文本
您应该在writeName()函数中编写以下代码:
echo json_encode(2);
您需要在服务器端PHP脚本中使用“json_encode()”函数,而不是简单的“echo”。
Javascript中的“eval()”方法尝试运行返回的文本
您应该在writeName()函数中编写以下代码:
echo json_encode(2);
旁白:如果您已经检查了
empty()
,则可以省略isset()
。旁白:如果您已经检查了empty()
@Sergey Kuznetsov:Thanx,则可以省略isset()
。我开始了解一个新概念。@Sergey Kuznetsov:Thanx。我开始了解一个新概念。