【问题标题】:Navigation after PHP form executesPHP 表单执行后的导航
【发布时间】:2019-03-04 13:07:50
【问题描述】:

我的 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> 
submitEvent.php 文件如下:

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();

目前它只是导航回主页,但我还希望在主页上或之前出现通知,以便用户知道记录已成功创建。

【问题讨论】:

    标签: javascript php html mysql


    【解决方案1】:

    你需要使用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

    【讨论】:

      【解决方案2】:

      或者您可以将消息保存在会话中,然后在重定向页面上说

      if(isset($_SESSION['message'])) echo $_SESSION['message']
      

      完成后别忘了取消设置

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-03
        相关资源
        最近更新 更多