如何在使用其他按钮提交表单之前使按钮单击功能起作用?

时间:2018-02-17 17:28:18

标签: javascript

我有3个按钮,其中一个提交(提交)表单,另一个用随机数据填充输入窗口(rand):

  <div type="input">
    <input type="submit" id="submit";">
    <input type="button" id="rand" value="Random";">
    <input type="reset" id="log" value="Log" onclick="window.location.href='log.txt';">
  </div>

在我提交表单之前,我无法使用随机按钮用随机数据填充输入窗口。我如何进行调试,以便在填充输入屏幕之前不需要提交表单?

<body>



  <h1>Payment Form</h1>
    <form id="myForm" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post";>
      <div type="input">
        <div>
          <label>First Name </label>
          <input type="text" name="firstname" id="firstname">
        </div>
        <div>
          <label>Last Name </label>
          <input type="text" name="lastname" id="lastname">
        </div>
        <div>
          <label>Student ID </label>
          <input type="text" name="studentid" id="studentid">
        </div>
        <div>
          <label>Tuition </label>
          <input type="text" name="tuition" id="tuition"> <br>
        </div>
      <div>
        <label>Payment Method </label>
        <select name="selection" id="selection">
            <option value="credit">Credit</option>
            <option value="debit">Debit</option>
            <option value="bitcoin">Bitcoin</option>
        </select>
      </div>
      </div>
      <div type="input">
        <input type="submit" id="submit";">
        <input type="button" id="rand" value="Random";">
        <input type="reset" id="log" value="Log" onclick="window.location.href='log.txt';">
      </div>
    </form>
  <br> 
     <div id="output">
       <ul></ul>
     </div>


<?php

  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {

    if (isset($_POST[firstname]) && isset($_POST[lastname]) && isset($_POST[studentid]) && isset($_POST[tuition]) && isset($_POST[selection]))
    {
        $success = 0;
        $frstnm = $_POST[firstname];
        $lstnm = $_POST[lastname];
        $stdntd = $_POST[studentid];
        $ttn = $_POST[tuition];
        $slctn = $_POST[selection];
        $slctn = ucwords($slctn);
        $dataarray = [$frstnm,$lstnm,$stdntd,$ttn,$slctn];

        if (strlen($frstnm) < 2)
        {
          $myerror = "<li> First name must be 2 or more characters in length";
        }
        if ((strlen($lstnm) < 3) || (strlen($lstnm) > 12))
        {
          $myerror .= "<li> Last name must be between 3 and 12 characters in length";
        }
        if (strlen($stdntd) != 9)
        {
          $myerror .= "<li> Student id must be exactly 9 characters in length";
        }
        if (($ttn < 2000) || ($ttn > 10000))
        {
          $myerror .= "<li> Tuition must be between 2000 and 10000";
        }
        if(strlen($myerror)==0)
        {
          $myerror = "Payment Successful!";
          $success = 1;
        }
        if($success == 1){
          $fp = fopen('log.txt', 'a');
          fputcsv($fp, $dataarray);
          fclose($fp);
        }
    }
  }
?>
<div id="output2"></div>

<script>

   if(<?php echo $success ?>== 1){
    document.getElementById("output").className = "success";
   }
   else{
    document.getElementById("output").className = "error";
   }

   <?php 
   echo "document.getElementById('output').innerHTML ='".$myerror."'";
   ?>


  $("#rand").click(
      function()
      {
        $.get("rangen.php",
        {},
        function(data)
        {
        $("#firstname").val(data.firstname);
        $("#lastname").val(data.lastname); 
        $("#studentid").val(data.studentid);
        $("#tuition").val(data.tuition);
        $("#selection").val(data.method.toLowerCase());
        },'json');
      }
  );


</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当您第一次加载页面时,即在提交表单之前,未定义以下变量:

  1. $myerror
  2. if(<?php echo $success ?>== 1)
  3. 因此,在呈现页面时,<br /> <b>Notice</b>: Undefined variable: success in <b><<filename>></b> on line <b>95</b><br />会产生类似:$myerror的内容。 $("#rand").click()发生了类似的错误。

    这些错误会阻止执行脚本标记中的其余代码。因此,isset()函数永远不会起作用。

    可以通过在回显其值之前使用if(<?php echo $success ?>== 1)函数检查它们的存在来修复这些错误。

    1. if(isset($success)){echo $success;}else{echo 0;}应更改为:echo "document.getElementById('output').innerHTML ='".$myerror."'";
    2. if(isset($myerror)){ echo "document.getElementById('output').innerHTML ='".$myerror."'"; }应更改为{{1}}
    3. 希望这有帮助!