[英]Ajax call to a php file is not working
I am trying to implement a simple form which will eventually connect to a database and make entries in it. 我正在尝试实现一个简单的表单,该表单最终将连接到数据库并在其中进行输入。 In the tag,I am calling the php file which will connect me to the database in the back-end.
在标签中,我正在调用php文件,它将把我连接到后端的数据库。
index.html index.html
<html>
<head>
<script>
function submitForm(formId){
//var formData= $.(formId).serialize();
$.ajax({
url:'new-user.php',
type:'POST',
data:{
user_name=$("#user_name").val(),
password=$("#password").val();
}
success:function(response){
alert(response);
}
});
}
</script>
</head>
<body>
<form onsubmit="submitForm('#myForm');" id='myForm'>
User Name: <input type="text" name="user_name" id="user_name" />
Password: <input type="text" name="password" id="password" />
<input type="submit"/>
</form>
</body>
</html>
new-user.php new-user.php
<?php include 'database.php';?>
<?php
mysqli_query($connect,"create table login(User_name varchar(50) NOT NULL,Password varchar(50) NOT NULL)");
$user_name=$_POST['user_name'];
$password=$_POST['password'];
if(empty($user_name)){
$name_error="name is required";
}
mysqli_query($connect,"Insert into login(User_name,Password) values('$user_name','$password')");
if(mysqli_affected_rows($connect)>0){
echo "<p>Credentials added</p>";
echo "<a href='index.html'>Go back</a>";
}else{
echo "<p>Error</p>";
echo mysqli_error($connect);
}
?>
database.php database.php
<?php
$connect=mysqli_connect('localhost','root','','testdb');
if(mysqli_connect_errno($connect)){
echo 'failed to connect';
}
?>
The above is not creating any table in the testdb database.Neither,it is generating any alert messages.The Url however changes after clicking the submit button as http://localhost/try2/?user_name=aayushi&password=ded
but after that nothing happens. 上面没有在testdb数据库中创建任何表。也没有生成任何警报消息。但是,在单击“提交”按钮后,URL更改为
http://localhost/try2/?user_name=aayushi&password=ded
但此后什么也没有发生。 This is my first php code, so I don't really know what's the meaning of this exactly. 这是我的第一个php代码,所以我真的不知道这到底是什么意思。
Okay, since no one seems to actually be reading your code, there's a couple of syntax errors that I missed until I threw it into PhpStorm 好的,因为似乎没有人真正在阅读您的代码,所以在将其放入PhpStorm之前,我遗漏了一些语法错误
Change your function to this: 将功能更改为此:
function submitForm(formId){
$.ajax({
url:'/new-user.php',
type:'POST',
data:{
user_name: $("#user_name").val(),
password: $("#password").val()
}
})
.complete(function (response) {
alert(response)
})
return false; // Prevents the form from submitting the standard way
}
EDIT: Change the form to this: 编辑:将窗体更改为此:
<form onsubmit="return submitForm('#myForm');" id='myForm'>
In your ajax method, the success property is wrong 在您的ajax方法中,成功属性是错误的
It is written as suceess
, when it was supposed to be success
当
success
时候,它被写成suceess
。
Also, to avoid refreshing the page, insert return false;
另外,为避免刷新页面,请插入
return false;
at the end of the function submitForm
在函数
submitForm
的末尾
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.