View Full Version : Retrieving radio button information in PHP
kairick
06-09-2010, 09:18 PM
I have set up the following form to (a) email confirmation to the user (b) email form input to my address (c) redirect to another page "attendee.html" on submit.
I had to edit the form to include radio buttons "giss1" and "giss2" and I am having trouble having those results seen in the emails.
Here's the code:
<?php
if(($_POST['college']=="")||($_POST['streetaddress']=="")||($_POST['city']=="")||($_POST['zip']=="")||($_POST['state']=="")||($_POST['contactperson']=="")||($_POST['contactemail']=="")||($_POST['phone']=="")||($_POST['fax']=="")||($_POST['giss1']=="")||($_POST['giss2']==""))
{
echo "<html><body><p>The following fields are <strong>required</strong>.</p><ul>";
if($_POST['college'] == ""){ echo "<li>*College</li>"; }
if($_POST['streetaddress'] == ""){ echo "<li>*Street Address</li>"; }
if($_POST['city'] == ""){ echo "<li>*City</li>"; }
if($_POST['zip'] == ""){ echo "<li>*Zip</li>"; }
if($_POST['state'] == ""){ echo "<li>*State</li>"; }
if($_POST['contactperson'] == ""){ echo "<li>*Contact Person</li>"; }
if($_POST['contactemail'] == ""){ echo "<li>*Contact Email</li>"; }
if($_POST['phone'] == ""){ echo "<li>*Phone</li>"; }
echo "</ul><p>Please use your browsers <a href=\"javascript:history.back();\">Back</a> button and fill out these fields.</p></body></html>";
}
if(isset($_POST['submit'])) {
$to = "my@email.com";
$subject = "Contact Has Registered";
$college = $_POST['college'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$contactperson = $_POST['contactperson'];
$contactemail = $_POST['contactemail'];
$phone = $_POST['phone'];
if ($selected_radio == 'giss1') {
$giss1_status = 'checked';
}
else if ($selected_radio == 'giss2') {
$giss2_status = 'checked';
}
$body = "From: $college
Street Address: $streetaddress
E-Mail: $contactemail
Phone Number: $phone
Contact Email: $contactemail
GISS Session: $selected_radio";
echo "We've recived your contact information $to! We will be in Contact with you shortly!" ;
mail($to, $subject, $body);
//These are the variables for the email
$sendto = $_POST['contactemail']; // this is the email address collected form the form
$ccto = "my@email.com"; //you can cc it to yourself
$subject = "Registration"; // Subject
$message = "Thank you for registering. Here are the registration details you have provided:
You are registered for:$selected_radio
College:$college
Address:$streetaddress
City:$city
State:$state
Contact Person:$contactperson
Contact Email:$contactemail
.......";
// This is the function to send the email
if(mail($sendto, $subject, $message, $header)) header("location: attendee.html");
}
?>
<script type=text/javascript>
setTimeout("location.href='index.html'", [3000]);
</script>
I'm a PHP amateur and have been working on this project for hours so now it's all starting to look like gibberish.
Thanks so much!
fileserverdirect
06-10-2010, 01:32 AM
Can you please post the html form here. That will help us understand whats going on in the PHP. Most likely its a naming problem and not even PHP, but the only way to know that is for us to see the form code ...and didn't I just work with you on this script, lol...
Thanks,
kairick
06-10-2010, 05:09 AM
Lol why, yes you did. Then the client wanted radio buttons and that sent me right back to feeling brain damaged. Original problems were fixed though! :D
Here's the form code:
<form action="home_contact.php" method="post" id="frmcontactform" >
<div>
<p>*Asterisk denotes mandatory field</p>
<fieldset>
<div><label for="college">*College</label>
<input type="text" name="college" id="college" size="41" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="streetaddress">*Street Address</label>
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="city">*City</label>
<input type="text" name="city" id="city" size="27" />
</div>
</fieldset>
<br />
<fieldset>
<div style="padding-bottom:12px"><label for="state">*State</label>
<input type="text" name="state" id="state" size="2" />
</div>
<div><label for="zip">*Zip</label>
<input type="text" name="zip" id="zip" size="5" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactperson">*Contact Person</label>
<input type="text" name="contactperson" id="contactperson" size="22" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactemail">*Contact Email</label>
<input type="text" name="contactemail" id="contactemail" size="23" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="phone">*Phone</label>
<input type="text" name="phone" id="phone" size="10" />
</div>
</fieldset>
<br />
<fieldset>
<div>
<label for="fax">Fax</label>
<input type="text" name="fax" id="fax" size="11" />
</div>
</fieldset>
<br />
<fieldset>
<div>
Please select the GISS session you plan to attend<P>
<input type="radio" name="giss1" id="giss1" value="GISS I" />GISS I
<input type="radio" name="giss1" id="giss1" value="GISS I" />GISS II
<br />
</div>
</fieldset>
<div class="centerac"><input type="submit" name="submit" id="submit" value="Submit to Enter Attendee Information" />
</div>
<br />
</div>
</form>
Thanks again!
djr33
06-10-2010, 05:21 AM
I believe that radio buttons work exactly the same way as a select menu or even just a text input: their value is sent as $_POST['fieldname'].
One obvious problem in the html is that the values are both the same. Is this intentional? Is this a typo while pasting the code here? If not, I bet that's the whole problem.
kairick
06-10-2010, 06:25 AM
sorry that was a typo in pasting here. lol. I've been up too long.
<form action="home_contact.php" method="post" id="frmcontactform" >
<div>
<p>*Asterisk denotes mandatory field</p>
<fieldset>
<div><label for="college">*College</label>
<input type="text" name="college" id="college" size="41" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="streetaddress">*Street Address</label>
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="city">*City</label>
<input type="text" name="city" id="city" size="27" />
</div>
</fieldset>
<br />
<fieldset>
<div style="padding-bottom:12px"><label for="state">*State</label>
<input type="text" name="state" id="state" size="2" />
</div>
<div><label for="zip">*Zip</label>
<input type="text" name="zip" id="zip" size="5" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactperson">*Contact Person</label>
<input type="text" name="contactperson" id="contactperson" size="22" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactemail">*Contact Email</label>
<input type="text" name="contactemail" id="contactemail" size="23" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="phone">*Phone</label>
<input type="text" name="phone" id="phone" size="10" />
</div>
</fieldset>
<br />
<fieldset>
<div>
<label for="fax">Fax</label>
<input type="text" name="fax" id="fax" size="11" />
</div>
</fieldset>
<br />
<fieldset>
<div>
Please select the GISS session you plan to attend<P>
<input type="radio" name="giss1" id="giss1" value="GISS I" />GISS I
<input type="radio" name="giss2" id="giss2" value="GISS II" />GISS II
<br />
</div>
</fieldset>
<div class="centerac"><input type="submit" name="submit" id="submit" value="Submit to Enter Attendee Information" />
</div>
<br />
</div>
</form>
The only thing not showing up in the emails is the radio button selection (GISS I or GISS II).
Thanks
djr33
06-10-2010, 06:31 AM
What happens if you submit the form and use the following line?
<?php print_r($_POST); ?>
That should tell you how to deal with the data.
kairick
06-10-2010, 12:08 PM
where would I enter that line? After "value" in the HTML? Do I customize the line as
<?PHP print $giss1_status; ?> or leave it as is?
Thanks
kairick
06-10-2010, 07:04 PM
Here's my updated php code. I don't think I posted the print_r($_POST) correctly.
<?php
if(($_POST['college']=="")||($_POST['streetaddress']=="")||($_POST['city']=="")||($_POST['zip']=="")||($_POST['state']=="")||($_POST['contactperson']=="")||($_POST['contactemail']=="")||($_POST['phone']=="")||($_POST['fax']=="")||($_POST['giss1']=="")||($_POST['giss2']==""))
{
echo "<html><body><p>The following fields are <strong>required</strong>.</p><ul>";
if($_POST['college'] == ""){ echo "<li>*College</li>"; }
if($_POST['streetaddress'] == ""){ echo "<li>*Street Address</li>"; }
if($_POST['city'] == ""){ echo "<li>*City</li>"; }
if($_POST['zip'] == ""){ echo "<li>*Zip</li>"; }
if($_POST['state'] == ""){ echo "<li>*State</li>"; }
if($_POST['contactperson'] == ""){ echo "<li>*Contact Person</li>"; }
if($_POST['contactemail'] == ""){ echo "<li>*Contact Email</li>"; }
if($_POST['phone'] == ""){ echo "<li>*Phone</li>"; }
echo "</ul><p>Please use your browsers <a href=\"javascript:history.back();\">Back</a> button and fill out these fields.</p></body></html>";
}
if(isset($_POST['submit'])) {
$to = "my@email.com";
$subject = "Contact Has Registered";
$college = $_POST['college'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$contactperson = $_POST['contactperson'];
$contactemail = $_POST['contactemail'];
$phone = $_POST['phone'];
if ($selected_radio == 'giss1') {
$giss1_status = 'checked';
print_r($_POST);
}
else if ($selected_radio == 'giss2') {
$giss2_status = 'checked';
print_r($_POST);
}
$body = "From:$college
Street Address:$streetaddress
City:$city
State:$state
Zip Code:$zip
E-Mail:$contactemail
Phone Number:$phone
Contact Email:$contactemail
GISS Session:$selected_radio";
mail($to, $subject, $body);
//These are the variables for the email
$sendto = $_POST['contactemail']; // this is the email address collected form the form
$ccto = "my@email.com";
$subject = "Registration";
$message = "Thank you for registering.
Here are the registration details you have provided:
You are registered for:$selected_radio
College:$college
Address:$streetaddress
City:$city
State:$state
Zip:$zip
Contact Person:$contactperson
Contact Email:$contactemail";
// This is the function to send the email
if(mail($sendto, $subject, $message, $header)) header("location: attendee.html");
}
?>
<script type=text/javascript>
setTimeout("location.href='index.html'", [3000]);
</script>
fileserverdirect
06-10-2010, 07:17 PM
Try just putting it at the top of the page and copy the output here. It's not showing up because $selected_radio is not being set to any $_POST value. It looks as if your copying 100% of your code off of some tutorial site and your not copying all of it. Just a guess.
kairick
06-10-2010, 07:32 PM
The basic form was generated using SiteGrinder 2 (It was only initially supposed to be basic.) but as the client wanted features added to the form (email, confirmation, redirect, now radio buttons), it gradually became pieced together from a couple of sources.
So to clarify, I'm posting print_r($_POST) at the top of the page (after <?php ) and copying the result here?
djr33
06-10-2010, 07:37 PM
print_r() displays the contents of a variable. We need to find out what $_POST contains: that is all the information sent through the "post" method-- in other words, all of the information from your form.
Look through the output (best to view it in view>source) and find the value corresponding to the field in your form.
To be entirely clear-- ALL you are doing is displaying what was sent so you know how to work with it.
You can insert this code before EVERYTHING else on the page as-is:
<?php print_r($_POST); exit(); ?>
That will print out the info then stop execution: you will only get the values of POST, then after that just remove that line from the top and use your page like normal-- but then you'll know what values are being sent.
Additionally, I recommend testing this several times to find out what the possible values are-- try clicking one radio button, clicking none, clicking the others.... etc.
Once you identify a pattern that is reliable, you can program PHP to do the same. But until you know what you're looking for, it's just a guess in PHP, and especially from us here since we can't test it.
kairick
06-10-2010, 08:12 PM
No change. Where do I set the $selected_radio $POST value?
I have the post values up top for 'giss1' and 'giss2'. Would I put the 'selected_radio' up there too?
fileserverdirect
06-10-2010, 08:42 PM
Do you have a link you could give us? Or just post the resulting output when<?php print_r($_POST); exit(); ?> is pasted at the absolute top and a form is submitted to the page.
kairick
06-10-2010, 08:57 PM
Turns out I was all screwed up. Here's the new code:
Inserted into head:
<?PHP
$selected_radio = $_POST['session'];
print $selected_radio;
?>
form:
<form action="home_contact.php" method="post" id="frmcontactform" >
<div>
<p>*Asterisk denotes mandatory field</p>
<fieldset>
<div><label for="college">*College</label>
<input type="text" name="college" id="college" size="41" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="streetaddress">*Street Address</label>
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="city">*City</label>
<input type="text" name="city" id="city" size="27" />
</div>
</fieldset>
<br />
<fieldset>
<div style="padding-bottom:12px"><label for="state">*State</label>
<input type="text" name="state" id="state" size="2" />
</div>
<div><label for="zip">*Zip</label>
<input type="text" name="zip" id="zip" size="5" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactperson">*Contact Person</label>
<input type="text" name="contactperson" id="contactperson" size="22" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="contactemail">*Contact Email</label>
<input type="text" name="contactemail" id="contactemail" size="23" />
</div>
</fieldset>
<br />
<fieldset>
<div><label for="phone">*Phone</label>
<input type="text" name="phone" id="phone" size="10" />
</div>
</fieldset>
<br />
<fieldset>
<div>
<label for="fax">Fax</label>
<input type="text" name="fax" id="fax" size="11" />
</div>
</fieldset>
<br />
<fieldset>
<div>
Please select the GISS session you plan to attend<P>
<input type="radio" name="session" id="giss1" value="GISS I"><?php print$giss1_status;?>
GISS I
<br />
<input type="radio" name="session" id="giss2" value="GISS II"><?php print$giss2_status;?> GISS II<br />
</div>
</fieldset>
<div class="centerac"><input type="submit" name="submit" id="submit" value="Submit to Enter Attendee Information" />
</div>
<br />
</div>
</form>
PHP
<?php
{
if(($_POST['college']=="")||($_POST['streetaddress']=="")||($_POST['city']=="")||($_POST['zip']=="")||($_POST['state']=="")||($_POST['contactperson']=="")||($_POST['contactemail']=="")||($_POST['phone']=="")||($_POST['fax']=="")||($_POST['giss1']=="")||($_POST['giss2']==""))
{
echo "<html><body><p>The following fields are <strong>required</strong>.</p><ul>";
if($_POST['college'] == ""){ echo "<li>*College</li>"; }
if($_POST['streetaddress'] == ""){ echo "<li>*Street Address</li>"; }
if($_POST['city'] == ""){ echo "<li>*City</li>"; }
if($_POST['zip'] == ""){ echo "<li>*Zip</li>"; }
if($_POST['state'] == ""){ echo "<li>*State</li>"; }
if($_POST['contactperson'] == ""){ echo "<li>*Contact Person</li>"; }
if($_POST['contactemail'] == ""){ echo "<li>*Contact Email</li>"; }
if($_POST['phone'] == ""){ echo "<li>*Phone</li>"; }
if($_POST['session'] == "unchecked") { echo "<li>You must select a GISS session</li>"; }
echo "</ul><p>Please use your browsers <a href=\"javascript:history.back();\">Back</a> button and fill out these fields.</p></body></html>";
}
if ($selected_radio == 'giss1') {
$giss1_status = 'checked';
}
else if ($selected_radio == 'giss2') {
$giss2_status = 'checked';
}
if(isset($_POST['submit'])) {
$to = "my@email.com";
$subject = "Contact Has Registered";
$college = $_POST['college'];
$streetaddress = $_POST['streetaddress'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$contactperson = $_POST['contactperson'];
$contactemail = $_POST['contactemail'];
$phone = $_POST['phone'];
$selected_radio = $_POST['session'];
print $session;
}
$body = "From:$college
Street Address:$streetaddress
City:$city
State:$state
Zip Code:$zip
E-Mail:$contactemail
Phone Number:$phone
Contact Email:$contactemail
GISS Session:$selected_radio";
mail($to, $subject, $body);
$sendto = $_POST['contactemail'];
$ccto = "my@email.com";
$subject = "Registration";
$message = "Thank you for registering.
You are registered for:$selected_radio
College:$college
Address:$streetaddress
City:$city
State:$state
Zip:$zip
Contact Person:$contactperson
Contact Email:$contactemail";
if(mail($sendto, $subject, $message, $header)) header("location: attendee.html");
}
?>
<script type=text/javascript>
setTimeout("location.href='index.html'", [3000]);
</script>
Thanks, everyone for your help!
djr33
06-11-2010, 01:57 AM
You're making this way too complex: read my posts carefully.
This has nothing to do with your existing code, nor is it intended to "solve" anything-- this is going to help you/us diagnose the problem.
Here's what you should do:
1. Ignore all of the PHP you have. Don't delete it, just don't use it for now.
2. Create a new php file called "phpnew.php"
3. In that file, paste exactly and only <?php print_r($_POST); ?> and NOTHING else.
4. Temporarily change the action of your HTML form to "....../phpnew.php" (the location of this new page).
5. Test it!!! Try different values in the form. See what the result is on the PHP page.
6. Find the pattern in the results from the radio buttons. What are you looking for? Is it "1" or "0"? Is it text? Is it being sent correctly? Then once you figure this out post the results here and we can try to help you figure out if the problem is in the PHP or in the HTML form.
This is a tool for diagnosing what is happening: it will tell us what data is actually being sent (and received by PHP) so we know what to do next.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.