2014-08-28 50 views
0

我有一個Ajax/PHP代碼,我想檢查返回的文本是真還是假:檢查返回的文本是真的還是假

$(".gets").click(function() { 

    // some details 

    $.ajax({ 
     type: "POST", 
     url: "step2.php", 
     data: dataString, 
     success: function(data) { 

      // This is the button to hide 
      $('.nums').fadeOut('fast'); 

     } // end function 
    }); // end ajax 

    return false; 
}); 

step2.php

if($num < 3) { 
echo 'please choose at least 3 nums<br />'; 
$hideButton = false; // hide show nums button 
} else {   
$hideButton = true; 
} 

在此先感謝

回答

0

您必須回顯$hideButton。這些信息將可再data

if($num < 3) { 
echo 'please choose at least 3 nums<br />'; 
    $hideButton = false; // hide show nums button 
} else {   
    $hideButton = true; 
} 
echo $hideButton; 

然後 - 改變你的腳本像下面

$.ajax({ 
    type: "POST", 
    url: "step2.php", 
    data: dataString, 
    success: function(data) { // data now has the value of $hideButton 
     if(data == 'true') { 
      // This is the button to hide 
      $('.nums').fadeOut('fast'); 
     } 
    } // end function 
}); // end ajax   
+0

但警報(數據);返回第2步的完整代碼 – 2014-08-28 17:25:36

+0

然後它聽起來像PHP沒有運行。你在網絡服務器上做了這個嗎?還是你在本地加載文件? – 2014-08-28 17:27:19

+0

PHP沒有運行!!!!!怎麼來的???我沒有使用json,我使用的是PHP/Ajax,所以如果我打電話給step2.php,我會收到step2.php中的完整代碼(使用alert和console) – 2014-08-28 17:29:46

0
$(".gets").click(function() { 

// some details 

$.ajax({ 
type: "POST", 
url: "step2.php", 
data: dataString, 
success: function(data) { 
       if(data == 'true') 
        {// your if code 
         } 
      else 
        {// your else code 
        } 

    // This is the button to hide 
    $('.nums').fadeOut('fast'); 
} // end function 
    }); // end ajax   
return false; 
}); 
0

嘗試。

AJAX success

success: function(data) { 
    if(data == 'false'){ 
     $('.nums').fadeOut('fast'); 
    }else{ 
     // other event if you want 
    } 

step2.php

echo ($num < 3) ? 'false' : 'true'; 
+0

它將在警告框中返回step2.php的代碼,step2.php以開頭,依此類推 – 2014-08-28 17:33:14

相關問題