-1

Hi I have this and works fine except the following issue.

  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>

The issue is if I have a value in the text box like John when I submit the form I retain the original user typed word Jonn in the text box. However if the user type John Carter, when the form is submitted it retains only John. In other words texts only up to first space between the texts. How do I retain entire text?

EDIT

<?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!";      
    }
  }
  ?>
msturdy
  • 9,343
  • 8
  • 38
  • 52
John English
  • 121
  • 1
  • 5
  • 12

2 Answers2

1

You need to include the single quotes around the value for the value= attribue when echoing the _SESSION["txt1"], otherwise the resulting HTML isn't valid .. like this:

<?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!";      
    }
  }
?>

Though, it's worth nothing that it is much more performant (and readable!) to use plain HTML where possible, and just use echo for the variables you wish to include.

msturdy
  • 9,343
  • 8
  • 38
  • 52
0

Check this code.

<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!";      
    }
  }
?>
  • can you post the code from which the values are generated for sendone and sendtwo –  Jul 06 '13 at 05:38
  • Assume form one has only a submit button. The form 2 content is in my question under edit. Thank you for looking. – John English Jul 06 '13 at 05:49
  • you are using sendtwo and sendone ie there have to be two submit buttons in form one.. –  Jul 06 '13 at 06:01
  • I have updated the code, please check if it works for you. I assume you have 2 submit buttons in form one. ` ` –  Jul 06 '13 at 06:05