/    Sign up×
Community /Pin to ProfileBookmark

help remember chosen radio button

i found the code below in a book. i have added the radio buttons and i am trying to validate them.
problem is if it doesn’t validate it forgets which radio button was chosen.

can someone help me make this code remember the chosen radio button just like it remembers the textfields.

any other comments to the code is also much appreciated.

[code=php]
<?php
$sent = false;
if (array_key_exists(‘fornavn’, $_POST)) {

$firstname = trim($_POST[‘fornavn’]);
if (empty($firstname)) {
$error[‘fornavn’] = ‘Vennligst skriv inn ditt fornavn.’;
}
$lastname = trim($_POST[‘etternavn’]);
if (empty($lastname)) {
$error[‘etternavn’] = ‘Vennligst skriv inn ditt etternavn.’;
}
$email = $_POST[‘epost’];
$pattern = ‘/^[^@]+@[^srn'”;,@%]+$/’;
if (!preg_match($pattern, trim($email))) {
$error[‘epost’] = ‘Vennligst skriv inn en gyldig e-post adresse.’;
}
$company = trim($_POST[‘firma’]);
if (empty($company)) {
$error[‘firma’] = ‘Vennligst skriv inn ditt firmanavn.’;
}
if (empty($_POST[‘platform’])) {
$error[‘platform’] = ‘Vennligst velg platform.’;
}
else{
$platform = trim($_POST[‘platform’]);
}

if (empty($_POST[‘layout’])) {
$error[‘layout’] = ‘Vennligst velg sideombrekkingsprogram.’;
}
else{
$layout = trim($_POST[‘layout’]);
}

if (empty($_POST[‘image’])) {
$error[‘image’] = ‘Vennligst velg bildebehandlingsprogram.’;
}
else{
$image = trim($_POST[‘image’]);
}

if (!isset($error)) {
$sent = mail($to, $subject, $message, $additionalHeaders);
// check that the mail was sent successfully
if (!$sent) {
$error[‘ikkeSendt’] = ‘Beklager, et problem oppsto. Vennligst pr&oslash;v igjen senere.’;
}
}

}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″ />
<title>Seminar</title>
<style type=”text/css”>
.warning{
color:#F00;
}
</style>
</head>

<body>
<?php if (isset($error[‘notSent’])) { ?>
<h1>Server error</h1>
<p class=”warning”><?php echo $error[‘notSent’]; ?></p>
<?php } elseif ($sent) { ?>
<h1>Takk for din p&aring;melding.</h1>
<p>Vi setter stor pris på din p&aring;melding, og gleder oss til &aring; se deg.</p>
<?php } else { ?>
<h2>Meld deg p&aring;!</h2>
<p>Her kan du melde deg p&aring;…</p>
<?php } ?>
<?php if (!$sent) { ?>

<form action=”<?php $_SERVER[‘PHP_SELF’]; ?>” method=”post” name=”signup” id=”signup”>
<p>
<label for=”fornavn”>Fornavn:</label> <?php if (isset($error[‘fornavn’])) { ?>
<span class=”warning”><?php echo $error[‘fornavn’]; ?></span>
<?php } ?>
<br />
<input type=”text” name=”fornavn” id=”fornavn”
<?php if(isset($error)) {echo “value=’$firstname'”;} ?> />
</p>

<p>
<label for=”etternavn”>Etternavn:</label> <?php if (isset($error[‘etternavn’])) { ?>
<span class=”warning”><?php echo $error[‘etternavn’]; ?></span>
<?php } ?>
<br />
<input type=”text” name=”etternavn” id=”etternavn”
<?php if(isset($error)) {echo “value=’$lastname'”;} ?> />
</p>

<p>
<label for=”epost”>Epost:</label> <?php if (isset($error[‘epost’])) { ?>
<span class=”warning”><?php echo $error[‘epost’]; ?></span>
<?php } ?>
<br />
<input type=”text” name=”epost” id=”epost”
<?php if(isset($error)) {echo “value=’$email'”;} ?> />
</p>

<p>
<label for=”firma”>Firma:</label> <?php if (isset($error[‘firma’])) { ?>
<span class=”warning”><?php echo $error[‘firma’]; ?></span>
<?php } ?>
<br />
<input type=”text” name=”firma” id=”firma”
<?php if(isset($error)) {echo “value=’$company'”;} ?> />
</p>

<p>
<label for=”platform”>Platform:</label> <?php if (isset($error[‘platform’])) { ?>
<span class=”warning”><?php echo $error[‘platform’]; ?></span>
<?php } ?>
<br />
<input type=”radio” name=”platform” id=”platform” value=”Mac” /> Mac
<input type=”radio” name=”platform” id=”platform” value=”PC” /> PC<br />
<?php if (isset($error[‘otherPlatform’])) { ?>
<span class=”warning”><?php echo $error[‘otherPlatform’]; ?></span>
<input type=”text” name=”otherPlatform” id=”otherPlatform”
<?php if(isset($error)) {echo “value=’$company'”;} ?> />
</p>

<p>
<label for=”layout”>Sideombrekking:</label> <?php if (isset($error[‘layout’])) { ?>
<span class=”warning”><?php echo $error[‘layout’]; ?></span>
<?php } ?>
<br />
<input type=”radio” name=”layout” id=”layout” value=”InDesign” /> InDesign
<input type=”radio” name=”layout” id=”layout” value=”Quark” /> Quark<br />
<input type=”text” name=”otherLayout” id=”otherLayout” />
</p>

<p>
<label for=”image”>Bildebehandling:</label> <?php if (isset($error[‘image’])) { ?>
<span class=”warning”><?php echo $error[‘image’]; ?></span>
<?php } ?>
<br />
<input type=”radio” name=”image” id=”image” value=”Photoshop” /> Photoshop<br />
<input type=”text” name=”otherImage” id=”otherImage” />
</p>

<p>
<input name=”knapp” type=”submit” id=”knapp” value=”Send” />
</p>
</form>
<?php } ?>
</body>
</html>

[/code]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bokehOct 25.2006 — [code=php]<input type="radio" name="platform" id="platform" value="PC"<?php echo((@$_POST['platform'] == 'PC')?' checked="checked":'') ?> />[/code]
Copy linkTweet thisAlerts:
@theRamonesOct 26.2006 — put all radio values to new array, an do loop :

<?

$platform = array("PC", "MAC", "SOLARIS");

while(list($key, $value) = each($platform)) {

if($_POST['platform'] == $value) $checked = "checked";

print "<input type="radio" name="platform" value="$value" $checked> $value";

unset($checked);

}

?>
Copy linkTweet thisAlerts:
@demiurgenauthorOct 26.2006 — [code=php]
<input type="radio" name="platform" id="platform" value="PC"<?php echo((@$_POST['platform'] == 'PC')?' checked="checked":'') ?> />
[/code]

this didn't work. you can check it out here:

http://www.waynemelrose.net/testing/thing.php

when you select mac and press send it doesn't validate because you didn't select any other fields and then i want it to remember that you selected mac and not go back to pc on the radiobutton.
Copy linkTweet thisAlerts:
@theRamonesOct 26.2006 — <?php

$sent = false;

if (array_key_exists('fornavn', $_POST)) {

$firstname = trim($_POST['fornavn']);
if (empty($firstname)) {
$error['fornavn'] = 'Vennligst skriv inn ditt fornavn.';
}
$lastname = trim($_POST['etternavn']);
if (empty($lastname)) {
$error['etternavn'] = 'Vennligst skriv inn ditt etternavn.';
}
$email = $_POST['epost'];
$pattern = '/^[^@]+@[^srn'";,@%]+$/';
if (!preg_match($pattern, trim($email))) {
$error['epost'] = 'Vennligst skriv inn en gyldig e-post adresse.';
}
$company = trim($_POST['firma']);
if (empty($company)) {
$error['firma'] = 'Vennligst skriv inn ditt firmanavn.';
}
if (empty($_POST['platform'])) {
$error['platform'] = 'Vennligst velg platform.';
}
else{
$platform = trim($_POST['platform']);
}

if (empty($_POST['layout'])) {
$error['layout'] = 'Vennligst velg sideombrekkingsprogram.';
}
else{
$layout = trim($_POST['layout']);
}

if (empty($_POST['image'])) {
$error['image'] = 'Vennligst velg bildebehandlingsprogram.';
}
else{
$image = trim($_POST['image']);
}

if (!isset($error)) {
$sent = mail($to, $subject, $message, $additionalHeaders);
// check that the mail was sent successfully
if (!$sent) {
$error['ikkeSendt'] = 'Beklager, et problem oppsto. Vennligst pr&oslash;v igjen senere.';
}
}

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Seminar</title>

<style type="text/css">

.warning{

color:#F00;

}

</style>

</head>

<body>

<?php if (isset($error['notSent'])) { ?>

<h1>Server error</h1>

<p class="warning"><?php echo $error['notSent']; ?></p>

<?php } elseif ($sent) { ?>

<h1>Takk for din p&aring;melding.</h1>

<p>Vi setter stor pris på din p&aring;melding, og gleder oss til &aring; se deg.</p>

<?php } else { ?>

<h2>Meld deg p&aring;!</h2>

<p>Her kan du melde deg p&aring;...</p>

<?php } ?>

<?php if (!$sent) { ?>

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" name="signup" id="signup">
<p>
<label for="fornavn">Fornavn:</label> <?php if (isset($error['fornavn'])) { ?>
<span class="warning"><?php echo $error['fornavn']; ?></span>
<?php } ?>
<br />
<input type="text" name="fornavn" id="fornavn"
<?php if(isset($error)) {echo "value='$firstname'";} ?> />
</p>

<p>
<label for="etternavn">Etternavn:</label> <?php if (isset($error['etternavn'])) { ?>
<span class="warning"><?php echo $error['etternavn']; ?></span>
<?php } ?>
<br />
<input type="text" name="etternavn" id="etternavn"
<?php if(isset($error)) {echo "value='$lastname'";} ?> />
</p>

<p>
<label for="epost">Epost:</label> <?php if (isset($error['epost'])) { ?>
<span class="warning"><?php echo $error['epost']; ?></span>
<?php } ?>
<br />
<input type="text" name="epost" id="epost"
<?php if(isset($error)) {echo "value='$email'";} ?> />
</p>

<p>
<label for="firma">Firma:</label> <?php if (isset($error['firma'])) { ?>
<span class="warning"><?php echo $error['firma']; ?></span>
<?php } ?>
<br />
<input type="text" name="firma" id="firma"
<?php if(isset($error)) {echo "value='$company'";} ?> />
</p>

<p>
<label for="platform">Platform:</label> <?php if (isset($error['platform'])) { ?>
<span class="warning"><?php echo $error['platform']; ?></span>
<?php } ?>
<br />
<?
$platform = array("PC", "MAC");
while(list($key, $value) = each($platform)) {
if($_POST['platform'] == $value) $checked = "checked";
print "<input type="radio" name="platform" value="$value" $checked> $value";
unset($checked);
}

?>
<br>
<input type="text" name="otherPlatform" id="otherPlatform"
<?php if(isset($error)) {echo "value='$company'";} ?> />
</p>

<p>
<label for="layout">Sideombrekking:</label> <?php if (isset($error['layout'])) { ?>
<span class="warning"><?php echo $error['layout']; ?></span>
<?php } ?>
<br />
<input type="radio" name="layout" id="layout" value="InDesign" /> InDesign
<input type="radio" name="layout" id="layout" value="Quark" /> Quark<br />
<input type="text" name="otherLayout" id="otherLayout" />
</p>

<p>
<label for="image">Bildebehandling:</label> <?php if (isset($error['image'])) { ?>
<span class="warning"><?php echo $error['image']; ?></span>
<?php } ?>
<br />
<input type="radio" name="image" id="image" value="Photoshop" /> Photoshop<br />
<input type="text" name="otherImage" id="otherImage" />
</p>

<p>
<input name="knapp" type="submit" id="knapp" value="Send" />
</p>
</form>
<?php } ?>

</body>

</html>
Copy linkTweet thisAlerts:
@demiurgenauthorOct 26.2006 — well it works but i still get some errors...

http://www.waynemelrose.net/testing/thing2.php


Notice: Undefined index: platform in c:Inetpubwwwroottestingthing2.php on line 123

Notice: Undefined variable: checked in c:Inetpubwwwroottestingthing2.php on line 124

Notice: Undefined index: platform in c:Inetpubwwwroottestingthing2.php on line 123

Notice: Undefined variable: checked in c:Inetpubwwwroottestingthing2.php on line 124


do know why?
Copy linkTweet thisAlerts:
@theRamonesOct 26.2006 — yeah, u right.

Sorry, i don't know why, cause the script doesn't notice any errors in my localhost server (apache2)
Copy linkTweet thisAlerts:
@demiurgenauthorOct 26.2006 — i moved it to another server running apache... and it worked...

so now my new challenge is the textfields under the radiobuttons...

anyone know how i can make them remember only if the radiobutton hasn't been clicked???

cause when a user doesn't have neither mac nor pc they can write it in the textfield. and this also needs to be remembered... ?
×

Success!

Help @demiurgen spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...