Solved

Web Form

Posted on 2010-08-20
12
234 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
 

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 108

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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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 108

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 108

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 108

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now