我在单独的文档中有我的HTML代码和PHP代码,因此HTML包含文本框等,而PHP文件包含实际的mySql命令,用于将条目写入数据库。我想拥有它,以便在PHP代码执行后弹出一个对话框,说“您的输入成功”,然后导航回到主页,或者回到我刚来自的页面。我的HTML表单如下
<form action = "submitEvent.php" method = "post">
Event Name: <br>
<input type = "text" name = "eventname" >
<br>
Event Type: <br>
<input type = "text" name = "eventtype" >
<br>
<!-- need to find a way to get charityID to link the tables
without having the user enter the charityID -->
Charity Number: <br>
<input type = "text" name = "charityid" value="<?php echo $charityid;?>" >
<br>
Contact Details: <br>
<input type = "text" name = "contactdetails" >
<br>
Location: <br>
<input type = "text" name = "eventlocation" >
<br>
Date : <br>
<input type ="date" name ="eventdate">
<br>
<input type = "submit" value = "Submit">
</form>
include 'conn.php';
$eventname = filter_input(INPUT_POST, 'eventname');
$eventtype = filter_input(INPUT_POST, 'eventtype');
$charitynumber = filter_input(INPUT_POST, 'charityid');
$contactdetails = filter_input(INPUT_POST, 'contactdetails');
$eventlocation = filter_input(INPUT_POST, 'eventlocation');
$eventdate = filter_input(INPUT_POST, 'eventdate');
//Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "INSERT
INTO event (eventname, eventtype, charityid, contactdetails, location, date) VALUES ('$eventname', '$eventtype', '$charitynumber', '$contactdetails', '$eventlocation', '$eventdate')";
if ($conn->query($sql) === TRUE) {
header("location: index.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
此刻,它只是导航回到主页,但我也希望在主页上或事先出现一条通知,以便用户知道记录已成功创建。
答案 0 :(得分:0)
您需要使用ajax请求和jQuery
<form method = "post" id="event_form">
Event Name: <br>
<input type = "text" name = "eventname" >
<br>
Event Type: <br>
<input type = "text" name = "eventtype" >
<br>
<!-- need to find a way to get charityID to link the tables
without having the user enter the charityID -->
Charity Number: <br>
<input type = "text" name = "charityid" value="<?php echo $charityid;?>" >
<br>
Contact Details: <br>
<input type = "text" name = "contactdetails" >
<br>
Location: <br>
<input type = "text" name = "eventlocation" >
<br>
Date : <br>
<input type ="date" name ="eventdate">
<br>
<input type = "submit" value = "Submit">
</form>
<script>
$(document).ready(function (){
$("event_form").submit(function (event){
event.preventDefault();
$.ajax({ // create an AJAX call...
data: $(this).serialize(), // get the form data
type: $(this).attr('method'), // GET or POST
dataType: "json",
url: 'submitEvent.php', // the file to call
success: function(responce) { // on success..
alert('Your entry was successfully processed');
window.location='index.php' // write the url of your page that you want to navigate to
}
});
return false; // cancel origin
});
});
</script>
//use cdn google or microsoft
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
有关更多信息,请访问jquery Documentation
答案 1 :(得分:0)
或者您可以将消息保存在会话中,然后在重定向页面说
if(isset($_SESSION['message'])) echo $_SESSION['message']
完成后别忘了取消设置