簡體   English   中英

PHP提交表單不起作用

[英]PHP submit form not working

下面是一段將查詢結果輸入單選按鈕的代碼。 然后,當用戶按下表單中的按鈕之一時,它將移至頁面test.php。 我遇到了一個問題,盡管第二個代碼(在test.php上)似乎不起作用,盡管我看不到任何不該這樣做的原因。 任何想法都很棒。 謝謝。

<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
    <td><input type="radio" name="server" value="<?php echo $row['serverId']; ?>" /></td>
    <td><?php echo $row['userName']; ?></td>
    <td><?php echo $row['dateCreated']; ?></td>
    <td><?php echo $row['serverName']; ?></td>
    <td><?php echo $row['serverId']; ?></td>
    <td><?php echo $row['publicDNS'] ?></td>
    <td><?php echo $row['isRunning']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<br>
<?php endif; ?>

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>

</body>
</html>  

第二碼:

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated"
}

?>

$_POST['submit']必須為$_POST['server_stop']

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated";
}

?>

您的代碼:

<?php
if (isset($_POST['server_stop'])) {
    echo "Server Stopped";
}

if (isset($_POST['server_terminate'])) {
    echo "Server Terminated"
}

?>

這兩個條件在表單提交時均成立。 如果要區分按下了哪個提交按鈕,則需要進行隱藏輸入並使用一些javascript:

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="server_stop" value="Stop Server" onclick="whatwasclicked(this);" />
<input type="submit" name="server_terminate" value="Terminate Server" onclick="whatwasclicked(this);" />
<input type="hidden" name="clicked" id="clicked" />
</form>

您的JavaScript將是:

function whatwasclicked(c) {
$("#clicked").val() = c.value;
}

現在在PHP方面,您將檢查$_POST['clicked']並查看按下了哪個按鈕。

編輯:請注意,這里使用了Jquery ...所以一定要加載該庫。

如果您根本不想使用javascript,而只需要在表單中使用那些提交按鈕,則也可以執行以下操作:

<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Stop Server"/>
</form>
<form method="post" name="server_information" id="server_information" action="test.php">
<input type="submit" name="clicked" value="Terminate Server"/>
</form>

現在只需檢查$_POST['clicked'] ,您就會看到按下了哪個按鈕。

if (isset($_POST['submit'])){
  $name =trim($_POST['name']);
  if (empty($name)){
    echo "please input name";    
  }
  else if (!preg_match("/^[a-zA-Z ]*$/",$name)){
    $namerr = "Only letters and white space allowed";
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM