简体   繁体   English

JavaScript 在 PHP 之前没有响应(表单操作 = “php 文件” onsubmit = “表单的字段验证功能”

[英]JavaScript not responsive before PHP (form action = "php file" onsubmit= " form’s fields validating function"

I have investigated for a long time (too long) on the net (including StackOverflow eg) without any success.我已经在网上(包括 StackOverflow 例如)上调查了很长时间(太长),但没有任何成功。 The question is asked by many people but no one among the proposed solutions helps me.很多人都问了这个问题,但提出的解决方案中没有一个对我有帮助。 So in desperation, I turn again to this forum.所以无奈之下,我又回到了这个论坛。

<form id="myForm" method="post" action="updateMySQLGuests.php" onsubmit="return formSubmit()">

This boolean result function checks whether the value of the fields is correct before calling UpdateMySQLGuests.php (for example, verify if the e-mail contains a @ ) to create a record with the value of these form fields by POST method in MySQL.这个布尔结果函数在调用UpdateMySQLGuests.php之前检查字段的值是否正确(例如,验证电子邮件是否包含@ )以在 MySQL 中通过 POST 方法使用这些表单字段的值创建记录。 But action= updateMySQLGuests.php is executed before the formSubmit() function (which, in fact, is not executed at all).但是action= updateMySQLGuests.phpformSubmit()函数之前执行formSubmit()实际上根本没有执行)。 I work in localhost with Wampserver.我在localhost使用 Wampserver 工作。

You can move the form's action to formSubmit() to solve this您可以将表单的操作移动到formSubmit()来解决此问题

<form id="myForm" method="post" onsubmit="return formSubmit()">

and the formSubmit() code: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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM