Yinelemeli Ajax işlevi nasıl

2 Cevap php

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>

Ve php kodu bu gibi

<?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";   
  }
 }
?>

İlk kez bu işlev adımı değişken 1'e eşit ve Fonksiyon Yaz adının değeri ile çağrılıp olduğu 2 olarak değiştirilmiş. Şimdi ben 2 eşittir Adım değişken değeri ile bu Ajax işlevini çağırmak istiyorum. 2 php fonksiyonu çağrılır böylece.

2 Cevap

Sen Ajax çağrı yapar ve bir argüman olarak adımı kabul eden bir işlevi oluşturabilirsiniz:

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.

Sen writeName () fonksiyonu bu kod yazmak gerekir:

echo json_encode(2);