Newbie question-how to make a radio button sticky
Moderator: General Moderators
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
Newbie question-how to make a radio button sticky
Hi
I just bought a book about php and made my first php form handler.
I'm a total ignorant. But I spent the last 3 days trying to figure out how to make my radio buttons and checkboxes STICKY, with no success.
I am validating and displaying my form all in the same page.
For every text field I use
value="' . $_POST['value_name'] . '"
to make them sticky but this doesn't work for radio buttons or check boxes.
Please help!
Thanx!
I just bought a book about php and made my first php form handler.
I'm a total ignorant. But I spent the last 3 days trying to figure out how to make my radio buttons and checkboxes STICKY, with no success.
I am validating and displaying my form all in the same page.
For every text field I use
value="' . $_POST['value_name'] . '"
to make them sticky but this doesn't work for radio buttons or check boxes.
Please help!
Thanx!
-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
Code: Select all
<?php
if(isset($_POST['radioname']))
{
echo "<input type='radio' name='radioname' checked='checked'>";
}
if(isset($_POST['checkboxname']))
{
echo "<input type='checkbox' name='checkboxname' checked='checked'>";
}
?>-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
thanks a lot for your reply.
I tried to place the code you gave me in various places of my page, but it always returned a parsing error.
May be you could better guide me if you looked at the code (how does one include code properly here? I'm sorry it's my first time on this forum...)
Anyway I greatly appreciate your help!:
Edit: added PHP highlighting - Sami
I tried to place the code you gave me in various places of my page, but it always returned a parsing error.
May be you could better guide me if you looked at the code (how does one include code properly here? I'm sorry it's my first time on this forum...)
Anyway I greatly appreciate your help!:
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Talent Application form</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
<!--
body,td,th {
font-family: Helvetica Neue, Helvetica;
font-size: 12px;
color: #333333;
}
body {
background-color: #FFFFFF;
}
.error {
font-weight: bold;
color: #FF0000;
}
-->
</style></head>
<body>
<?php
ob_start(); // this regulates buffer size in such way that all page data gets to the browser at once
ini_set ('didplay_errors',1); // error handling (display errors)
error_reporting (E_ALL & ~E_NOTICE);
print '<table width="550" border="0" align="center" cellpadding="10" cellspacing="1" bgcolor="#990066">
<tr><td bgcolor="#FFFFFF" align="left" valign="top"><p>TALENT APPLICATION FORM</p>
<span class="error">*</span> indicates required fields<br />';
// *** BEGIN CONDITIONAL: IF SUBMIT BUTTON PRESSED, AND VALIDATION:
if (isset ($_POST['submit'])) { //if submit button has been pressed,...
$problem = FALSE; //...and each field validates...
if (empty ($_POST['firstName'])) { //if first name field is empty...
$problem = TRUE; //...it is a problem
print '<p class="error">Please enter your First Name</p>'; //so, write warning
}
if (empty ($_POST['lastName'])) {
$problem = TRUE;
print '<p class="error">Please enter your Last Name</p>';
}
if (empty ($_POST['address'])) {
$problem = TRUE;
print '<p class="error">Please enter your Address</p>';
}
if (empty ($_POST['city'])) {
$problem = TRUE;
print '<p class="error">Please enter your City</p>';
}
if (empty ($_POST['state']) OR ($_POST['state']) == "select...") {
$problem = TRUE;
print '<p class="error">Please select a State</p>';
}
if (empty ($_POST['zip'])) {
$problem = TRUE;
print '<p class="error">Please enter your Zip Code</p>';
}
if (empty ($_POST['areaCode']) OR strlen ($_POST['areaCode']) != 3 OR !is_numeric ($_POST['areaCode']) OR empty ($_POST['phoneNumMid']) OR strlen ($_POST['phoneNumMid']) != 3 OR !is_numeric ($_POST['phoneNumMid']) OR empty ($_POST['phoneNumEnd']) OR strlen ($_POST['phoneNumEnd']) != 4 OR !is_numeric ($_POST['phoneNumEnd']) ) {
$problem = TRUE;
print '<p class="error">Please enter a full/valid Phone Number</p>';
}
if (empty ($_POST['email'])) {
$problem = TRUE;
print '<p class="error">Please enter your email address</p>';
}
if (!isset ($_POST['education'])) { //if education option is !NOT set...
$problem = TRUE; //...it is a problem
print '<p class="error">Please select your education level</p>';
}
if (empty ($_POST['workExperience'])) {
$problem = TRUE;
print '<p class="error">Please describe your work experience</p>';
}
if (!isset ($_POST['Temporary']) AND !isset ($_POST['TempToPerm']) AND !isset ($_POST['LongTerm']) AND !isset ($_POST['Permanent'])) {
$problem = TRUE;
print '<p class="error">Please select at least one job position you would be interested in</p>';
}
if (empty ($_POST['whereDidUHear'])) {
$problem = TRUE;
print '<p class="error">Please let us know how you heard about New York Green Room</p>';
}
if (!isset ($_POST['agree'])) {
$problem = TRUE;
print '<p class="error">You didn''t agree with our Privacy Policy and Terms & Conditions</p>';
}
if (!$problem) { //if !NO validation problem found after submit, the form passes, emails are sent, and user is sent to thank you page:
//*** email sent to ny green room
$sendto = "sebnewyork@earthlink.net";
$headers = "From: NYGreenRoom<sebnewyork@earthlink.net>\r\n";
$headers .= "Reply-To: NYGreenRoom <admin@nygreenroom.com>\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
// setup variables
$subject = "$firstName $lastName Applied to NYGreenRoom!";
// append to the message that the user typed in
$message .= "<br /><br />
On " . date("F j, Y, g:i a") . " $firstName $lastName ( $email ) Applied to NYGreenRoom. Details follow: <br /><br />
First Name: $firstName <br />
Last Name: $lastName <br />
Address: $address <br />
City: $city <br />
State: $state <br />
Zip Code: $zip <br />
Phone Number: $areaCode $phoneNumMid $phoneNumEnd <br />
Phone 2: $areaCode2 $phoneNumMid2 $phoneNumEnd2 <br />
Email: $email <br />
Highest Education: $education <br />
Additional Education: <br />
$additionalEdu <br />
Work Experience: <br />
$workExperience <br />
Looking for: <br />
Temporary Job: $Temporary <br />
Temp to Perm Job: $TempToPerm <br />
Long Term Job: $LongTerm <br />
Permanent Job: $Permanent <br />
Where did you Hear from Us: $whereDidUHear<br />";
mail($sendto, $subject, $message, $headers);
// *** thank you message.
$sendto = $email;
$subject = "Thank you $firstName $lastName for your interest in New York Green Room";
$message = "$firstName $lastName, We received your application and are reviewing it.
A representative will contact you shortly.
Thank you from all of us at New York Green Room!";
mail($sendto, $subject, $message, $headers);
// start session, for passing user name to thank you page:
session_start();
$_SESSION['firstname'] = $_POST['firstName'];
$_SESSION['lastname'] = $_POST['lastName'];
// *** go to next page:
header ('location: afterapply.php');
exit();
} else { //if there is a validation problem, then write warning and...
print '<p><span class="error"> - Please try again - </span></p>';
} //...display the form again:
} // *** end of first conditionnal: if submit button has not been pressed, display the form:
// *** HERE IS THE FORM PER SE:
print '<form action="applyForm2.php" method="post"><p><strong>Contact Information:</strong></p>
<table width="80%" cellpadding="2" cellspacing="1" bgcolor="#CCFFCC" align="center">
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Firt Name<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="firstName" size="20" value="' . $_POST['firstName'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Last Name<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="lastName" size="20" value="' . $_POST['lastName'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Address<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="address" size="20" value="' . $_POST['address'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
City<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="city" size="20" value="' . $_POST['city'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
State<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<select name="state" size="1">
<option selected="selected" value="' . $_POST['state'] . '">'; print $_POST['state']; print '</option>
<option value="Alabama">Alabama</option>
<option value="Alaska">Alaska</option>
<option value="Arizona">Arizona</option>
<option value="Arkansas">Arkansas</option>
<option value="California">California</option>
<option value="Colorado">Colorado</option>
<option value="Connecticut">Connecticut</option>
<option value="Delaware">Delaware</option>
<option value="Florida">Florida</option>
<option value="Georgia">Georgia</option>
<option value="Hawaii">Hawaii</option>
<option value="Idaho">Idaho</option>
<option value="Illinois">Illinois</option>
<option value="Indiana">Indiana</option>
<option value="Iowa">Iowa</option>
<option value="Kansas">Kansas</option>
<option value="Kentucky">Kentucky</option>
<option value="Louisiana">Louisiana</option>
<option value="Maine">Maine</option>
<option value="Maryland">Maryland</option>
<option value="Massachusetts">Massachusetts</option>
<option value="Michigan">Michigan</option>
<option value="Minnesota">Minnesota</option>
<option value="Mississippi">Mississippi</option>
<option value="Missouri">Missouri</option>
<option value="Montana">Montana</option>
<option value="Nebraska">Nebraska</option>
<option value="Nevada">Nevada</option>
<option value="New Hampshire">New Hampshire</option>
<option value="New Jersey">New Jersey</option>
<option value="New Mexico">New Mexico</option>
<option value="New York">New York</option>
<option value="North Carolina">North Carolina</option>
<option value="North Dakota">North Dakota</option>
<option value="Ohio">Ohio</option>
<option value="Oklahoma">Oklahoma</option>
<option value="Oregon">Oregon</option>
<option value="Pennsylvania">Pennsylvania</option>
<option value="Rhode Island">Rhode Island</option>
<option value="South Carolina">South Carolina</option>
<option value="South Dakota">South Dakota</option>
<option value="Tennessee">Tennessee</option>
<option value="Texas">Texas</option>
<option value="Utah">Utah</option>
<option value="Vermont">Vermont</option>
<option value="Virginia">Virginia</option>
<option value="Washington">Washington</option>
<option value="West Virginia">West Virginia</option>
<option value="Wisconsin">Wisconsin</option>
<option value="Wyoming">Wyoming</option>
</select>
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Zip / Postal Code<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="zip" size="20" value="' . $_POST['zip'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Phone<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input name="areaCode" type="text" id="areaCode" size="4" maxlength="3" value="' . $_POST['areaCode'] . '" />
<input name="phoneNumMid" type="text" id="phoneNumMid" size="4" maxlength="3" value="' . $_POST['phoneNumMid'] . '" />
<input name="phoneNumEnd" type="text" id="phoneNumEnd" size="5" maxlength="4" value="' . $_POST['phoneNumEnd'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Alternative Phone:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input name="areaCode2" type="text" id="areaCode2" size="4" maxlength="3" value="' . $_POST['areaCode2'] . '" />
<input name="phoneNumMid2" type="text" id="phoneNumMid2" size="4" maxlength="3" value="' . $_POST['phoneNumMid2'] . '" />
<input name="phoneNumEnd2" type="text" id="phoneNumEnd2" size="5" maxlength="4" value="' . $_POST['phoneNumEnd2'] . '" />
</td>
</tr>
<tr>
<td align="left" valign="middle" bgcolor="#FFFFFF">
Email<span class="error">*</span>:
</td>
<td align="left" valign="middle" bgcolor="#FFFFFF">
<input type="text" name="email" size="30" value="' . $_POST['email'] . '" />
</td>
</tr>
</table>
<p><strong>Education:</strong><br />
Please select the highest level of education you have attained<span class="error">*</span>:</p>
<table width="200" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCFFCC">
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="phd" />
PhD</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="PostGraduateCredits" />
Post graduate Credits</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="AssociateDegree" />
Associate Degree</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="HighSchoolDegree" />
High School Degree</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="MasterDegree" />
Master Degree</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="BachelorDegree" />
Bachelor Degree</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="CollegeCredit" />
College Credit</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="GED" />
GED</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">
<input type="radio" name="education" value="other" />
Other</td>
</tr>
</table>
<p>Please list any additional educational information you beleive to be relevent (e.g. certifications, liceneses, etc.)</p>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">
<textarea name="additionalEdu" cols="40" rows="5" value="' . $_POST['additionalEdu'] . '">'; print $additionalEdu; print'</textarea>
</div></td>
</tr>
</table>
<p><strong>Resume / Work experience</strong><span class="error">*</span>:<br />
Please describe your related work experience</p>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">
<textarea name="workExperience" cols="40" rows="5" value="' . $_POST['workExperience'] . '">'; print $workExperience; print'</textarea>
</div></td>
</tr>
</table>
<p>What kind of positions would you be interested in?<span class="error">*</span></p>
<table width="200" border="0" align="center" cellpadding="2" cellspacing="1" bgcolor="#CCFFCC">
<tr>
<td bgcolor="#FFFFFF"><input name="Temporary" type="checkbox" value="YES" />
Temporary Job </td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><input name="TempToPerm" type="checkbox" value="YES" />
Temp to Perm Job </td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><input name="LongTerm" type="checkbox" value="YES" />
Long-Term Temporary Job </td>
</tr>
<tr>
<td bgcolor="#FFFFFF"><input name="Permanent" type="checkbox" value="YES" />
Permanent Job </td>
</tr>
</table>
<p>Where did you hear about New York Green Room? <span class="error">*</span>
<select name="whereDidUHear" id="whereDidUHear">
<option value="' . $_POST['whereDidUHear'] . '" selected="selected">'; print $_POST['whereDidUHear']; print '</option>
<option value="newspaper ad">newspaper ad</option>
<option value="Friend">Friend</option>
<option value="Yellow Pages">Yellow Pages</option>
<option value="Flyer">Flyer</option>
<option value="Internet">Internet</option>
<option value="Other">Other</option>
</select>
</p>
<p>Submission of personal information signifies that you have read and
agreed to be bound by the New York Greenroom <a href="terms.htm" target="blank"><span class="error">Terms
of Use</span></a> Agreement, and that you consent to New York Greenroom''s use of your resume and personal information
in accordance with the New York Greenroom Web Site <a href="privacy.htm" target="blank"><span class="error">Privacy
Policy</span></a>.</p>
<p>
<input type="checkbox" name="agree" value="' . $_POST['agree'] . '" />
Yes I agree with the <a href="terms.htm" target="blank"><span class="error">Terms
of Use</span></a> & <a href="privacy.htm" target="blank"><span class="error">Privacy
Policy*</a></span></p>
<p>
<input type="submit" name="submit" value="Apply!" />
</p>
</form>';
// *** END OF FORM PER SE
print '</td></tr>
</table>';
ob_end_flush(); //this ends buffer size regulation started on top of page
?>
</body>
</html>-
TheBentinel.com
- Forum Contributor
- Posts: 282
- Joined: Wed Mar 10, 2004 1:52 pm
- Location: Columbus, Ohio
This shows the various ways you set default values in HTML forms. On check boxes, include the word "checked", same for radio's, in select's put the word "selected" in the <option> tag.
Code: Select all
<html>
<form>
checked?<input type=checkbox checked><br>
rad1<input type=radio name=blahradio value=1 ><br>
rad2<input type=radio name=blahradio value=1 checked><br>
rad3<input type=radio name=blahradio value=1 ><br>
<select name=myselect>
<option value=1>val1
<option value=2 selected>val2
</select>
<input type=text name=testinput value="blah">
<input type=submit name=submitButton>
</form>
</html>-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm
hey Goowe
I actually managed to make your code work for my check boxes! Thanks!!
But for the radio buttons, it won't work, because all the radio buttons in a list have the same name, as in:
<input type="radio" name="education" value="phd">
<input type="radio" name="education" value="MasterDegree">
etc...
so this, (which worked for the checkboxes because they have different names):
if (isset ($_POST['education'])) {
echo '<input type="radio" name="education" value="phd" checked="checked" />';
}else{
print '<input type="radio" name="education" value="phd" />';
}
doesn't work for radio buttons. What happens after submitting the form, is the last radio button of the list gets checked, no matter which one was originally checked before submit.
Any ideas???
I actually managed to make your code work for my check boxes! Thanks!!
But for the radio buttons, it won't work, because all the radio buttons in a list have the same name, as in:
<input type="radio" name="education" value="phd">
<input type="radio" name="education" value="MasterDegree">
etc...
so this, (which worked for the checkboxes because they have different names):
if (isset ($_POST['education'])) {
echo '<input type="radio" name="education" value="phd" checked="checked" />';
}else{
print '<input type="radio" name="education" value="phd" />';
}
doesn't work for radio buttons. What happens after submitting the form, is the last radio button of the list gets checked, no matter which one was originally checked before submit.
Any ideas???
probably need something more like this for the radio
if you have 1 or 2
if you have quite a few radio buttons named the same, I would use something more like this:
PS I didnt test any of this for syntax, but you should get the idea 
if you have 1 or 2
Code: Select all
if($_POSTї"education"]=="phd"){
$sel = " CHECKED";
}else{
$sel = "";
}
echo '<input type="radio" name="education" value="phd" '.$sel.'> ';Code: Select all
$radio_values = array("PHD","MASTERS","Bachelors");
$radio_name = "education";
foreach($radio_values as $key => $radio_value){
if($_POSTї$radio_name]==$radio_value){
$sel = " CHECKED";
}else{
$sel = "";
}
echo '<input type="radio" name="'.$radio_name.'" value="'.$radio_value.'" '.$sel.'> ';
}-
sebnewyork
- Forum Commoner
- Posts: 43
- Joined: Wed Mar 17, 2004 10:20 pm