Solved

Web Form

Posted on 2010-08-20
12
244 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IFRAME in a ASPX file no longer working 3 27
updating the date data 12 23
how to handle $x_amount to payment gateway in php - stumped. 14 14
Objects on Same Line 2 18
Find out what you should include to make the best professional email signature for your organization.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

920 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

12 Experts available now in Live!

Get 1:1 Help Now