php的工作方式是action,而不是html形式的onSubmit

php works from action but not from onSubmit in html form

本文关键字:html onSubmit 工作 方式 action php      更新时间:2023-09-26

我有一个HTML表单,它从用户获得一些文本数据。我想将其插入到我的数据库中,并保持在相同的表单上,以便在数据库中创建另一行。我有php工作很好,如果我从使用action='includes/addUser.php'的形式调用它,但那就离开浏览器的addUser.php页面不显示任何东西。我曾以为我可以使用ajax来不改变显示,但php似乎没有被调用。如果我在ajax调用中更改php文件的名称,我确实会看到一个错误。

<div id="addUserFormDivID">
    <!-- <form id='addUserFormID' method='post' action='includes/addUser.php'> -->
    <form id='addUserFormID' method='post' onSubmit='javascript:addUser()'>
        Add User<br /><br />
        <table>
            <tr>
                <td align="right">Full name:</td>
                <td align="left"><input type="text" name="fullName" /></td>
            </tr>
            <tr>
                <td align="right">Nickname:</td>
                <td align="left"><input type="text" name="nickName" /></td>
            </tr>
            <tr>
                <td align="right">Email:</td>
                <td align="left"><input type="text" name="email" /></td>
            </tr>
            <tr>
                <td align="right">Phone:</td>
                <td align="left"><input type="text" name="phone" /></td>
            </tr>
            <tr>
                <td align="right">Postal Address:</td>
                <td align="left"><input type="text" name="address" /></td>
            </tr>
            <tr>
                <td align="right">Postcode:</td>
                <td align="left"><input type="text" name="postcode" /></td>
            </tr>
        </table>            
        <input type="submit" name="submitAddUser" value="Add User" style="float:right">  
    </form>
</div>
<script type="text/javascript">
    function addUser() 
    {
        console.log("javascript addUser()");
        $.ajax
        ({
            type: "POST",
            url: "includes/addUser.php",
            //data: '1',
            success: function()
            {
                alert("User added");
            }
        }); // Ajax Call            alert("hello");
    }

addUser.php:

<?php
include_once 'db_connect.php';
include_once 'functions.php';
//sec_session_start();
debug_to_console("addUser.php");
$fullName = $_POST[fullName];
$nickName = $_POST[nickName];
$email = $_POST[email];
$phone = $_POST[phone];
$address = $_POST[address];
$postcode = $_POST[postcode];
$stmt = mysqli_prepare($mysqli, "INSERT INTO Person (FullName, NickName, Email, Phone, PostalAddress, Postcode) VALUES (?, ?, ?, ?, ?, ?)");
if ($stmt === false) 
{
    trigger_error('Statement failed! ' . htmlspecialchars(mysqli_error($mysqli)), E_USER_ERROR);
}
$bind = mysqli_stmt_bind_param($stmt, "ssssss", $fullName, $nickName, $email, $phone, $address, $postcode );
if ($bind === false) 
{
    trigger_error('Bind param failed!', E_USER_ERROR);
}
$exec = mysqli_stmt_execute($stmt);
if ($exec === false) 
{
    trigger_error('Statement execute failed! ' . htmlspecialchars(mysqli_stmt_error($stmt)), E_USER_ERROR); 
}
//printf ("New Record has id %d.'n", mysqli_insert_id($mysqli));
mysqli_stmt_close($stmt);
echo "done addUser";

?>

与action='includes/addUser.php'行未注释掉(和onSubmit注释掉),然后它的工作,因为我想要显示一个空白页面

与onSubmit行活动相反,javascript addUser函数被调用("javascript addUser()"输出到控制台,警告显示为"用户添加"),但addUser.php文件中没有任何内容似乎正在运行。如果我在addUser.php的开头包含一个不存在的文件,我不会看到错误。

您需要在请求中包含form的数据。还要注意,在JavaScript中连接事件通常被认为是最佳实践。考虑到这一点,试着这样做:

<form id="addUserFormID" method="post" action="include/addUser.php">
    <!-- rest of your inputs -->
</form>
$(function() {
    $('#addUserFormID').submit(function(e) {
        e.preventDefault(); // stop the normal form submission
        $.ajax({
            type: this.method,
            url: this.action,
            data: $(this).serialize(),
            success: function()  {
                alert("User added");
            }
        });
    });
});

url使用后:

  url: "includes/addUser.php",
  data : $("#addUserFormID").serialize(),  
  type: "POST",
  .......