-1

嗨,我有这个并且工作正常,除了以下问题。

  echo "<form method='post' action=''>";
  echo "  <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
  echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
  echo "</form>";
<A submit button here to send the form data>

问题是如果我在文本框中有一个值,例如John当我提交表单时,我会在文本框中保留原始用户键入的单词Jonn。但是,如果用户键入John Carter,则在提交表单时它只保留John。换句话说,文本只到文本之间的第一个空格。如何保留整个文本?

编辑

<?php
  if(isset($_POST['sendone']))
  {
    echo "Hello";
    echo "<form method='post' action=''>";
    echo "  <input type='text' name='txt1' id='txt1'>";
    echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
    echo "</form>";
  }
  if(isset($_POST['sendtwo']))
  {
    if($_POST['txt1']=='')
    {
      echo "Hello";
      echo "<form method='post' action=''>";
      echo "  <input type='text' name='txt1' id='txt1'>";
      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Empty";
    }
    else
    {
      $_SESSION['txt1'] = $_POST['txt1'];
      echo "Hello";
      echo "<form method='post' action=''>";
      echo "  <input type='text' name='txt1' id='txt1' value=".$_SESSION['txt1'].">";
      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Hit success!";      
    }
  }
  ?>
4

2 回答 2

1

在ingvalue=时,您需要在属性值周围加上单引号,否则生成的 HTML 无效.. 像这样:echo_SESSION["txt1"]

<?php
  if(isset($_POST['sendone']))
  {
    echo "Hello";
    echo "<form method='post' action=''>";
    echo "  <input type='text' name='txt1' id='txt1'>";
    echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
    echo "</form>";
  }
  if(isset($_POST['sendtwo']))
  {
    if($_POST['txt1']=='')
    {
      echo "Hello";
      echo "<form method='post' action=''>";
      echo "  <input type='text' name='txt1' id='txt1'>";
      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Empty";
    }
    else
    {
      $_SESSION['txt1'] = $_POST['txt1'];
      echo "Hello";
      echo "<form method='post' action=''>";

      // check out this line here:
      echo "  <input type='text' name='txt1' id='txt1' value='" . htmlspecialchars($_SESSION['txt1']) . "'>";

      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Hit success!";      
    }
  }
?>

echo不过,在可能的情况下使用纯 HTML 并仅用于您希望包含的变量,它的性能(和可读性!)要高得多,这一点毫无价值。

于 2013-07-06T05:51:11.700 回答
0

检查此代码。

<form name="" method="post">
<input type="text" name="txt1">
<input type="submit" name="sendtwo">
<input type="submit" name="sendone">
</form>

<?php
  if(isset($_POST['sendone']))
  {
    echo "Hello";
    echo "<form method='post' action=''>";
    echo "  <input type='text' name='txt1' id='txt1'>";
    echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
    echo "</form>";
  }
  if(isset($_POST['sendtwo']))
  {
    if($_POST['txt1']=='')
    {
      echo "Hello";
      echo "<form method='post' action=''>";
      echo "  <input type='text' name='txt1' id='txt1'>";
      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Empty";
    }
    else
    {
      $_SESSION['txt1'] = $_POST['txt1'];
      echo "Hello";
      echo "<form method='post' action=''>";

      // check out this line here:
      echo "  <input type='text' name='txt1' id='txt1' value='" . $_SESSION['txt1'] . "'>";

      echo "  <input type='submit' name='sendtwo' id='sendtwo' value='TwoClick'>";
      echo "</form>";
      echo "Hit success!";      
    }
  }
?>
于 2013-07-06T05:35:57.037 回答