表单对刷新感到不满 [英] Form get's resent on refresh
问题描述
表单获取刷新刷新,我读过关于 header(Location:my_page.php)
unset($ _ POST)
,但我不知道该把它放在哪里。
这是我们的脚本,它可以根据需要工作,但它在页面刷新时不断重新发送(Chrome浏览器警报一遍又一遍),有人能修复代码并解释给我像2岁的孩子。
< form action ='thi_very_same_page.php'method ='post'> ;
搜寻基督教电影< input type ='text'name ='query'id ='text'/>
< input type ='submit'name ='submit'id ='search'value ='Search'/>
< / form>
<?php
if(isset($ _ POST ['submit']))
{
mysql_connect(localhost,root,toor )或死(连接到数据库的错误:。mysql_error());
mysql_select_db(db_name)或者死(mysql_error());
$ query = $ _POST ['query'];
$ min_length = 2;
if(strlen($ query)> = $ min_length)
{
$ query = htmlspecialchars($ query);
$ query = mysql_real_escape_string($ query);
echo;
$ result = mysql_query(SELECT *,DATE_FORMAT(lasteditdate,'%m-%d-%Y')AS lasteditdate FROM movies WHERE(`moviename` LIKE'%。$ query。 %')OR(`year` LIKE'%。$ query。%'))or die(mysql_error());
if(mysql_num_rows($ result)> 0)
{
while($ results = mysql_fetch_array($ result))
{
echo ;
}
}
else
{
echo;
}
}
else
{
echo;
}
}
问题在于你使用 post
方法提交表单值,所以当你试图刷新浏览器时询问你是否发送表单信息,这是默认行为的浏览器来解决发布的信息,你的问题的替代解决方案是你可以使用 get
方法,如表单属性 method ='get'
它执行的操作会在URL中追加所有我们称之为查询字符串
的信息,并且在php代码中访问表单值在 $ _ POST
中,但是当使用get方法时,表单值现在将出现在 $ _ GET
方法中,这些方法称为请求方法和PHP的全局变量,现在当你尝试刷新它不会要求你重新发送信息,因为信息现在驻留在url中。 $ c>< form action ='thi_ very_same_page.php'method ='get'>
搜寻基督教电影< input type ='text'name ='query'id ='text'/>
< input type ='submit'name ='submit'id ='search'value ='Search'/>
< / form>
<?php
if(isset($ _ GET ['submit']))
{
mysql_connect(localhost,root,toor )或死(连接到数据库的错误:。mysql_error());
mysql_select_db(db_name)或者死(mysql_error());
$ query = $ _GET ['query'];
$ min_length = 2;
if(strlen($ query)> = $ min_length)
{
$ query = htmlspecialchars($ query);
$ query = mysql_real_escape_string($ query);
echo;
$ result = mysql_query(SELECT *,DATE_FORMAT(lasteditdate,'%m-%d-%Y')AS lasteditdate FROM movies WHERE(`moviename` LIKE'%。$ query。 %')OR(`year` LIKE'%。$ query。%'))or die(mysql_error());
if(mysql_num_rows($ result)> 0)
{
while($ results = mysql_fetch_array($ result))
{
echo ;
}
}
else
{
echo;
}
}
else
{
echo;
}
}?>
希望这足以向您解释关于表单提交的一件事,我建议您深入研究一下以下
请不要在新代码中使用
mysql_ *
函数 。他们不再维护并被正式弃用。请参阅 红色框 ?了解 准备语句 ,并使用 PDO 或 MySQLi - 这篇文章将帮助你决定哪个。如果您选择PDO,这是一个很好的教程。
Form get's resent on refresh, I had read about header("Location:my_page.php")
unset($_POST)
, but I'm not sure where to place it.
This is our script, it works as need it, but it keeps re-sending on page refresh (Chrome browser alerts over and over), can some one fix the code and explain to my like 2 years old child.
<form action='thi_very_same_page.php' method='post'>
Search for Christian Movies <input type='text' name='query' id='text' />
<input type='submit' name='submit' id='search' value='Search' />
</form>
<?php
if (isset($_POST['submit']))
{
mysql_connect("localhost", "root", "toor") or die("Error connecting to database: " . mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$query = $_POST['query'];
$min_length = 2;
if (strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
echo "";
$result = mysql_query("SELECT *, DATE_FORMAT(lasteditdate, '%m-%d-%Y') AS lasteditdate FROM movies WHERE (`moviename` LIKE '%" . $query . "%') OR (`year` LIKE '%" . $query . "%')") or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($results = mysql_fetch_array($result))
{
echo "";
}
}
else
{
echo "";
}
}
else
{
echo "";
}
}
The problem is you are using the post
method to submit the form values so when ever you tries to refresh the browser asks you whether to send the form information or not it is the default behavior of the browser to tackle the posted information, the alternate solution for your problem is you can use the get
method like in form attribute method='get'
what it does it will append all the information of form in the url which we call the query string
and in php code you are accessing the form values in $_POST
but when using get method the form values will now appear in the $_GET
method these methods are called request method and are php's global variables, Now when you try to refresh it will not ask you to resend information because the information now resides in the url
<form action='thi_very_same_page.php' method='get'>
Search for Christian Movies <input type='text' name='query' id='text' />
<input type='submit' name='submit' id='search' value='Search' />
</form>
<?php
if (isset($_GET['submit']))
{
mysql_connect("localhost", "root", "toor") or die("Error connecting to database: " . mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$query = $_GET['query'];
$min_length = 2;
if (strlen($query) >= $min_length)
{
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
echo "";
$result = mysql_query("SELECT *, DATE_FORMAT(lasteditdate, '%m-%d-%Y') AS lasteditdate FROM movies WHERE (`moviename` LIKE '%" . $query . "%') OR (`year` LIKE '%" . $query . "%')") or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
while ($results = mysql_fetch_array($result))
{
echo "";
}
}
else
{
echo "";
}
}
else
{
echo "";
}
} ?>
Hope this is enough to explain you about the form submission one thing i will suggest you to deeply look at below
Please, don't use
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
这篇关于表单对刷新感到不满的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!