[enter image description here][1] **add.php**
I am not sure that my data is inserted through ajax or not, can anyone solve my issue.
这是我add.php文件我正在插入数据库中的数据,首先我已经包含在该文件中的数据库连接文件,使用POST方法使用mysqli_real_escape_string提交数据从字符串去除特殊字符。检查空白字段,链接到上一页,显示成功消息。只在php中通过AJAX在数据库中插入数据。
<html>
<head>
<title>Add Data</title>
</head>
<body>
<?php
//including the database connection file
include_once("config.php");
$name = $_POST['name'];
$age = $_POST['age'];
$email = $_POST['email'];
// checking empty fields
if(empty($name) || empty($age) || empty($email)) {
if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if(empty($age)) {
echo "<font color='red'>Age field is empty.</font><br/>";
}
if(empty($email)) {
echo "<font color='red'>Email field is empty.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
// if all the fields are filled (not empty)
//insert data to database
$result = mysqli_query($mysqli, "INSERT INTO users(name,age,email) VALUES('$name','$age','$email')");
//display success message
//echo "<font color='green'>Data added successfully.";
//echo "<br/><a href='index.php'>View Result</a>";
}
?>
</body>
</html>
**add.html**
In this file i have included all my js file creating a small table for name, age, email. my method is post. i am unable to do this task first time i am using ajax without ajax i insert data many times.
I am not sure that my data is inserted through ajax or not, can anyone solve my issue.
<html>
<head>
<title>Add Data</title>
<script src="style/jquery-3.2.1.js"></script>
<script src="style/insert.js"></script>
</head>
<body>
<a href="index.php">Home</a>
<br/><br/>
<p id="alert"><span id="show"></span></p>
<table align="center" width="25%" border="0">
<tr>
<td>Name</td>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<td>Age</td>
<td><input type="text" name="age" id="age"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="Submit" id="submit" value="Add"></td>
</tr>
</table>
</body>
</html>
**insert.js**
In this file save input value in variable, my type is POST url is add.php, success function is result, my data is inserted but i am not sure that is this through AJAX or not.
I am not sure that my data is inserted in database through ajax or not, can anyone solve my issue.
$(document).ready(function(e) {
$('#submit').click(function()
{
var name = $('#name').val(); //save input value in variable
var age = $('#age').val();
var email = $('#email').val();
$.ajax({
data :{name:name,age:age,email:email},
url :"add.php", //php page URL where we post this data to save in database
type :'POST',
success: function(data){
alert("Form submitted");
$('#name').val("");
$('#age').val("");
$('#email').val("");
}
})
});
});
我在我的代码中做了很多更改,您的建议和我的承诺可以再次看到我的代码,我在做错误我已附加调试器截图。 –