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.

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

Comments

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>

3 Comments

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?
but be sure to change the action in the form to be the page you are on instead of update.php
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.
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"]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.