6 Replies - 1841 Views - Last Post: 30 January 2013 - 02:16 PM

#1 dirigible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-October 08

Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 08:53 AM

Hi, all.
I'm pulling my hair out trying to get radio buttons to work using HTML and PHP.
I have some text fields and 5 radio buttons on a page. Here's the radio button HTML:
<input type="radio" name="q1" value="Excellent"/> &nbsp Excellent<br/>
<input type="radio" name="q1" value="Very good"/> &nbsp Very good<br/>
<input type="radio" name="q1" value="Good"/> &nbsp Good<br/>
<input type="radio" name="q1" value="Fair"/> &nbsp Fair<br/>
<input type="radio" name="q1" value="Poor"/> &nbsp Poor<br/>

When the use clicks Submit, I want to send the value of the selected button back to me via e-mail. I get all my text field info correctly, but for the radio buttons I just get "On" rather than the value.

Here are the relevant bits of PHP:
$q1_answer = $_POST['q1'];
$body = "$q1_answer";


All the info I've found online tells me this happens if I don't assign the radio buttons a value, but I think I've done that correctly. Any ideas?

Thanks for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting radio buttons values using HTML and PHP

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 10:12 AM

Should work. Maybe you need to show more of the relevant code.
Was This Post Helpful? 0
  • +
  • -

#3 dirigible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-October 08

Re: Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 12:17 PM

OK. Here's the rest of the PHP. I didn't want to clutter up the original post with the bits that seem to work fine.

$to = "xxxxx@xxxxxxxxxxxxxxx.com";
$subject = "Survey response".$_POST['name'];
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$company_field = $_POST['company'];
$date_field = $_POST['date'];
$q1_answer = $_POST['q1'];


$headers = 'From: '.$_POST['email'].'' . "\r\n" .
   'Reply-To: '.$_POST['email'].'' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();
 
$name_toMail = "Respondant's name: ".$name_field; 
$email_toMail = "Respondant's e-mail address: ".$email_field;
$company_toMail = "Company name: ".$company_field;
$date_toMail = "Date of response: ".$date_field;

$body = "$name_toMail \n $company_toMail \n $email_toMail \n $date_toMail \n $q1_answer";


mail($to, $subject, $body, $headers );



And here's more of the HTML:
		<form method="post" action="survey.php">
			<br />
			Please enter the name of your company? &nbsp <input type="text" style="outline-style:solid;
outline-color:#000000; outline-width:1px; width:300px;"  class="input" name="company"/>
<br/>
<br/>
			Please enter your name? &nbsp <input type="text" style="outline-style:solid;
outline-color:#000000; outline-width:1px; width:300px;" class="input" name="name"/>
			<br/>
			<br/>
			What is your e-mail address? &nbsp <input type="text" style="outline-style:solid;
outline-color:#000000; outline-width:1px; width:300px;" class="input" name="email"/>
			<br/>
			<br/>
			What is today's date? &nbsp <input type="text" style="outline-style:solid;
outline-color:#000000; outline-width:1px; width:300px;" class="input" name="date"/>
			<br/>
			<br/>
			
			1. How would you rate the quality of our rental delivery/pickup service? <br/>
			<input type="radio" name="q1" value="Excellent"/> &nbsp Excellent<br/>
			<input type="radio" name="q1" value="Very good"/> &nbsp Very good<br/>
			<input type="radio" name="q1" value="Good"/> &nbsp Good<br/>
			<input type="radio" name="q1" value="Fair"/> &nbsp Fair<br/>
			<input type="radio" name="q1" value="Poor"/> &nbsp Poor<br/>
			<br/>



Again, thanks for your help!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 12:24 PM

All those nbsp's aren't closed properly - should be &​nbsp;. This is possibly breaking the HTML in the browser.

I assume that you have closed the FORM tag..

NB You should not just grab form data and bung it directly into a email! You need to perform some validation and sanitizing of the values.

http://net.tutsplus....th-php-filters/
(.. if you have PHP 5.2)

In particular, you should remove newlines from the subject:
$subject = str_replace(array("\r","\n"),array(" "," "),$subject);

This post has been edited by Dormilich: 30 January 2013 - 09:48 AM
Reason for edit:: applying magick

Was This Post Helpful? 0
  • +
  • -

#5 dirigible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-October 08

Re: Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 01:38 PM

I tidied up the &nbsp's, but unfortunately that didn't fix it. My gut says the issue is in the PHP.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3624
  • View blog
  • Posts: 12,527
  • Joined: 12-December 12

Re: Getting radio buttons values using HTML and PHP

Posted 29 January 2013 - 01:50 PM

Some debugging required :)/>

Add print_r($_POST); to the top of your PHP code and view the source for the page to see the content.

Turn on all error reporting:

error_reporting(E_ALL);
ini_set('display_errors', '1');


echo or print_r your variables just before the email is sent; etc., etc.

Validate your HTML

Have you properly closed your form tag..? I suspect the error is in your HTML.

This post has been edited by andrewsw: 29 January 2013 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 dirigible  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 02-October 08

Re: Getting radio buttons values using HTML and PHP

Posted 30 January 2013 - 02:16 PM

Works now! I think the issue was some extra spaces in the PHP, but not entirely certain.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1