2016-04-08 15 views
1
<html> 
<head> 
<script> 
function addstafflist() 
{ 
    for(var i = 0; i < document.getElementById("stafflist").options.length; i++) 
    { 
     if(document.getElementById("stafflist").options[i].selected) 
     { 
      var staffname = document.getElementById("stafflist").options[i].text; 
      var selectedstaffnamelist = document.getElementById("selectedstafflist"); 
      var found = false; 

     for(var j = 0; j < selectedstaffnamelist.length; j++) 
     { 
      if(selectedstaffnamelist[j].text == staffname) 
      { 
       found = true; 
       break; 
      } 
     } 

     if(!found) 
     { 
      var option = document.createElement("option"); 
      option.text = staffname; 
      selectedstaffnamelist.add(option); 
     } 
    } 
} 

</script> 
</head> 

<body> 
<form action="test2.php" method="post"> 
<table border="0" width="300" style="border-collpase:collapse"> 
    <tr> 
    <td width="50"> 
     <select id="stafflist" name="stafflist[]" style="width:70px;" multiple> 
     <option value="[email protected]">ali</option> 
     <option value="[email protected]">abu</option> 
     <option value="[email protected]">ahmad</option> 
     </select> 
    </td> 

    <td width="30"> 
     <input type="button" name="add" value=">>" onclick="addstafflist()" /> 
    </td> 

    <td> 
     <select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple> 
     </select> 
    </td> 
    </tr> 

    <tr height="20"> 
    <td colspan="3"> 
     <input type="submit" name="submit" value="Show" /> 
    </td> 
    </tr> 
</table> 
</form> 
</body> 
</html> 

<?php 
if(isset($_POST['submit'])) 
{ 
    //[HERE] 
} 
?> 

上記のコードから、ドロップダウンリストから「abu」を選択して「表示」ボタンをクリックすると、PHPを使用して電子メールアドレス([email protected]、abu @ gmail.com)の[ここ]セクションの[表示]ボタンをクリックしてください。誰か助けてくれますか?PHPを使用した表示名

+0

あなたは、クライアント側とサーバー側の違いを学ぶ必要があります。 http://stackoverflow.com/a/13840431/3083093 – R3tep

答えて

1

投稿されたスタッフリストとエコーメールをすべてforeachするには、次のコードを追加します。

<?php 
if(isset($_POST['submit'])) 
{ 
    foreach($_POST['stafflist'] as $email){ 
     echo $email . "<br/>"; 
    } 
} 
?> 

私はあなたのJSのすべてを使用しているかどうかはわかりませんが。

0

あなたのコードにいくつかのアドバイスを行いました。 これは正しい方法で、あなたの提出を検証する必要があります。

<html> 
    <head> 
     <title>Example</title> 
    </head> 
    <body> 

     <?php 
     // If stafflist is array(), then you can go to loop 
     if(is_array($_POST['stafflist'])) 
     { 
      // Run the loop 
      foreach($_POST['stafflist'] as $email) 
      { 
       // Show all emails per line 
       echo $email . "<br/>"; 
      } 
     }  
     ?> 

     <form action="test2.php" method="post"> 
      <table border="0" width="300" style="border-collpase:collapse"> 
       <tr> 
       <td width="50"> 
        <select id="stafflist" name="stafflist[]" style="width:70px;" multiple> 
        <option value="[email protected]">ali</option> 
        <option value="[email protected]">abu</option> 
        <option value="[email protected]">ahmad</option> 
        </select> 
       </td> 

       <td width="30"> 
        <input type="button" name="add" value=">>" onclick="addstafflist()" /> 
       </td> 

       <td> 
        <select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple> 
        </select> 
       </td> 
       </tr> 

       <tr height="20"> 
       <td colspan="3"> 
        <input type="submit" name="submit" value="Show" /> 
       </td> 
       </tr> 
      </table> 
     </form> 

     <script> 
      // Put all JS before end of body 
      function addstafflist() 
      { 
       for(var i = 0; i < document.getElementById("stafflist").options.length; i++) 
       { 
        if(document.getElementById("stafflist").options[i].selected) 
        { 
         var staffname = document.getElementById("stafflist").options[i].text; 
         var selectedstaffnamelist = document.getElementById("selectedstafflist"); 
         var found = false; 

        for(var j = 0; j < selectedstaffnamelist.length; j++) 
        { 
         if(selectedstaffnamelist[j].text == staffname) 
         { 
          found = true; 
          break; 
         } 
        } 

        if(!found) 
        { 
         var option = document.createElement("option"); 
         option.text = staffname; 
         selectedstaffnamelist.add(option); 
        } 
       } 
      } 
     </script> 
    </body> 
</html> 
0

私は、POSTが配列である場合には、第1チェックするクリスティアーノのPHPの提案を使用していました。私はまた、最後のbodyタグの近くにjavascriptを置く方が良いことに同意するので、より多くのスクリプトでは、ページレンダリングの遅延が少なくなります。あなたのコードのネストに注意するもう一つの良いアドバイス。それは読みやすく、デバッグしやすくなります...ブラケットがないと貴重な時間を無駄にすることがあります。 ;)

あなたのコードに3行だけ追加しました。そして私は正しいPOSTを使いました。テスト済みの作業。

<html> 
<head> 
    <title>Example</title> 
</head> 

<?php 
if(isset($_POST['submit'])) 
{ 
    //[HERE] 
    // If stafflist is array(), then you can go to loop 
    if(is_array($_POST['stafflist'])) 
    { 
     foreach($_POST['selectedstafflist'] as $email){ 
      echo $email . "<br/>"; 
     } 
    } 
} 
?> 

<body> 
<form action="test2.php" method="post"> 
    <table border="0" width="300" style="border-collpase:collapse"> 
     <tr> 
      <td width="50"> 
       <select id="stafflist" name="stafflist[]" style="width:70px;" multiple> 
        <option value="[email protected]">ali</option> 
        <option value="[email protected]">abu</option> 
        <option value="[email protected]">ahmad</option> 
       </select> 
      </td> 

      <td width="30"> 
       <input type="button" name="add" value=">>" onclick="addstafflist()" /> 
      </td> 

      <td> 
       <select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple> 
       </select> 
      </td> 
     </tr> 

     <tr height="20"> 
      <td colspan="3"> 
       <input type="submit" name="submit" value="Show" /> 
      </td> 
     </tr> 
    </table> 
</form> 

<script> 
function addstafflist() 
{ 
    for(var i = 0; i < document.getElementById("stafflist").options.length; i++) 
    { 
     if(document.getElementById("stafflist").options[i].selected) 
     { 
      var staffname = document.getElementById("stafflist").options[i].text; 
      var staffemail = document.getElementById("stafflist").options[i].value;   // <-- Added this line 
      var selectedstaffnamelist = document.getElementById("selectedstafflist"); 
      var found = false; 

      for(var j = 0; j < selectedstaffnamelist.length; j++) 
      { 
       if(selectedstaffnamelist[j].text == staffname) 
       { 
        found = true; 
        break; 
       } 
      } 

      if(!found) 
      { 
       var option = document.createElement("option"); 
       option.text = staffname; 
       option.value = staffemail;   // <-- Added this line 
       option.selected=true;    // <-- Added this line 
       selectedstaffnamelist.add(option); 
      } 
     } 
    } 
} // <-- Added this missing bracket 
</script> 
</body> 

関連する問題