How to transfer value from one php page to another using session

I can pass values ​​from one page to another, but I need to pass a value like this,

Page 1:

Page4.php

Page3.php

I need to pass the value in the text box in Page1.php to the text box in Page2.php, since the form is not directly redirected to page2, I can’t pass the value, I tried the session, the form is the mail method and several other methods, but I haven’t achieved success.

I would be very happy if you could help me with the code or some suggestions.

Thanks!

Edit ..........

I found the answer, thanks for the help, in fact it was a careless error on my part, I used $ _post instead of $ _session.

Now it works.

Thanks for the help.

+4
source share
2 answers

Use something like this:

page1.php,

<?php session_start(); $_SESSION['myValue']=3; // You can set the value however you like. ?> 

Any other PHP page:

 <?php session_start(); echo $_SESSION['myValue']; ?> 

A few notes to keep in mind: you need to call session_start() BEFORE any output, HTML, echos - even white space characters.

You can continue to change the value in the session - but it can only be used after the first page - this means that if you install it on page 1, you cannot use it until you get to another page or refresh the page.

Setting the variable itself can be done in one of several ways:

 $_SESSION['myValue']=1; $_SESSION['myValue']=$var; $_SESSION['myValue']=$_GET['YourFormElement']; 

And if you want to check if a variable is set before getting a potential error, use something like this:

 if(!empty($_SESSION['myValue']) { echo $_SESSION['myValue']; } else { echo "Session not set yet."; } 
+7
source

A solution using only POST - no $ _SESSION

page1.php,

 <form action="page2.php" method="post"> <textarea name="textarea1" id="textarea1"></textarea><br /> <input type="submit" value="submit" /> </form> 

page2.php

 <?php // this page outputs the contents of the textarea if posted $textarea1 = ""; // set var to avoid errors if(isset($_POST['textarea1'])){ $textarea1 = $_POST['textarea1'] } ?> <textarea><?php echo $textarea1;?></textarea> 

Solution using $ _SESSION and POST

page1.php,

 <?php session_start(); // needs to be before anything else on page to use $_SESSION $textarea1 = ""; if(isset($_POST['textarea1'])){ $_SESSION['textarea1'] = $_POST['textarea1']; } ?> <form action="page1.php" method="post"> <textarea name="textarea1" id="textarea1"></textarea><br /> <input type="submit" value="submit" /> </form> <br /><br /> <a href="page2.php">Go to page2</a> 

page2.php

 <?php session_start(); // needs to be before anything else on page to use $_SESSION // this page outputs the textarea1 from the session IF it exists $textarea1 = ""; // set var to avoid errors if(isset($_SESSION['textarea1'])){ $textarea1 = $_SESSION['textarea1'] } ?> <textarea><?php echo $textarea1;?></textarea> 

ATTENTION!!! - It does not contain validation !!!

+3
source

Source: https://habr.com/ru/post/1436326/


All Articles