0

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.

Illusionist
  • 5,204
  • 11
  • 46
  • 76

3 Answers3

1

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>
ewein
  • 2,695
  • 6
  • 36
  • 54
  • Thanks, but it does not seem to work for me. Perhaps i did not understand you answer properly - the if($_POST['submit_button'] == "Submit") is supposed to be on the first page right, and I can put all my update.php code into this php block? – Illusionist Dec 09 '12 at 02:23
  • but be sure to change the action in the form to be the page you are on instead of update.php – ewein Dec 09 '12 at 02:29
  • thanks, but it doesn't work - the first problem is that i was getting variables from the previous page on the page i am on, which i can perhaps get around by using isset()? but anyway , the mysql code i am using was storing values on update.php page, but its not now. – Illusionist Dec 09 '12 at 02:32
1

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"]

Rimu Atkinson
  • 775
  • 4
  • 15
1
    <?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>
sven
  • 775
  • 5
  • 14