[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 284
  • Last Modified:

Web Form

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
dmrussell12345
Asked:
dmrussell12345
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Casey HermanCommented:
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
 
zogmitCommented:
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
 
dmrussell12345Author Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
zogmitCommented:
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
 
Ray PaseurCommented:
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
 
dmrussell12345Author Commented:
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
 
dmrussell12345Author Commented:
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
 
Ray PaseurCommented:
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
 
dmrussell12345Author Commented:
I apologize for the ignorance... where do I include the code to activate the var-dump?

0
 
Ray PaseurCommented:
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
 
dmrussell12345Author Commented:
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
 
Ray PaseurCommented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now