如何解决PHP之前没有响应的JavaScript(表单action =“php file”onsubmit =“form的字段验证函数”?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (110)
<form id="myForm" method="post" action="updateMySQLGuests.php" onsubmit="return formSubmit()">

布尔结果函数在调用之前检查字段的值是否正确UpdateMySQLGuests.php(例如,验证电子邮件是否包含a @),以便通过MySQL中的POST方法创建具有这些表单字段值的记录,但是action= updateMySQLGuests.phpformSubmit()函数之前执行,该如何解决呢?

提问于
用户回答回答于

可以将窗体的操作移动到formSubmit()来解决这个问题

<form id="myForm" method="post" onsubmit="return formSubmit()">
function formSubmit() {
  // the old code
  ...

  // call submit form
  document.getElementById('myForm').action = "updateMySQLGuests.php";
  document.getElementById('myForm').submit();

}

用户回答回答于
<!--  in place of <form name= "myForm" id="myForm" action="updateMySQLGuests.php" method="post" onsubmit="return formSubmit()"> : -->

<form name="myForm" id="myForm" action="updateMySQLGuests.php" method="post">
    <!-- action="updateMySQLGuests.php" -->
    <label for="firstnames">Prénom:</label>
    <input type="text" id="firstname" size="20" name="firstname"><br>
    <label for="Lastnames">Nom:</label>
    <input type="text" id="lastname" size="20" name="lastname" list="lstLastnames">
    <datalist id="lstLastnames"> </datalist> <br>
    <label for="email">E-mail:</label>
    <input type="text" id="email" size="20" name="email"><br><br>

    <!-- in place of <input type="submit" value="post"/> : -->

    <button onclick="return validateValues()">Submit</button>
</form>

扫码关注云+社区

领取腾讯云代金券