0

我在推送提交后将文本保留在先前存在的文本字段中时遇到问题。我正在使用这个:

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

?>
4

4 回答 4

0
  1. 如果您正在使用会话变量..session_start();在页面顶部使用..

  2. 检查表单中是否有method="POST"因为默认情况下它的 GET ..

  3. 检查天气您有正确的表单操作字段,例如<form action="abc.php">

  4. 如果要将文本放入 value,最好检查 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);
}
}

?>

希望对你有帮助...

于 2013-08-17T22:13:01.020 回答
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>
于 2013-08-17T20:56:31.323 回答
0

用于检测

<?php  echo $_POST['date']; ?>

如果以上正确,请尝试设置为 true

<input name="date" type="text" id="date" <?php if(isset($_POST['date']) === true){echo 'value="'.$_POST['date'].'"';} ?>/>
于 2013-08-17T20:56:33.777 回答
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。

于 2013-08-17T20:40:42.113 回答