Html forms and PHP variables problem

  • Pbpixels
  • Novice
  • Novice
  • User avatar
  • Posts: 17

Post 3+ Months Ago

Hello. I have a piece of code that I want to talk together.
This variable:
PHP Code: [ Select ]
$fields = $_POST['fields'];
 
  1. $fields = $_POST['fields'];
  2.  

I have a field and a submit button number 1.:
PHP Code: [ Select ]
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <table>
      <tr>
         <td><label for=""><p>Fields:</p></label></td>
         <td><input type="text" size="30" name="fields" value="<?php echo $fields; ?>" /></td>
      </tr>
   </table>
   <input type="submit" value="Add" name="submit1" />
</form>
 
  1. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  2.    <table>
  3.       <tr>
  4.          <td><label for=""><p>Fields:</p></label></td>
  5.          <td><input type="text" size="30" name="fields" value="<?php echo $fields; ?>" /></td>
  6.       </tr>
  7.    </table>
  8.    <input type="submit" value="Add" name="submit1" />
  9. </form>
  10.  


After the button is pressed, it remembers the variable "fields" which the user just typed. Let's say he typed 5.
So now the $fields = 5.

Now I do like to make another button, but it has to remember what the user typed inside fields.

PHP Code: [ Select ]
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="submit" value="Create" name="submit2" />
</form>
<?php
if (isset($_POST['submit2'])) {
echo "hey $fields";
?>
 
  1. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  2.    <input type="submit" value="Create" name="submit2" />
  3. </form>
  4. <?php
  5. if (isset($_POST['submit2'])) {
  6. echo "hey $fields";
  7. ?>
  8.  


But after I press on the secound submit button, it did not remember what the user typed in the beginning? Why's that and how can I make it remember it when I press submit2?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Change your other second submit form to this it will work.

PHP Code: [ Select ]
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="submit" value="Create" name="submit2" />
   <input type="hidden" size="30" name="fields" value="<?php echo $fields; ?>" />
</form>
<?php
if (isset($_POST['submit2'])) {
echo "hey $fields";
?>
 
  1. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  2.    <input type="submit" value="Create" name="submit2" />
  3.    <input type="hidden" size="30" name="fields" value="<?php echo $fields; ?>" />
  4. </form>
  5. <?php
  6. if (isset($_POST['submit2'])) {
  7. echo "hey $fields";
  8. ?>
  9.  
  • Pbpixels
  • Novice
  • Novice
  • User avatar
  • Posts: 17

Post 3+ Months Ago

Wow that was a really simple solution, I was searching for returns and superglobals etc. Thanks for your help, it really helped me out and it works perfectly! :)
  • Satwant
  • Graduate
  • Graduate
  • User avatar
  • Posts: 127
  • Loc: Bangalore

Post 3+ Months Ago

Welcome :)

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 101 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.