簡體   English   中英

確定表單上有兩個按鈕時單擊了哪個按鈕

[英]Determine which button was clicked when two buttons are on a form

我試圖在表單中添加兩個按鈕以捕獲與供應商聯系以及他們回復時的時間。

我遇到的問題是,當我單擊一個按鈕時,兩個字段中的值都在變化。 請建議我該如何解決。

謝謝!

 <tr>     
    <th>
    <p>Vendor was contacted at:</p>
      <button name="start" >Start</button><br>
      <?php
      session_start ();
      if (isset($_POST['start']))
      {
      $date_start = date('m-d-Y H:i:s');
      $_SESSION['start'] = $date_start;
      }
      ?>
      <br>
      <input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>"
      <br>
      </th>
 </tr>
 <tr>
      <th>
      <p>Vendor responded at:</p>
      <button name="response" >Response</button><br>
      <?php
      if (isset($_POST['response']))
      {
      $date_response = date('m-d-Y H:i:s');
      $_SESSION['response'] = $date_response;
      }
      ?>
      <br>
      <input name="response" type="text" class="textfield" value="<?php echo $_SESSION['response']; ?>"
      <br>
      </th>
 </tr>

首先, $_POST['start']是名為start的輸入值,而$_POST['response']是名為response的輸入值。 如果您想知道在表單中單擊了哪個按鈕,則必須為按鈕設置一個值。 <button name="start" >更改為<input type="submit" value="start" /> 比起檢查單擊的按鈕, if ('start' === $_POST['submit']) 在底部,您的代碼應如下所示:

<tr>     
    <th>
        <p>Vendor was contacted at:</p>
          <!-- changed line -->
          <input type="submit" name="submit" value="start" /><br>
          <?php
          session_start ();
          /* changed line */
          if ('start' === $_POST['submit']) {
              $date_start = date('m-d-Y H:i:s');
              $_SESSION['start'] = $date_start;
          }
          ?>
          <br>
          <input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>"
          <br>
    </th>
</tr>

祝好運!

這里有幾處錯誤...

首先-您正在加載頁面並嘗試回顯不存在的會話。 這會給你錯誤。

要解決此問題,請放置$_SESSION['start'] = ''; 就在if (isset($_POST['start']))上方並放置$_SESSION['response'] = ''; 恰好在if (isset($_POST['response']))上方。這定義了您的會話,但其值為空。

第二-將每個“表格”行放在各自的表格和表單中。

<form name="form1" method="post" action="">
<table>
  <tr>     
    <th>
    <p>Vendor was contacted at:</p>
      <button name="start" >Start</button><br>
      <?php
      if (!isset($_SESSION)) {
          session_start ();
      }
      /* these five lines ask if there is an existing session & if there is an existing session... is it empty
      if there is an existing session & it's not empty... we use it
      otherwise... we create an empty session to prevent errors
      this also maintains the session data when the other button is clicked like you asked in your comments */
      if(isset($_SESSION['start']) && $_SESSION['start'] != '') {
          $_SESSION['start'] = $_SESSION['start'];
      } else {
          $_SESSION['start'] = '';
      }
      if (isset($_POST['start']))
      {
      $date_start = date('m-d-Y H:i:s');
      $_SESSION['start'] = $date_start;
      }
      ?>
      <br>
      <input name="start" type="text" class="textfield" value="<?php echo $_SESSION['start']; ?>">
      <br>
      </th>
  </tr>
</table>
</form>
<form name="form2" method="post" action="">
<table>
  <tr>
      <th>
      <p>Vendor responded at:</p>
      <button name="response" >Response</button><br>
      <?php
      if(isset($_SESSION['response']) && $_SESSION['response'] != '') {
          $_SESSION['response'] = $_SESSION['response'];
      } else {
          $_SESSION['response'] = '';
      }
      if (isset($_POST['response']))
      {
      $date_response = date('m-d-Y H:i:s');
      $_SESSION['response'] = $date_response;
      }
      ?>
      <br>
      <input name="response" type="text" class="textfield" value="<?php echo $_SESSION['response']; ?>">
      <br>
      </th>
  </tr>
</table>
</form>

更新:

如果要保留數據(如果要保留數據)...更改...

      <?php
      session_start ();
      $_SESSION['start'] = '';
      if (isset($_POST['start'])) {
          $date_start = date('m-d-Y H:i:s');
          $_SESSION['start'] = $date_start;
      }
      ?>

至...

      <?php
      if (!isset($_SESSION)) {
          session_start ();
      }
      if(isset($_SESSION['start']) && $_SESSION['start'] != '') {
          $_SESSION['start'] = $_SESSION['start'];
      } else {
          $_SESSION['start'] = '';
      }
      if (isset($_POST['start']))
      {
      $date_start = date('m-d-Y H:i:s');
      $_SESSION['start'] = $date_start;
      }
      ?>

並改變...

  <?php
  $_SESSION['response'] = ''; 
  if (isset($_POST['response'])) {
      $date_response = date('m-d-Y H:i:s');
      $_SESSION['response'] = $date_response;
  }
  ?>

至...

      <?php
      if(isset($_SESSION['response']) && $_SESSION['response'] != '') {
          $_SESSION['response'] = $_SESSION['response'];
      } else {
          $_SESSION['response'] = '';
      }
      if (isset($_POST['response']))
      {
      $date_response = date('m-d-Y H:i:s');
      $_SESSION['response'] = $date_response;
      }
      ?>

編碼愉快!

暫無
暫無

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

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