2016-11-18 63 views
-2

我有一個PHP表單,如下所示。一旦用戶選擇了特定的行/行,我需要將相應的數據發送到我的PHP後端腳本。這裏的externalID是唯一的。我的臉在我的代碼的兩個問題如何將選定的html行數據發送到PHP後端腳本

  1. 目前我得到整個表單數據到我的PHP腳本的後臺,我需要相當於只有我選擇的行數據。

  2. 我在頁面中有兩個按鈕,我怎樣才能在點擊按鈕時調用不同的php腳本。目前,點擊「發送MTDATA」在表單動作中調用php腳本。

SRC代碼:

<form target="iframe_b" action="/php_src/first.php" method="POST" 
      echo "sending data"> 
     <fieldset> 
    <br><br> 
     <input type="button" id="activate_device" name="activatedevice" value="Activate_device"> 
     <input type="submit" value="SEND MTDATA"> 
    <table border="1"> 
    <tr> 
      <th><input type="checkbox" id="selectall"/></th> 
      <th>External ID</th> 
      <th>Status</th> 
    </tr> 

    <?php 
    $dbhost = 'localhost:3036'; 
     $dbuser = 'root'; 
     $dbpass = 'xxxx'; 
     $conn = mysql_connect($dbhost, $dbuser, $dbpass); 
     if (!$conn) { 
      die('Could not connect: ' . mysql_error()); 
     } 
     mysql_select_db("ApplicationServer") or die(mysql_error()); 
     // Get all the data from the "example" table 
     $result = mysql_query("SELECT EXTERNAL_ID,CONNECTION_STATUS FROM DEVICE_DETAILS") or die(mysql_error()); 
     while ($row = mysql_fetch_array($result)) { 
     $extID = $row['EXTERNAL_ID']; 
     $conStatus = $row['CONNECTION_STATUS']; 
      echo "</tr><tr>"; 
      echo "</td><td>"; 
      echo "<input type=\"checkbox\" class=\"case\" name=\"checkBox".$extID."\" />"; 
      echo "</td><td>"; 
      echo "<input type=\"text\" class=\"classextID\" value=\"$extID\" name=\"textExID".$extID."\" />"; 
      echo "</td><td>"; 
      echo "<input type=\"text\" class=\"classConnection\" value=\"$conStatus\" name=\"textcon".$extID."\" />"; 

    } 
?> 

</table> 
</fieldset> 
</form> 

輸出:

array(9) { ["[email protected]_com"]=> string(2) "on" ["[email protected]_com"]=> string(19) "[email protected]" ["[email protected]_com"]=> string(16) "request accepted" ["[email protected]_com"]=> string(2) "on" ["[email protected]_com"]=> string(17) "[email protected]" ["[email protected]_com"]=> string(16) "request accepted" ["[email protected]_com"]=> string(2) "on" ["[email protected]_com"]=> string(18) "[email protected]" ["[email protected]_com"]=> string(16) "request accepted" } 

Htm form with checkbox

回答

1

所有的HTML投入 - 除了選中複選框 - 被髮送到服務器,所以唯一的辦法,以避免即在表單完成之前,使用javascript從表單中完全刪除未選中的行NT。

但是,如果總的數據量是沒有問題的,但你只是希望能夠輕鬆地選擇檢查行,你應該在你的HTML中使用數組:

echo "<input type=\"checkbox\" class=\"case\" name=\"checkBox[".$extID."]\" />"; 
                  ^ array^
echo "</td><td>"; 
echo "<input type=\"text\" class=\"classextID\" value=\"$extID\" name=\"textExID[".$extID."]\" />"; 
echo "</td><td>"; 
echo "<input type=\"text\" class=\"classConnection\" value=\"$conStatus\" name=\"textcon[".$extID.]"\" />"; 

現在你$_POST['checkBox']等。變量也是php中的數組,所以你可以很容易地循環它們,因爲關鍵是你的$extID值。正如我前面提到的,只有選中的複選框被髮送到服務器:

// Loop over the sent-in/selected checkboxes 
foreach (array_keys($_POST['checkBox']) as $key) { 
    var_dump($_POST['textExID'][$key]); 
    // etc. 
} 

另外請注意,您的HTML可能是無效的,你不關閉的最後一排,但我不認爲這會導致任何形式的問題。

+1

我建議您在其個人資料中看到OP的最新問題。他們正在慢慢從其他答案獲取代碼並從其上重新發布。我關閉了他們的最新版本,與他們在此發佈的版本完全相同。 –

+0

@ Fred-ii-嗯,這不太好,我必須記住這一點... – jeroen

+1

我發現在OP的某些模式上更好些;-) –

0

一種方法是收集數組中的輸入。

<?php 
echo "</tr><tr>"; 
echo "</td><td>"; 
echo "<input type='checkbox' class='case' name='$extID[ checkBox ]' />"; 
echo "</td><td>"; 
echo "<input type='text' class='classextID' value='$extID' name='$extID[ textExID ]' />"; 
echo "</td><td>"; 
echo "<input type='text' class='classConnection' value='$conStatus' name='$extID[ textcon ]' />"; 
?> 

然後,如果你檢查$ _ POST,你會發現一個數組內的陣列中,環路的第一陣列和收集每個$ EXTID細節。

至於這兩個按鈕,你可以命名提交按鈕,然後檢查看看哪些被按下。

<input type="button" id="activate_device" name="activatedevice" value="Activate_device"> 
<input type="submit" value="SEND MTDATA" name="submit_button"> 

<?php 

if(isset($_POST[ 'submit_button' ])) 
{ 
    // Process submit 
} 
elseif(isset($_POST[ 'activatedevice' ])) 
{ 
    // Process device 
} 
+0

真棒謝謝你們...... – ramkriz

相關問題