问题描述
<?php
if ($_POST['submit']) {
mysql_connect ("localhost", "root", "swt") or die ('Error: ' . mysql_error());
mysql_select_db("db") or die ('Data error:' . mysql_error());
$text = mysql_real_escape_string($_POST['comments']);
$query="INSERT INTO greetings (msg) VALUES ('$text')";
mysql_query($query) or die ('Error updating database' . mysql_error());
$id= mysql_insert_id();
$url = "preview.php?id=".$id;
}
?>
<form method="post" action="<? echo $url ?>" enctype="multipart/form-data" >
<textarea name="comments" placeholder="please input your message"></textarea>
<input name="submit" type="submit" value="submit" />
</form>
您好,对不起,我是PHP的新手.我想问一问,为什么我提交时必须先按2次才能进入Preview.php
hello, sorry im newbie in PHP.i want to ask, why when i submit it must takes 2 times pressed before go to the preview.php
谢谢.
推荐答案
第一次$url
为空,以便浏览器请求同一页面,然后更改$ url,然后注入表单,以便下一篇文章将重定向到您的Preview.php文件.
刚发送的标头用于重定向.
First time the $url
is empty so the browser is requesting same page, then the $url is changed, then injected to form so the next post will redirect to your preview.php file.
Just sent header for redirect.
header("Location: /preview.php?id=".$id);
因此它将是:
<?php
if ($_POST['submit']) {
mysql_connect ("localhost", "root", "swt") or die ('Error: ' . mysql_error());
mysql_select_db("db") or die ('Data error:' . mysql_error());
$text = mysql_real_escape_string($_POST['comments']);
$query="INSERT INTO greetings (msg) VALUES ('$text')";
mysql_query($query) or die ('Error updating database' . mysql_error());
$id= mysql_insert_id();
$url = "preview.php?id=".$id;
header("Location: $url");
}
?>
<form method="post" enctype="multipart/form-data" >
<textarea name="comments" placeholder="please input your message"></textarea>
<input name="submit" type="submit" value="submit" />
</form>
当您要将请求发送到同一页面时,请不要使用操作.
(假设您的代码段中的php部分是同一文件)
Dont use action when you are wanting to send the request to the same page.
(Im assuming the php part in your snippet is the same file)
另外,要发送重定向,您在发送标题之前不需要html输出
Also to send the redirect you need to not have html output before sending the header
这篇关于在PHP中,为什么输入表单提交时需要按2次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!