Page 1 of 1

PHP SESSION AND FIELD VALIDATION

Posted: Sun Sep 19, 2010 6:21 pm
by ziaul1234
Hi
I am just stucked. Would be great if someone can help me.
I have created form. Has also created a session in php to view the data and reuse it for editting any data. While trying to edit all the default values which are inputed in the form are coming in all fields except the value for radio button(no value is checked)

I am giving a short coding for all 3 pages:
Page 1: (HTML- FORM)

Code: Select all

<html>
<body>
<form action="page2.php" method="post">
Name : <input type="text" name="name"/>
Discount: <input type="radio" name="discount" value="2"> TWO </br>
<input type="radio" name="discount" value="4"> THREE
<input type="submit" value="Submit"/>
</form>
</body>
</html>
PAGE 2 (Displaying Input Values)

Code: Select all

<html>
<?php session_start();?>
<body>
<form action="page3.php" method="post">
Name :<?php
$name=$_POST['name'];
$_SESSION['name']=$name;
echo $_SESSION['name']; ?>

Discount:<?php
$discount=$_POST['discount'];
$_SESSION['discount']=$discount;
echo $_SESSION['discount']; ?>

<input type="Submit" value="Edit">
</form>
</body>
</html>
Page 3 (editing field with prefilled fields) The problem is in this section:

Code: Select all

<html>
<?php session_start();?>
<head>
<?php
$name=$_SESSION['name'];
$discount=$_SESSION['discount'];
echo
"<form action=\"page4.php\" method=\"POST\">
Name :<input type=\"text\" name=\"name\" value=\"$name\"/> </br>
<input type=\"radio\" name=\"discount\" value=\"2\"<?php if($discount=='2') echo 'checked=\"checked\"'; ?> TWO
<br/>
<input type=\"radio\" name=\"discount\" value=\"4\" <?php if($discount=='4') echo 'checked=\"checked\"'; ?> FOUR
<br/>
<input type=\"submit\" value=\"Confirmed\"/>";
?>
</body>
</html>
When I am clicking Edit button on page2, page3 is appearing where name field is prefilled but no radio buttons are checked. I need help for the radio button to be checked according to the information given on first page.
Also, it will be a great help if someone can tell me how to validate the fields on page3 before processing to page4.

THank you ver very much.......

Re: PHP SESSION AND FIELD VALIDATION

Posted: Sun Sep 19, 2010 7:02 pm
by Jonah Bron
The problem is in page three. You cannot put PHP inside of a string, because it's not evaluated. Anything you echo is outputted literally. You need to change it to this:

Code: Select all

<?php session_start();?>
<html>
<head>
<?php
$name=$_SESSION['name'];
$discount=$_SESSION['discount'];
?>
<form action="page4.php" method="POST">
Name :<input type="text" name="name" value="$name"/> </br>
<input type="radio" name="discount" value="2" <?php if($discount=='2') echo 'checked="checked"'; ?>/> TWO
<br/>
<input type="radio" name="discount" value="4" <?php if($discount=='4') echo 'checked="checked"'; ?>/> FOUR
<br/>
<input type="submit" value="Confirmed" />
</body>
</html>
And as I did here, you need to change the other two files to put the session_start() at the top of the file.

Re: PHP SESSION AND FIELD VALIDATION

Posted: Sun Sep 19, 2010 7:07 pm
by McInfo
Problems like this can be debugged by looking at the page source in the browser.

Duplicate post: 121355

Re: PHP SESSION AND FIELD VALIDATION

Posted: Mon Sep 20, 2010 10:43 am
by ziaul1234
Thank you.
I has worked.