我有一个HTML表单,可以从用户那里获取一些文本数据。我想将它插入到我的数据库中并保持在同一个表单上,以便可以在数据库中创建另一行。如果我使用action ='includes / addUser.php'从表单中调用它,那么我的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";
&GT;
使用action ='includes / addUser.php'行未注释掉(并且onSubmit已注释掉)然后它可以正常工作,因为我想要显示空白页
当onSubmit行处于活动状态时,调用javascript addUser函数(“javascript addUser()”输出到控制台,警报显示“User added”)但addUser.php文件中似乎没有任何内容跑。如果我在addUser.php的开头include_once一个不存在的文件,我没有看到错误。
答案 0 :(得分:1)
使用网址后:
url: "includes/addUser.php",
data : $("#addUserFormID").serialize(),
type: "POST",
.......
答案 1 :(得分:1)
您需要在请求中包含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");
}
});
});
});