Solved

Web Form

Posted on 2010-08-20
12
257 Views
Last Modified: 2012-08-14
I need to make a simple form using radio buttons that will allow someone to choose between one thing or another.  Then when they hit submit, their choice will be emailed to me.

Can someone help me with this code?

Thanks!
0
Comment
Question by:dmrussell12345
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 10

Expert Comment

by:Casey Herman
ID: 33487339
This is very possible but the more elegant you want it, the more complex the code becomes. Do you have a PHP enabled webserver?

If so, here is a very basic example:

http://www.homeandlearn.co.uk/php/php4p10.html
0
 

Expert Comment

by:zogmit
ID: 33488699
Sure here is an example, it isn't elegant at all, and I wrote it up in 2 minutes, but it will do the job or at least give you an idea.
<?php

if(isset($_GET['submit']) && !empty($_POST['cfrm'])){

//change $to="myemail@mail.com"; to the email where you will receive the submitted web form
	$to="myemail@mail.com";

	$subject=$_POST['field2'];
	$message=$_POST['field3']."\r\n";
	$headers='From: '.$_POST['field1']."\r\n".
		'Reply-To: '.$_POST['field1']."\r\n".
		'X-Mailer: PHP/'.phpversion();
	
	mail($to, $subject, $message, $headers);

	echo "Your request has been submitted, thank you.<br />";
	exit();
}

?>
<html>
    <head><title>Webform</title></head>

<body>
    <form method="POST" action="?submit">
	<input type="hidden" value="1" name="cfrm" />
	From: &nbsp;&nbsp;<input type="text" name="field1" value="" /><br />
	Subject: &nbsp;&nbsp;<input type="text" name="field2" value="" /><br />
	Message: &nbsp;&nbsp;<textarea name="field3" cols="10" rows="40"></textarea><br />
    </form>
</body>

</html>

Open in new window

0
 

Author Comment

by:dmrussell12345
ID: 33488736
Thanks zogmit.  I'll play with this.  But it doesn't appear that there are any radio buttons in this code.

I'm really trying to give a person two options with radio buttons they can choose.  Then when they submit the form it emails me which one they chose.

casedog21 - I'll take a look at the link you posted and see if it will work.

Thanks!
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

Expert Comment

by:zogmit
ID: 33488753
Sorry, forgot about the radio buttons, here it is
<?php

if(isset($_GET['submit']) && !empty($_POST['cfrm'])){

//change $to="myemail@mail.com"; to the email where you will receive the submitted web form
	$to="myemail@mail.com";

	$subject=$_POST['field2'];
	$message="A user has selected the following option".$_POST'choice']."\r\nMessage:\r\n$_POST['field3']."\r\n";
	$headers='From: '.$_POST['field1']."\r\n".
		'Reply-To: '.$_POST['field1']."\r\n".
		'X-Mailer: PHP/'.phpversion();
	
	mail($to, $subject, $message, $headers);

	echo "Your request has been submitted, thank you.<br />";
	exit();
}

?>
<html>
    <head><title>Webform</title></head>

<body>
    <form method="POST" action="?submit">
	<input type="hidden" value="1" name="cfrm" />
	From: &nbsp;&nbsp;<input type="text" name="field1" value="" /><br />
	Option 1: &nbsp;&nbsp;<input type="radio" name="choice" value="Option 1" />
	Option 2: &nbsp;&nbsp;<input type="radio" name="choice" value="Option 2" />
	Subject: &nbsp;&nbsp;<input type="text" name="field2" value="" /><br />
	Message: &nbsp;&nbsp;<textarea name="field3" cols="10" rows="40"></textarea><br />
    </form>
</body>

</html>

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33489440
This script illustrates the essential moving parts.

One item of note about radio buttons and checkboxes in HTML forms... If they are not selected, THEY DO NOT APPEAR in the submitted forms arrays ($_GET, $_POST, $_REQUEST).  You might want to try submitting this form without selecting anything so you can see what this means to your logic.

Best regards, ~Ray
<?php // RAY_radio.php
error_reporting(E_ALL);

// IF THE FORM HAS BEEN POSTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POST VARIABLES
    // var_dump($_POST);

    // GET THE INFORMATION FROM THE POST ARRAY
    $p = serialize($_POST);

    // MAIL IT
    mail ('You@Your.org', 'POST RADIO BUTTONS', $p);

    // END OF JOB
    die('THANKS');
}

// END OF PHP - PUT UP THE FORM
?>
<form method="post">
<input name="agreement" type="radio" value="agree" />Agree
<input name="agreement" type="radio" value="disagree" />Disagree
<input type="submit" name="go" value="go" />
</form>

Open in new window

0
 

Author Comment

by:dmrussell12345
ID: 33500995
Ray Paseur,

This code seems to work just fine.

However, I would like to add a field for the person's name who is filling out the form.  I have added it to the code, but need a little help sending it to my email along with the radio button result.

Please advise.

<?php // RAY_radio.php
error_reporting(E_ALL);

// IF THE FORM HAS BEEN POSTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POST VARIABLES
    // var_dump($_POST);

    // GET THE INFORMATION FROM THE POST ARRAY
    $p = serialize($_POST);

    // MAIL IT
    mail ('myemail@myemail.com', 'POST RADIO BUTTONS', $p);

    // END OF JOB
    die('THANKS');
}

// END OF PHP - PUT UP THE FORM
?>
<form method="post">
  <p>NAME: 
    <label>
    <input name="textfield" type="text" id="Name" size="50">
    </label>
</p>
  <p>
    <input name="agreement" type="radio" value="agree" />
    Agree <br>
    <input name="agreement" type="radio" value="disagree" />
    Disagree<br>
    <input type="submit" name="go" value="go" />
    </p>
</form>

Open in new window

0
 

Author Comment

by:dmrussell12345
ID: 33501626
Disregard that last comment.  I figured that out.

One other question though...

Is there a way to have the email response more organized?  For instance, if I have two different  groups of radio buttons.  (Group 1: Choose Radio 1 or Radio 2) ( Group 2: Choose Radio 1 or Radio 2).

I would like for the email to be something like this:
Group 1: Radio Choice
Group 2: Radio Choice

Hope this makes sense.

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33502050
Sure - instead of serializing the POST, you can format it.  Activate the var-dump() command to see what is in the POST array.  If you need any help with that, just post the output of var_dump() here and we can show you some ways to get it formatted the way you want.
0
 

Author Comment

by:dmrussell12345
ID: 33510938
I apologize for the ignorance... where do I include the code to activate the var-dump?

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33511050
See these lines from the code snippet above.  The // means they are comments.  Remove the comment marker from the var-dump() line.

Consider getting this book - it is very readable and has a lot of great examples.  Should help you get a good foundation in PHP.
http://www.sitepoint.com/books/phpmysql4/
// ACTIVATE THIS TO SHOW THE POST VARIABLES
    // var_dump($_POST);

Open in new window

0
 

Author Comment

by:dmrussell12345
ID: 33511184
Thanks for the suggested reading.  I will look for that book.

Below if is the code I'm using.

I activated the var-dump and here is what comes up when I submit:
array(4) { ["textfield"]=> string(9) "Joe Smith" ["GroupOne"]=> string(9) "choicetwo" ["GroupTwo"]=> string(9) "choiceone" ["go"]=> string(6) "Submit" } THANKS

I would rather it be emailed to me in the following organized format:
Joe Smith
Group One: choicetwo
Group Two: choiceone

<?php // RAY_radio.php
error_reporting(E_ALL);

// IF THE FORM HAS BEEN POSTED
if (!empty($_POST))
{
    // ACTIVATE THIS TO SHOW THE POST VARIABLES
    var_dump($_POST);

    // GET THE INFORMATION FROM THE POST ARRAY
    $p = serialize($_POST);

    // MAIL IT
    mail ('myemail@myemail.com', 'POST RADIO BUTTONS', $p);

    // END OF JOB
    die('THANKS');
}

// END OF PHP - PUT UP THE FORM
?>
<form method="post">
  <p>NAME:<br> 
    <label>
    <input name="textfield" type="text" id="Name" size="50">
    </label>
</p>
  <p>
    <input name="GroupOne" type="radio" value="choiceone" />
    Choice One<br>
    <input name="GroupOne" type="radio" value="choicetwo" />
  Choice Two
  <label></label>
  </p>
  <p>    <input name="GroupTwo" type="radio" value="choiceone" />
    Choice One<br>
    <input name="GroupTwo" type="radio" value="choicetwo" />
  Choice Two
  <label></label><br>
    <input type="submit" name="go" value="Submit" />
  </p>
</form>

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33511440
The moving parts below are lines 15 - 20 (the other stuff is for my testing).  Copy those lines into your script to replace the line that says this:

    $p = serialize($_POST);

That should do it.  Best of luck with your project, ~Ray
<?php // RAY_temp_post.php

// TEST DATA FROM THE OP
// array(4) { ["textfield"]=> string(9) "Joe Smith" ["GroupOne"]=> string(9) "choicetwo" ["GroupTwo"]=> string(9) "choiceone" ["go"]=> string(6) "Submit" } THANKS

// CONSTRUCT AN ARRAY
$_POST["textfield"] = 'Joe Smith';
$_POST["GroupOne"]  = 'choicetwo';
$_POST["GroupTwo"]  = 'choiceone';
$_POST["go"] = 'Submit';

// DOES THE ARRAY LOOK RIGHT? YES, IT DOES.
var_dump($_POST);

// CONSTRUCT THE EMAIL BODY
$p = '';
$p .= $_POST["textfield"] . PHP_EOL;
$p .= $_POST["GroupOne"]  . PHP_EOL;
$p .= $_POST["GroupTwo"]  . PHP_EOL;

// USE THIS TO SHOW THE FORMATTED DATA THE WAY A PLAIN-TEXT EMAIL WOULD SHOW IT
echo "<pre>";

// SHOW WHAT WE CREATED
echo $p;

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question