我在按下提交后在文本字段中保留文本时遇到问题。我正在使用这个:
<input name="date" type="text" id="date" <?php if(isset($_POST['date'])){echo 'value="'.$_POST['date'].'"';} ?>/>
我也成功提交了这个,以便重新加载表格,但也删除了上面的内容,因为它很新鲜。
echo "Succesfully added transaction. Updating table...";
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"6\">";
我的整个代码=
<?php
// if ('POST' === $_SERVER['REQUEST_METHOD'])
if (isset($_POST['submit2']))
{
$con = mysql_connect("xx","xx_xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xx_xx", $con);
$date = $_POST['date'];
$_SESSION['date'] = $_POST;
//Writes the to the server
$sql = mysql_query("INSERT INTO `reservation__date` (`reservation_id`, `reservation_date`)
VALUES
('000', '$date')") or die(mysql_error());
$query = mysql_query($sql);
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$_SESSION['DATE'] = $_REQUEST['date'];
echo "Succesfully added transaction. Updating table...";
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"6\">";
mysql_close($con);
}
}
?>
答案 0 :(得分:0)
如果您遵循PRG原则(POST-redirect-GET),则需要在会话中跟踪POST数据。
我假设你在提交后做了类似的事情:
if (POST is fine) {
do something;
redirect;
} else {
re-render page and show feedback
}
由于服务器将丢弃重定向上的任何POST数据,因此您需要在每次提交时跟踪POST数据,并在页面加载(GET)时重新填充POST数据。
$_SESSION['user_POST'] = $_POST;
if (POST is fine) ...
在包含要提交的表单的页面上:
if (isset($_SESSION['user_POST']) {
$_POST = $_SESSION['user_POST'];
}
忽略语法,我不是那么多PHP。
答案 1 :(得分:0)
这是您的要求
<?php
session_start();
if(isset($_REQUEST['date'])){
$_SESSION['date'] = $_REQUEST['date'];
echo "Succesfully added transaction. Updating table...";
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"6\">";
}
?>
<form method="post">
<input name="date" type="text" id="date" value="<?php echo $_SESSION['date'];?>" />
<input type="submit" name="submit" />
</form>
答案 2 :(得分:0)
FOR TESTING
<?php echo $_POST['date']; ?>
如果以上是正确的话,尝试设置为等于
<input name="date" type="text" id="date" <?php if(isset($_POST['date']) === true){echo 'value="'.$_POST['date'].'"';} ?>/>
答案 3 :(得分:0)
如果您正在使用会话变量..请在页面顶部使用session_start();
..
检查表单中是否有 method =“POST”,因为默认为GET ..
检查天气是否有<form action="abc.php">
如果要将文本放入值中,请更好地检查value属性中的条件并回显该值。因为即使您没有在HTML插入标记中写入值,也需要value=""
。这会在以后造成模棱两可的情况。
做得更好
<form action="abc.php" method="post" >
<input name="date" type="text" id="date" value="<?php if(isset($_POST['date'])){echo $_POST['date'];} ?>" />
.......................
</form
现在在abc.php
<?php
session_start(); // as you are using session variables..
if (isset($_POST['submit2']))
{
$con = mysql_connect("xx","xx_xx","xx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("xx_xx", $con);
$date = $_POST['date'];
$_SESSION['date'] = $_POST['date']; // POST is an array carrying many elements so be specific which element you want. inthis case it is $_POST['date'];
//Writes the to the server
$sql = mysql_query("INSERT INTO `reservation__date` (`reservation_id`, `reservation_date`) VALUES ('000', '$date')") or die(mysql_error());
$query = mysql_query($sql);
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$_SESSION['DATE'] = $_REQUEST['date'];
echo "Succesfully added transaction. Updating table...";
echo "<META HTTP-EQUIV=\"refresh\" CONTENT=\"6\">";
mysql_close($con);
}
}
?>
希望这会对你有所帮助......