?
Solved

Web Form

Posted on 2010-08-20
12
Medium Priority
?
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 

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 111

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 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

Originally, this post was published on Monitis Blog, you can check it here . Websites are getting bigger and more complicated by the day. Video, images and custom fonts are all great for showcasing your product or service. But the price to pay in…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 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