简体   繁体   English

运行php代码后如何不重定向到空白页?

[英]How to not redirect to blank page after running php code?

I have a form to add email into my database. 我有一个将电子邮件添加到数据库中的表格。 This is the HTML: 这是HTML:

<form action="/store_email_db.php" method="POST">
  <input type="email" class="control" placeholder="Enter your email" required="required" name="customermail">
  <button class="bttn-white active" type="submit"><span class="lnr lnr-location"></span> Subscribe</button>
  <label class="mt10" for="mc-email"></label>
</form>

And this is my php code (in another file) to add it into my database: 这是我的php代码(在另一个文件中),将其添加到我的数据库中:

$sql = "INSERT INTO contacts(email, insert_date) VALUES('$email', Now())";
if ($conn->query($sql) === TRUE) {
  echo "New record created successfully"; 
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

And I get this blank page: 我得到这个空白页:

空白页

What I would like is to change the html label value below the email box in the main page instead without redirecting to a blank page : 我想要的是更改主页电子邮件框下方的html标签值,而不是重定向到空白页: 在此处输入图片说明

Seems like it is some verbose mode in your database driver, which is give you "Connected Successfully". 看来这是数据库驱动程序中的一些详细模式,它使您“成功连接”。 Disable it, and after that, read something about ajax requests. 禁用它,然后,阅读有关ajax请求的内容。 With ajax you can get info from PHP without refreshing the page. 使用ajax,无需刷新页面即可从PHP获取信息。

The action inside of the form redirects to whatever page you put there, and then it is echoing the appropriate stuff on that page. action的形式的内部重定向到你把任何网页那里,然后它呼应的是页面上的相应的东西。 There are a couple different things you can do instead to display the message 您可以执行几项不同的操作来显示消息

  1. I usually put the php code at the top of my file, and then put the same file in the action of the form. 我通常将php代码放在文件的顶部,然后将同一文件放在表单的action中。 Here is what it might look like after this is done: 完成后,可能会是这样:

     <?php session_start(); if(isset($_SESSION['msg'])) { unset($_SESSION['msg']); } if(isset($_POST['submit'])) { $sql = "INSERT INTO contacts(email, insert_date) VALUES('$email', Now())"; if ($conn->query($sql) === TRUE) { $_SESSION['msg'] = "New record created successfully"; } else { $_SESSION['msg'] = "Error: " . $sql . "<br>" . $conn->error; } } ?> <form action="same_file.php" method="POST"> <input type="email" class="control" placeholder="Enter your email" required="required" name="customermail"> <button class="bttn-white active" type="submit" name="submit"><span class="lnr lnr- location"></span> Subscribe</button> <label class="mt10" for="mc-email"></label> <div> <?php echo $_SESSION['msg'] ?> </div> </form> 

    A couple things to note: the button has to have the submit name associated with it; 需要注意的几件事:按钮必须具有与之关联的submit名称; the first if statement in the php is used for unsetting the message if there is one when the user visits the page. 如果用户访问页面时有一条消息,则使用php中的第一个if语句取消设置消息。

  2. OR you can make an ajax request for store_email_db.php when the user clicks the submit button, and display the response in a dynamically created div, but I feel like this method is a bit more difficult to implement. 或者,您可以在用户单击“提交”按钮时向store_email_db.php发出ajax请求,并在动态创建的div中显示响应,但是我觉得这种方法难以实现。

This code is untested, but I have used something just like it in a website I built so it should work. 该代码未经测试,但是我在自己建立的网站中使用过类似的代码,因此它应该可以正常工作。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM