我的發送按鈕不起作用。不應該腳本帶我去add.php? 但它而不是它刷新頁面?如何和爲什麼?幫助我請 它沒有Ajax腳本工作。Ajax不能使用按鈕
Tommorow是我的實驗考試,我無法讓事情正常工作。
我應該做些什麼改變才能解決問題。
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body>
<a class="btn btn-outline-primary" href="index.php">Home</a>
<br/><br/>
<form method="post" id="form1">
<table width="25%" border="0">
...
...
<tr>
<td><input class="btn btn-primary" type="reset"
name="Clear" value="Clear"></td>
<td><input type="button" value="send" id="asdasd"
name="Submit" /></td>
</tr>
</table>
</form>
<script>
$(document).ready(function(e){
$("#form1").click(function() {
var name=$('#name').val();
var email=$('#email').val();
var age=$('#age').val();
var status=$('#status').val();
$.ajax({
type: "POST",
url: '../crud/add2.php',
data: $('#form1').serialize()
});
});
});
</script>
這裏是我的add.php文件
<body>
<?php
//including the database connection file
include_once("config.php");
if(isset($_POST)) {
$name = mysqli_real_escape_string($mysqli, $_POST['name']);
$age = mysqli_real_escape_string($mysqli, $_POST['age']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$status = mysqli_real_escape_string($mysqli, $_POST['status']);
// checking empty fields
if(empty($name) || empty($age) || empty($email)|| empty($status)) {
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/>";
}
if(empty($status)) {
echo "<font color='red'>Status 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,Status)
VALUES('$name','$age','$email','$status')");
//display success message
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
}
else {
echo "<font color='green'>NOOOOOOO added successfully.";
echo "<br/><a href='index.php'>View Result</a>";
}
?>
</body>
</html>
據我所知,沒有重定向到add.php。你可以在ajax返回輸出後添加它以獲得add.php。 – Redrif
您是否還需要在實驗考試中使用ajax?因爲如果沒有,你可以把行動iside窗體標籤,並將其設置爲add.php – Redrif
是的,我需要使用ajax。它是我的實驗室要求,這個要求在幾分鐘前剛剛發佈。「CRUD WITH AJAX」 –