Recalling Session Value for Radio Buttons

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

Recalling Session Value for Radio Buttons

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)
<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)
<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:
<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 ziaul1234 on Sun Sep 19, 2010 6:17 pm, edited 1 time in total.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Recalling Session Value for Radio Buttons

Post by McInfo »

The code you posted is invalid for many reasons that I care not to enumerate.

Your problem may be related to the fact that these inputs are not submitted by the browser and will, therefore, not have an index in $_POST:
  • Unchecked radio buttons
  • Unchecked checkboxes
  • Inputs without names
  • Buttons that were not clicked
Play around with this example to learn what happens under the circumstances I listed above.

form-demo.php

Code: Select all

<!DOCTYPE HTML>
<html>
    <head>
        <title>Form Demo</title>
        <style type="text/css">
            * {
                font-size: small;
            }
            p {
                margin: 0;
                padding: 0;
            }
        </style>
    </head>
    <body>
        <form method="post" action="">
            <fieldset>
                <legend>Radio Buttons</legend>
                <fieldset>
                    <legend>Group 1 "rad1"</legend>
                    <p>
                        <label>
                            <span>Choice 1</span>
                            <input type="radio" name="rad1" value="1" />
                        </label>
                    </p>
                    <p>
                        <label>
                            <span>Choice 2</span>
                            <input type="radio" name="rad1" value="2" />
                        </label>
                    </p>
                    <p>
                        <label>
                            <span>Choice 3</span>
                            <input type="radio" name="rad1" value="3" />
                        </label>
                    </p>
                </fieldset>
                <fieldset>
                    <legend>Group 2 "rad2"</legend>
                    <p>
                        <label>
                            <span>Choice 1</span>
                            <input type="radio" name="rad2" value="1" />
                        </label>
                    </p>
                    <p>
                        <label>
                            <span>Choice 2</span>
                            <input type="radio" name="rad2" value="2" />
                        </label>
                    </p>
                    <p>
                        <label>
                            <span>Choice 3</span>
                            <input type="radio" name="rad2" value="3" />
                        </label>
                    </p>
                </fieldset>
            </fieldset>
            <fieldset>
                <legend>Checkboxes</legend>
                <p>
                    <label>
                        <span>Option 1 "chk1"</span>
                        <input type="checkbox" name="chk1" value="1" />
                    </label>
                </p>
                <p>
                    <label>
                        <span>Option 2 "chk2"</span>
                        <input type="checkbox" name="chk2" value="1" />
                    </label>
                </p>
                <p>
                    <label>
                        <span>Option 3 "chk3"</span>
                        <input type="checkbox" name="chk3" value="1" />
                    </label>
                </p>
                <p>
                    <label>
                        <span>No-name Option</span>
                        <input type="checkbox" value="1" />
                    </label>
                </p>
            </fieldset>
            <fieldset>
                <legend>Buttons</legend>
                <input type="reset" name="reset" value="Reset" />
                <input type="submit" name="submit" value="Submit" />
            </fieldset>
            <fieldset>
                <legend>$_POST</legend>
                <pre><?php print_r($_POST); ?></pre>
            </fieldset>
        </form>
    </body>
</html>
Post Reply