web form to email php error - php

I have been using php to try and email the user entered content from a web form to an email address. Been messing around with the php code to no avail. Here's the web form:
<form action="form2email.php" method="post">
<div id="problem" class="fluid">
<textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>
</div>
<div id="email" class="fluid">
<input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">
<label for="yes">Yes</label>
<input name="publish" type="radio" value="Yes">
<label for="no">No</label>
<input type="radio" name="publish" value="No">
<label for="responder"><b>Michael</b></label>
<input name="responder" type="checkbox" value="Michael" id="responder">
<label for="responder"><b>Jennifer</b></label>
<input type="checkbox" name="responder" value="Jennifer" id="responder">
<label for="responder"><b>Mei</b></label>
<input type="checkbox" name="responder" value="Mei" id="responder">
<label for="responder"><b>Random</b></label>
<input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
<input type="submit" value="Submit">
</div>
</form>
and here's the php:
<?php
$emailBody= 'problem: '.$_POST['problem']."\n".
'email: '.$_POST['email']."\n".
'publish: '.$_POST['publish']."\n".
'responder: '.$_POST['responder']."\n"
if(isset($_POST['submit'])) {
mail('testing#gmail.com', 'problem' , $emailBody);
header('location: thankyoupage.HTML');
}
else{
header('location: testing.html');
}
?>
The problems vary when I mess around with the code but it always has a problem with everything after the variable. The only way not to generate an error message is by making th variable at the end of the if else statements.
In this case shown above the form wont work at all it just takes me to a page that says "Object not found error 404". And on the php file it says there's an unexpected T_if but if I put a semi-colon at the end of the variable the script doesn't work at all. Help will be much appreciated and thank you in advance.

2 issues with your form and your handler. (N.B.: See my EDIT a little further down).
1) Form: Missing name for submit button <input type="submit" value="Submit">
Without it being named, your handler will never execute the mail() function.
2) PHP handler: Missing semi-colon at the end of 'responder: '.$_POST['responder']."\n"
Tested and working for me
HTML FORM
<form action="form2email.php" method="post">
<div id="problem" class="fluid">
<textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>
</div>
<div id="email" class="fluid">
<input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">
<label for="yes">Yes</label>
<input name="publish" type="radio" value="Yes">
<label for="no">No</label>
<input type="radio" name="publish" value="No">
<label for="responder"><b>Michael</b></label>
<input name="responder" type="checkbox" value="Michael" id="responder">
<label for="responder"><b>Jennifer</b></label>
<input type="checkbox" name="responder" value="Jennifer" id="responder">
<label for="responder"><b>Mei</b></label>
<input type="checkbox" name="responder" value="Mei" id="responder">
<label for="responder"><b>Random</b></label>
<input type="checkbox" name="responder" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
<input type="submit" value="Submit" name="submit">
</div>
</form>
PHP handler
<?php
$emailBody= 'problem: '.$_POST['problem']."\n".
'email: '.$_POST['email']."\n".
'publish: '.$_POST['publish']."\n".
'responder: '.$_POST['responder']."\n";
if(isset($_POST['submit'])) {
mail('testing#gmail.com', 'problem' , $emailBody);
header('location: thankyoupage.HTML');
}
else{
header('location: testing.html');
}
?>
EDIT
Another thing I noticed is that you have multiple choices used as checkboxes.
There are a few more things that need to be added in order for it to work properly and show each checked box in the Email.
1) Using a foreach in the PHP handler:
foreach ($_POST['responder'] as $value) {
$check_msg .= "Checked: $value\n";
}
2) Adding brackets to treat each checkbox as an array:
I.e.: name="responder[]"
New Handler:
<?php
foreach ($_POST['responder'] as $value) {
$check_msg .= "Checked: $value\n";
}
$emailBody= 'problem: '.$_POST['problem']."\n".
'email: '.$_POST['email']."\n".
'publish: '.$_POST['publish']."\n".
'' . $check_msg . "\n";
if(isset($_POST['submit'])) {
mail('testing#gmail.com', 'problem' , $emailBody);
header('location: thankyoupage.HTML');
}
else{
header('location: testing.html');
}
?>
New form:
<form action="form2email.php" method="post">
<div id="problem" class="fluid">
<textarea name="problem" cols="50" rows="4" placeholder="enter problem here" row="4"></textarea>
</div>
<div id="email" class="fluid">
<input type="email" name="email" placeholder="email optional">
</div>
<div id="userOptions" class="fluid">
<label for="yes">Yes</label>
<input name="publish" type="radio" value="Yes">
<label for="no">No</label>
<input type="radio" name="publish" value="No">
<label for="responder"><b>Michael</b></label>
<input name="responder[]" type="checkbox" value="Michael" id="responder">
<label for="responder"><b>Jennifer</b></label>
<input type="checkbox" name="responder[]" value="Jennifer" id="responder">
<label for="responder"><b>Mei</b></label>
<input type="checkbox" name="responder[]" value="Mei" id="responder">
<label for="responder"><b>Random</b></label>
<input type="checkbox" name="responder[]" value="Random" id="responder">
</div>
<div id="submit" class="fluid">
<input type="submit" value="Submit" name="submit">
</div>
</form>
Choosing 3 names from the form will produce something similar to the following:
Checked: Jennifer
Checked: Mei
Checked: Random

Related

php post superglobal array doesn't contain all keys from form

I have this html form in my php script
<form action="" method="post">
<div style="width: 30em;">
<label for="firstName">First name *</label>
<input type="text" name="firstName" id="firstName" value=""/>
<label for="lastName">Last name *</label>
<input type="text" name="lastName" id="lastName" value=""/>
<label for="password1">Choose a password *</label>
<input type="password" name="password1" id="password1" value=""/>
<label for="password2">Retype password *</label>
<input type="password" name="password2" id="password2" value=""/>
<label>Your gender: *</label>
<label for="genderMale">Male</label>
<input type="radio" name="gender1" id="genderMale" value="M" />
<label for="genderFemale">Female</label>
<input type="radio" name="gender" id="genderFemale" value="F" />
<label for="favoriteWidget">What's your favorite widget? *</label>
<select name="favoriteWidget" id="favoriteWidget" size="1">
<option value="superWidget">The SuperWidget</option>
<option value="megaWidget">The MegaWidget</option>
<option value="wonderWidget">The WonderWidget
</option>
</select>
<label for="newsletter">Do you want to receive our newsletter?</label>
<input type="checkbox" name="newsletter" id="newsletter" value="yes"/>
<label for="comments">Any comments?</label>
<textarea name="comments" id="comments" rows="4" cols="50"></textarea>
<div style="clear: both;">
<input type="submit" name="submitButton" id="submitButton" value="Send Details"/>
<input type="reset" name="resetButton" id="resetButton" value="Reset Form" style="margin-right: 20px;"/>
</div>
</div>
</form>
Problem is when i submit my form and run this php code:
if ( isset($_POST["submitButton"]) ) {
echo '<pre>' . print_r($_POST, true) . '</pre>';
}
I get the following results:
Array
(
[firstName] =>
[lastName] =>
[password1] =>
[password2] =>
[favoriteWidget] => superWidget
[comments] =>
[submitButton] => Send Details
)
Most noticeably the gender and newsletter keys in the $_POST array are missing?
What could be the reason for this?
All checkboxes are boolean for browsers.
This is not about PHP but HTML.
to verify that the person chose newsletter field, try this:
$_POST['newsletter'] = isset($_POST['newsletter']) ? $_POST['newsletter'] : 'no';
This force your $_POST set newsletter when form not checked!
isset() verify if your variable exists, then put default value else 'no' for this value.
you can use array_key_exists() instead of isset().
$_POST['newsletter'] = array_key_exists('newsletter', $_POST) ? $_POST['newsletter'] : 'no';

html form-- need help having new input area populate if radio button is clicked

I am making a basic new customer database with mysql and php. I would like when people click on the radio button"different mailing address" for another couple of input fields to appear for the mailing address. Im not quite sure how to handle this with inputs and not variables. Is there a way to do an if statement here is my html form code below
<form method="POST" action="insert.php">
<fieldset>
<legend>New Customer data</legend>
<label>Complete Below</label>
<div class="controls controls-row">
<input class="span4" name="firstname" type="text" placeholder="First name">
<input class="span3" name="lastname" type="text" placeholder="Last Name">
<input class="span3" name="phone" type="text" placeholder="Phone">
</div>
<div class="controls controls-row">
<input class="span4" name="address" type="text" placeholder="Address">
<input class="span2" name="city" type="text" placeholder="City">
<input class="span1" name="state" type="text" placeholder="State">
</div>
<div class="controls controls-row">
<input class="span4" name="zip" type="text" placeholder="Zip Code">
<input class="span2" name="email" type="text" placeholder="Email">
<input class="span2" name="ccemail" type="text" placeholder="cc email">
</div>
<span class="help-block">When is the customers due date monthly</span>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios1" value="1" checked>
1st of the month</label>
<label class="radio">
<input type="radio" name="duedate" id="optionsRadios2" value="15">
15th of the month
</label>
<span class="help-block">Mailing address</span>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios3" value="1" checked>
Check if mailing address is the same as the service address
</label>
<label class="radio">
<input type="radio" name="mailingaddress" id="optionsRadios4" value="0">
Check if mailing address is different
</label>
<button type="submit" class="btn btn-primary">Submit</button>
</fieldset>
</form>
Using jQuery, have two inputs that are initially hidden. Once the user checks the box, '$.show()' on those two inputs. Id have a div containing two inputs, and just show or hide the div depending on whether the box is checked or not
you can try this.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" >
</script>
<script>
$(document).ready(function(){
$("#checkme").on('change',function(){
if($("#checkme").is(":checked")){
$("input#myemail").fadeIn();
}else{
$("input#myemail").fadeOut();
}
});
});
</script>
</head>
<body>
show: <input type="checkbox" id="checkme" /><br />
<input type="text" id="myemail" hidden />
</body>
</html>

No new line before form

First my code:
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post">
<P>
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</P>
</FORM>
';
?>
So if i run that on my xampp-Server, it shows a "Hello" and the Form in a new line.
What must I do that all this is written in one line?
Thanks
You need to remove the <p> element and display the form inline.
<?php
echo 'Hello
<FORM ACTION="uebung3.php" METHOD="post" style="display:inline">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>
Remove the paragraph break '<P>'
You should really use a heredoc for this type of HTML output in PHP. Technically, the <p> tag should be around the Hello, not the form. You're looking for something like:
<?php
echo <<<EOT
Hello
<FORM ACTION="uebung3.php" METHOD="post">
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
EOT;
?>
Even by removing <p> tags, your hello will still appear on a seperate line. This is because it is outside your <form> tag.
Put it inside <form> like this, while removing the <p> tags:
<?php
echo '<FORM ACTION="uebung3.php" METHOD="post">Hello
<LABEL FOR="vorname">Vorname: </LABEL>
<INPUT TYPE="text" NAME="vorname">
<LABEL FOR="nachname">Nachname: </LABEL>
<INPUT TYPE="textarea" NAME="nachname">
<LABEL FOR="email">E-Mail: </LABEL>
<INPUT TYPE="text" NAME="email">
<INPUT TYPE="radio" NAME="geschlecht" VALUE="Maskulin"> Maskulin
<INPUT TYPE="checkbox" NAME="geschlecht" VALUE="Feminin"> Feminin
<input type="password" for="pw" NAME="PW">
<INPUT TYPE="submit" VALUE="Absenden">
<INPUT TYPE="reset" VALUE="Zurücksetzen">
</FORM>
';
?>

Cannot store radio button value in table

I am trying to save the value of the radio buttons in my database table choice. I get the message Data saved successfully but no value is stored in the table.
Form:
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<center<legend>Choose in which category you'd like to be included</legend></center>
<p><input type="radio" name="choice[]" value="player" id="player" class="custom" />
<label for="player">Player</label>
<input type="radio" name="choice[]" value="coach" id="coach" class="custom" />
<label for="coach">Coach</label>
<input type="radio" name="choice[]" value="supporter" id="supporter" class="custom" />
<label for="supporter">Supporter</label>
<input type="radio" name="choice[]" value="sponsor" id="sponsor" class="custom" />
<label for="sponsor">Sponsor</label>
<input type="radio" name="choice[]" value="alumni" id="alumni" class="custom" />
<label for="alumni">Alumni</label>
<input type="radio" name="choice[]" value="other" id="o" class="custom" />
<label for="o">Other</label>
</fieldset>
</div>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
</body>
</html>
PHP:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$choice = $mysqli->real_escape_string($_POST['choice']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`,`choice`) VALUES ('".$name."','".$email."','".$phone."','".$other."','".$choice."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
var_dump($_POST) to sere what data flooding in.
One thing more to check if its save or submit ? in $_POST['save']
EDIT
After getting your full form - the error lies in your center tag
Change <center<legend> TO <center><legend>
The error - ↑ in this tag

Radio Buttons with PHP Form Handling

I have a basic form that I am submitting using some basic PHP. I have the form submission working great, except that I have a radio button (for preferred method of contact) and I am not sure how to add that in the PHP so that sends in the email. Both radio button options have the same name, so that isn't working as the value. My code is below.
The PHP is as follows:
<?php
$name = stripslashes($_POST['name']);
$email = stripslashes($_POST['email']);
$phone = stripslashes($_POST['phone']);
$contact = stripslashes($_POST['contact']);
$message = stripslashes($_POST['message']);
$form_message = "Name: $name \nEmail: $email \nPhone: $phone \nPreferred Method of Contact: $contact \nMessage: $message";
// Exit process if field "human" is filled (because this means it is spam)
if ( $_POST['human'] ) {
echo 'Tastes Like Spam!'; exit; }
// if it is not filled, submit form
else {
header( "Location: http://www.newurl.com");
mail("myemail#gmail.com", "Email Subject", $form_message, "From: $email" );
}
?>
The HTML for the form is below:
<form method="post" id="form" action="handle_form.php">
<div class="field">
<input type="text" name="human" id="human" class="txt" />
</div>
<div class="field form-inline">
<label class="contact-info" for="txtName">Name*</label>
<input type="text" name="name" id="name" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtEmail">Email*</label>
<input type="text" name="email" id="email" class="txt" value=""/>
</div>
<div class="field form-inline">
<label class="contact-info" for="txtPhone">Phone</label>
<input type="text" name="phone" id="phone" class="txt" value=""/>
</div>
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" /> <span>Phone</span>
</div>
<div class="field form-inline">
<textarea rows="10" cols="20" name="message" id="message" class="txt" value=""></textarea>
</div>
<div class="submit">
<input class="submit" type="submit" name="submit" value="Submit Form">
</div>
</form>
Thanks so much for the help!
<div class="field form-inline radio">
<label class="radio" for="txtContact">Preferred Method of Contact</label>
<input class="radio" type="radio" name="contact" value="email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="phone" /> <span>Phone</span>
</div>
Note the added value attribute.
And the PHP:
$contact = $_POST['contact']
//Will return either "email" or "phone".
You radios need values:
<input class="radio" type="radio" value="email" name="contact" checked /> <span>Email</span>
<input class="radio" type="radio" value="phone" name="contact" /> <span>Phone</span>
Just give your radio inputs a value-attribute. This is what will get submitted via POST. You can then access it via $_POST['nameofradio']
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>
Easy! Just add a value to your radio buttons.
<input class="radio" type="radio" name="contact" value="Email" checked /> <span>Email</span>
<input class="radio" type="radio" name="contact" value="Phone" /> <span>Phone</span>

Categories