Jathin
Jathin

Reputation: 79

The use of back button in php

I am very new to the world of PHP . The problem that I am facing is with the use of back button . When I click on the back button from page2 to page1 the options which I had selected on page1 is not getting saved . So I need to select all the data once again in page1 . I am using sessions . The funny part is when I use a text box question on page 1 and submit its value to page 2 and then click on back button on page2 the data is being saved . Please take a look into this code I named this file as 1.php

<?php
  session_start();
?>
<html>
<body>
<form action="2.php" method="post">

Name<input type="text" name="name" value="<?php if(isset($_SESSION['name'])) echo $_SESSION['name']; ?>"/>


<input type="submit"  value="next"/>
</form>
</body>
</html>

I named this file as 2.php

<?php
session_start();
$name = $_POST['name'];
session_register('name');

?>
<html>
<body>
<form action="1.php" method="post">

<input type="submit"  value="back"/>
</form>
</body>

The problem is that I do not know how to implement this on the radio buttons . Could you please help me out Thanks in advance

Upvotes: 0

Views: 552

Answers (3)

Sandeep Manne
Sandeep Manne

Reputation: 6092

If you need a multi-step form, instead submitting each page at once, you can use css to hide and show the prev and next steps, and submit all data at once, this way it will be faster to user because number of times you talk to server also get reduced.

Check this link to know how to create multi step form using css and js

Upvotes: 2

sephoy08
sephoy08

Reputation: 1114

Try this one,

1.php

<?php
  session_start();
?>
<html>
<body>
<form action="2.php" method="post">

Radio1 <input type="radio" name="name" value="radio1" <?php if($_SESSION['name'] == 'radio1') echo "checked='checked'" ?>/><br/>
Radio2 <input type="radio" name="name" value="radio2" <?php if($_SESSION['name'] == 'radio2') echo "checked='checked'" ?>/><br/>
Radio3 <input type="radio" name="name" value="radio3" <?php if($_SESSION['name'] == 'radio3') echo "checked='checked'" ?>/><br/>

<input type="submit"  value="next"/>
</form>
</body>
</html>

2.php

<?php
session_start();
$name = $_POST['name'];
$_SESSION['name'] = $name;

?>
<html>
<body>
<form action="1.php" method="post">

<input type="submit"  value="back"/>
</form>
</body>

Upvotes: 1

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

With radio buttons, you figure out which one was selected (based on value), and add the selected property to it.

However, Sandeep's answer above is better if you can use it.

Upvotes: 0

Related Questions