2017-01-12 9 views
-3

すべての文字を無視してくださいPHPとAJAXを使用して条件を検証してください

この闘いは本当です。主にHTMLコードとindex.phpファイルで、私はclass="delete_this"でボタンとして機能する複数の<span>の要素を持っている:任意の<span>要素(は、技術的にどのclass="delete_this"とをクリックすると

<ul id="products"> 
    <li class="product"> 
     <!-- more code --> 
     <div class="select-delete"> 
      <span class="delete-product delete_this" title="Delete"></span> 
     </div> 
    </li> 
    <li class="product"> 
     <!-- more code --> 
     <div class="select-delete"> 
      <span class="delete-product delete_this" title="Delete"></span> 
     </div> 
    </li> 
    <li class="product"> 
     <!-- more code --> 
     <div class="select-delete"> 
      <span class="delete-product delete_this" title="Delete"></span> 
     </div> 
    </li> 
</ul> 

は、jQueryのAJAX要求が行われます

// The exact following line is a simple on click function, it's a little different 
// than the usual since I'm loading .delete_this also dynamically. It runs when you click 
// on any element *.delete_this 
$("#products").on("click", ".delete_this", function(event) { 
    var $count = $("#products > li").length; 
    $.ajax({ 
     method: "POST", 
     url: "validate_existence.php", 
     // The following sends some data to the validate_existence.php file 
     data: {"quantity_of_products": $count}, 

 success:function(html) { 
      if (html == "true") { 
       // condition met 
      } else { 
       // condition not met 
      } 
     } 

}); 
}); 

したがって、ユーザは、次に、<span class="delete_this">をクリックする:(1)変数が定義され、(2)AJAXが要求され、データとしてその変数の値を送信します。

今、validate_existence.phpで、if() {}を使用して条件文を作成します。 AJAX要求で送信された変数$countが1に等しい場合、試験すべき基準は、次のとおりです。問題は、index.phpの内部// condition not metのコードが常に実行である

<?php 

if ($_POST['quantity_of_products'] = 1) { // Edit: thanks everyone for saying to use two equal symbols 
    // Run code if criteria is met 
} else { 
    // Run code if criteria is not met 
} 

?> 

、でも基準wasn場合't met(AKA $_POST['quantity_of_products'] /= 1)。

は、私が最も可能性が高いvalidate_existence.phpファイルで何かが足りない、またはindex.phpに誤っfunction(html)を使用しています。

+0

であなたのコードを置き換え

<?php if ($_POST['quantity_of_products'] = 1) { echo true; } else { echo false; } ?> 

をFiを使用してあなたのajaxをデバッグすることができますreBug – Akshay

+3

$ _POST ['quantity_of_products'] = 1は、使用する必要がある$ _POST ['quantity_of_products']変数に値を割り当てています。== – JYoThI

+0

'valid_existence.php'で' print_r($ _ POST); '変数を最初に表示します。ファイルを取得して何が得られるかを確認しますか? –

答えて

3

変更次の行:

if($_POST['quantity_of_products'] == 1) // This is condition checking 
2

$_POST['quantity_of_products'] = 1

if($_POST['quantity_of_products'] = 1) // This is assignment 

$_POST['quantity_of_products']変数に値を代入します。あなたは(少なくとも私にとっては)POST値をデバッグするこの$_POST['quantity_of_products']==1

if($_POST['quantity_of_products'] == 1) 
+1

レスポンスは文字列ではありませんか? '成功コールバック関数は返されたデータを渡します。返されるデータは、XMLルート要素または応答のMIMEタイプに応じたテキスト文字列になります。 ' – chris85

+0

レスポンスとして文字列を取得することはできますが、Boolen値を返すので、 – JYoThI

+0

ええ、私はあなたに同意します@chris85 – JYoThI

1

まずポイントのように使用しなければならないサービスがデータを受信して​​いることを確認するために、そしてそれが受け取るものを確認することです。 次でそれを行うための最も簡単な:

print_r($_POST['quantity_of_products']; 

または

echo $_POST['quantity_of_products']; 
0

私は最善の方法は、三項演算子を使用することであると思う:

<?php 
$test = $_POST['quantity_of_products'] == 1? true: false; 
echo $test; 
関連する問題