PHP SESSION AND FIELD VALIDATION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ziaul1234
Forum Newbie
Posts: 7
Joined: Thu Sep 16, 2010 4:52 pm

PHP SESSION AND FIELD VALIDATION

Post 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.......
Last edited by califdon on Sun Sep 19, 2010 6:27 pm, edited 1 time in total.
Reason: Added syntax tags to make php code readable
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP SESSION AND FIELD VALIDATION

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: PHP SESSION AND FIELD VALIDATION

Post by McInfo »

Problems like this can be debugged by looking at the page source in the browser.

Duplicate post: 121355
ziaul1234
Forum Newbie
Posts: 7
Joined: Thu Sep 16, 2010 4:52 pm

Re: PHP SESSION AND FIELD VALIDATION

Post by ziaul1234 »

Thank you.
I has worked.
Post Reply