How to call Ajax function recursively
I want to know how to call Ajax function Recursively. My ajax code is like this,
<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>
And php code is like this
<?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)
{
ca开发者_开发技巧se '1' :
writeName();
break;
case '2' :
ReadName();
break;
default :
echo "Empty String";
}
}
?>
first time this function is get called with value of step variable equal to 1 and Function Write name modified it as 2. Now i want to call this Ajax function with Step variable value equal to 2. So that 2nd php function gets called.
You can create a function that makes the Ajax call and accepts the step as an argument:
function getStep(step) {
$.ajax
(
{
url: 'test1.php',
data: {step: step},
success: function(output)
{
step=parseInt(output);
alert(step);
if (step < 2) {
getStep(step);
}
}
}
);
}
You need to use "json_encode()" function at the server-side PHP script instead of simple "echo". "eval()" method in the Javascript try to run returned text.
You should write in the writeName() function this code:
echo json_encode(2);
精彩评论