Illusionist
Illusionist

Reputation: 5509

php and html form on the same page

I am learning php. I am passing some values from one page to another page, and letting the user enter form into this page and then storing into the database.

I would like to stay on the same page, so instead of this first page-

<form method="post" action="update.php">
   <input type="text" name="name" value="value1" />
</form>

update.php

<?php
   $name= $_POST['name'];
?>

I want to be on the same page because i am getting some variables and arrays from a previous page with get()

<?php 

$count= $_GET['count'];
$sum= $_GET['Sum'];
for ($i=0;$i<$count;$i++){
echo unserialize($_GET['serialized_name'])[$i];
?>

Since I also need to send the form values so i am not sure how i can pass the values i am getting to the next page- which is why i was hoping to be on the same page instead of going to update.php.

Upvotes: 0

Views: 7718

Answers (3)

sven
sven

Reputation: 785

    <?php 
       @session_start();
       $_session['count']= $_GET['count'];
       $_session['sum']= $_GET['Sum'];
       for ($i=0;$i<$_session['count'];$i++){

          //make necessary changes here as well
         echo unserialize($_GET['serialized_name'])[$i];

         //use session to store your data from previous page
       ?>
     <?php
         //put this code above the form to process the submitted data which was previously sent to update.php

      if(isset($_POST[submit])){
                //Your code
        e.g.
              $name=$_POST['name']
           //whenever you want to access previous data just get it from session variable.
         e.g. $count=$_SESSION['count'];

      }?>

     <html>
            <!--Submit the data of the form to itself instead of update.php -->
       <form method="post" action="<?php echo $PHP_SELF;?>">
           <!--your html code -->
       </form>
     </html>

Upvotes: 1

Rimu Atkinson
Rimu Atkinson

Reputation: 777

You can pass more data over to update.php by using hidden fields just above your <input type=submit.

e.g. <input type="hidden" name="some_data" value="<?php echo $some_data; ?>" />

Of course, any web visitor can see this data if they use their browser's "View source" feature, so only do this with data that cannot cause a security problem.

Then, in update.php, you can access that data by doing something like $some_data = $_POST["some_data"]

Upvotes: 1

ewein
ewein

Reputation: 2735

Try this, put all this code in the page you want to stay on (I am saying it is update.php here):

<?php
    if($_POST['submit_button'] == "Submit")
    {
        $name= $_POST['name'];
    }
?>

    <form method="post" action="update.php">
       <input type="text" name="name" value="value1" />
       <input type="submit" name="submit_button" value="Submit"/>
    </form>

Upvotes: 1

Related Questions