php的工作方式是action,而不是html形式的onSubmit
php works from action but not from onSubmit in html form
我有一个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",
.......
相关文章:
- OnSubmit字段中的Html Form If语句
- html获取调用onsubmit的位置
- 尽管使用了Onsubmit确认,HTML表单仍然设置PHP$_POST[]
- HTML和JavaScript.onSubmit从不阻止发布表单
- Java Script: onsubmit=“check_rfields();” in form tag of html
- HTML Forms - 通过 onsubmit 事件处理程序调用 JavaScript
- Javascript 输入到 HTML 表单 onSubmit 与 # 操作到 PHP $_GET(同一文件上的所有代码
- PHP & HTML Form with javascript onsubmit not executed
- html onsubmit不't启动Javascript函数
- HTML Javascript onsubmit忽略了我的Javascript函数
- 当我从onsubmit返回false时,HTML表单仍然提交
- 在HTML表单的onSubmit属性中使用的简单PHP脚本到JavaScript函数
- 取消一个HTML onSubmit事件
- HTML表单动作和onsubmit问题
- 为什么当我从onsubmit返回false时,我的HTML表单仍然提交
- 我需要一个自定义的确认框在html表单Onsubmit()中实现.我该如何实现呢
- 在php中调用html表单onsubmit
- php的工作方式是action,而不是html形式的onSubmit
- JavaScript/HTML onSubmit="return Validation()" iss
- JavaScript onsubmit event won't更新html表单动作属性