2016-08-06 14 views
0

クリックしたときにデータを切り替えるボタンがあります。ページが読み込まれるとき、私はすべての "新しい"項目をロードしたい。ページが読み込まれたときに「新規」が読み込まれますが、フォームを送信して「使用済み」または「古い」を取得した場合でも、「新規」アイテムは引き続き表示されます。私はPHPに新しいです。ありがとう!phpフォームelse ifステートメント

<form action='' method='POST'> 
      <input type='submit' name='New' value="New"/> 
      <input type='submit' name='Used' value="Used"/> 
      <input type='submit' name='Outdated' value="Outdated" /> 
      </form> 

ここはphpです。これは今だけ1つの条件が動作します試してみてください

if(isset($_POST['New'])){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
    include '/../includes/product-layout.php'; 
    } 

else if(isset($_POST['Used'])){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' "; 
    include '/../includes/product-layout.php'; 
    } 

else if(isset($_POST['Outdated'])){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' "; 
    include '/../includes/product-layout.php'; 
    } 

else { 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
    include '/../includes/product-layout.php'; 
    } 

if(isset($_POST['New'])){ 
     $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
     include '/../includes/product-layout.php'; 
     } 

    if(isset($_POST['Used'])){ 
     $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' "; 
     include '/../includes/product-layout.php'; 
     } 

    if(isset($_POST['Outdated'])){ 
     $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' "; 
     include '/../includes/product-layout.php'; 
     } 

    else { 
     $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
     include '/../includes/product-layout.php'; 
     } 
+0

フォームを送信するときに 'New'、' Used'、 'Outdated'の値はすべて' $ _POST'配列になりますので、すべての条件が真となります...論理を再考してください – RamRaider

答えて

0

あなたがように、すべてのこれらの条件を変更してください間違った方法であれば、他の条件を使用しています。

+0

これはうまくいきました完全に!ありがとう!! – Alex

0

あなたは

<form action='' method='POST'> 
     <input type='submit' name='my_submit' value="New"/> 
     <input type='submit' name='my_submit' value="Used"/> 
     <input type='submit' name='my_submit' value="Outdated" /> 
</form> 

とサーバ側ここ

if($_POST['my_submit'] =='New'){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
    include '/../includes/product-layout.php'; 
    } 

if($_POST['my_submit'] == 'Used'){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Used%' "; 
    include '/../includes/product-layout.php'; 
    } 

if($_POST['my_submit'] == 'Outdated'){ 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%Outdated%' "; 
    include '/../includes/product-layout.php'; 
    } 

else { 
    $sql = "SELECT name FROM table_all_items WHERE name LIKE '%New%' "; 
    include '/../includes/product-layout.php'; 
    } 
0

はの問題を回避するために、一つの可能​​なアプローチで同じ名前を使用して、あなたのHTMLページに異なる値

をテストする必要があります複数の条件が成立していると真である

<form method='POST'> 
    <input type='button' name='New' value="New"/> 
    <input type='button' name='Used' value="Used"/> 
    <input type='button' name='Outdated' value="Outdated" /> 
    <input type='hidden' id='action' /> 
</form> 

<script type='text/javascript'> 
    var col=document.querySelectorAll('form > input[type="button"]'); 
    if(col)for(var n in col)if(col[n].nodeType==1)col[n].addEventListener('click',function(e){ 
     document.getElementById('action').value=this.value; 
     this.parentNode.submit(); 
    }.bind(col[n]),false); 
</script> 

if(isset($_POST['action'])){ 
    $term=filter_input(FILTER_POST, 'action', FILTER_SANITIZE_STRING); 
    $sql="select `name` from `table_all` where `name` like '%{$term}%';"; 
    if(in_array($term,array('New','Used','Outdated'))){ 
     include '/../includes/product-layout.php'; 
    } 
} 
+0

私は間違っていたと私は間違っていることを認識しています。提出ボタンのすべての値はPOST配列内にあると思われましたが、確かにクロムではそうではありません。 – RamRaider

関連する問題