PHP form isn't working - php

Can anyone see what's wrong with these two files? When I click submit, it just redirects me to a blank page and I get no feedback from the form what so ever let alone an email.
HTML code
<form action="../script/form14.php" method="POST" >
<h1>KittyBoo</h1>
<p>Beställ kattmat online - alltid laktos- och glutenfri</p><fieldset class="formular">
<legend>1. Välj produkt</legend>
<ul>
<li>
<input type="checkbox" name="Kitty Extra Small" value="Kitty Extra Small"> <li> Kitty Extra Small: 90 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Small" value="Kitty Small"> <li> Kitty Small: 140 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Medium" value="Kitty Medium"> <li> Kitty Medium: 180 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Large" value="Kitty Large"> <li> Kitty Large: 210 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Extra Large" value="Kitty Extra Large"> <li> Kitty Extra Large: 240 kr<br></li>
</ul>
</fieldset>
<fieldset class="formular">
<legend>2. Dina uppgifter</legend>
<label for="namn"> För- och efternamn:</label><br>
<input type="text" id="namn" name="from" required><br>
<label for="email">Epost:</label><br>
<input type="text" id="email" name="email" required><br>
<label for="emailconfirm">Konfirmera Epost:</label>
<input type="text" id="emailconfirm" name="emailconfirm" required>
<label for="adress">Adress:</label>
<input type="text" id="adress" name="adress" required>
<label for="postnummer">Postnummer:</label>
<input type="text" id="postnummer" name="postnummer" required>
<label for="stad">Stad:</label>
<input type="text" id="stad" name="stad" required>
<label for="land">Land:</label>
<select id="land" name="Land">
<option> Sverige</option>
<option> Finland</option>
<option> Norge</option>
<option> Danmark</option>
<option> Island</option>
</select>
</fieldset>
<fieldset class="formular">
<legend>3. Leverans</legend>
<label for="date">Välj datum för leverans:</label>
<input type="date" id="date" name="date" required/>
<p>Leveranssätt:</p>
<ul>
<li>
<input type="radio" name="radAnswer" required value="Posten"> <a> Posten</a>
<input type="radio" name="radAnswer" required value="DHL"> <a> DHL</a>
<input type="radio" name="radAnswer" required value="FedEx"> <a> FedEx</a>
</li>
</ul>
<p>Välj färg på din KittyBoo:</p>
<input type="radio" name="radAnswer" value="bla"> <a> Blå</a>
<input type="radio" name="radAnswer" value="rosa"> <a> Rosa</a>
<p>Övriga önskemål kring din leverans:</p>
<textarea name="subject" id="ovrig" rows="3" cols="30" maxlength="300" required> Max 300 ord</textarea>
</fieldset>
<input type="submit" value="skicka">
</form>
PHP Script
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["email"]))
{
// Check if "from" email address is valid
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$email = $_POST["email"];
function build_message($request_input){if(!isset($message_output))
{$message_output ="";}if(!is_array($request_input))
{$message_output = $request_input;}
else{foreach($request_input as $key => $value)
{if(!empty($value)){if(!is_numeric($key))
{$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}
else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("helenaevaschroder#gmail.com",$subject,$message, "From: $from\n");
echo "Tillbakaack för ditt meddelande";
echo "<p><a href=http://www.sh.se>Tillbaka till sidan</a></p>";
}
}
}
?>
The server is set up for PHP.
If it would be of any help, you can check the form out here:http://student.mtstudent.se/~sh14hf2407/pages/formular.html

Your PHP condition if (!isset($_POST["submit"])) { ... } is always FALSE because $_POST['submit'] doesn't exist in your html form. You have created an element <input type="submit" value="skicka"> but $_POST['submit'] means "an element which name attribute is submit". So replace :
<input type="submit" value="skicka">
by
<input type="submit" name="submit" value="skicka">
and this issue will be fixed.

change
<input type="submit" value="skicka">
into
<input type="submit" name="submit" value="skicka">

Related

Cannot send form with checkboxes PHP

I want to send a form that have checkboxes in it, it was working fine but I do not what I did that now I received an error everytime I send it. I think the problem has to do with the checkboxes because I recall that I had problems with them in the past. I am working with a single file in PHP.
This is the code for the form:
<div class="contactFrm">
<?php if(!empty($statusMsg)){ ?>
<p class="statusMsg <?php echo !empty($msgClass)?$msgClass:''; ?>"><?php echo $statusMsg; ?></p>
<?php } ?>
<form action="" method="post">
<h6>Contact Information</h6>
<fieldset id="left">
<h4>First Name</h4>
<input type="text" name="name" required>
<h4>Company Name</h4>
<input type="text" name="compname" required>
<h4>Phone Number</h4>
<input type="text" name="phone" required>
<h4>Date of Arrival</h4>
<input type="date" name="ardate" required>
</fieldset>
<fieldset id="right">
<h4>Last Name</h4>
<input type="text" name="lname" required>
<h4>Website URL</h4>
<input type="text" name="website" required>
<h4>Email</h4>
<input type="email" name="email" required>
<h4>Date of Departure</h4>
<input type="date" name="depdate" required>
</fieldset>
<h4>What is your estimated time of arrival?</h4>
<input type="text" name="estimated_time" required>
<h4>How many groups will you be hosting?</h4>
<textarea name="groups" required> </textarea>
<h4>How many participants per group do you need to accommodate?</h4>
<input type="text" name="mparticipants" required>
<h4>What are the date/time scheduled for each group?</h4>
<textarea name="scheduled" required> </textarea>
<h4>What kind of set-up will you like?</h4>
<p>Additional fees may apply for living room/specialty set-ups.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="living_room" id="CheckboxGroup1_0">
Living Room</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="conference_room" id="CheckboxGroup1_1">
Conference Room</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="other" id="CheckboxGroup1_2">
Other (please specify at the end of the form)</label>
<br>
</div></p>
<h6>Clients</h6>
<h4>How many individuals/clients from your company will be attending?</h4>
<p>These individuals will be stationed in the client room.</p>
<input type="text" name="attending">
<h6>Services</h6>
<h4>Which technology services are you interested in?</h4>
<p>Check all that apply (All of these services are charged separately. Some will involve additional costs from the estimate provided to you.)</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="sd_dvd" id="CheckboxGroup2_0">
Stationary SD DVD Recording</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="hd_video" id="CheckboxGroup2_1">
Stationary HD Video Recording</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="streaming" id="CheckboxGroup2_2">
High Definition Video Streaming (Focus Vision (SD) streaming is also available)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="smartboard" id="CheckboxGroup2_3">
Smartboard (also functions as TV/DVD/PC)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="translation" id="CheckboxGroup2_4">
Translation Equipment</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="videographer" id="CheckboxGroup2_5">
Videographer Assisted Recording (SD/HD)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup2[]" value="other" id="CheckboxGroup2_6">
Other (please specify on notes section)</label>
<br>
</div></p>
<h4>Will you need meal(s) to be provided?</h4>
<p>You will be able to pick from various menus once the booking is confirmed.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="participants" id="CheckboxGroup3_0">
Yes, for Participants</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="clients" id="CheckboxGroup3_1">
Yes, for Clients</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="breakfast" id="CheckboxGroup3_2">
Breakfast</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="lunch" id="CheckboxGroup3_3">
Lunch</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="dinner" id="CheckboxGroup3_4">
Dinner</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="snacks" id="CheckboxGroup3_5">
Snacks</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup3[]" value="beverages" id="CheckboxGroup3_6">
Beverages</label>
<br>
</div></p>
<h4>Do you need research service(s)?</h4>
<p>Check all that apply.</p>
<p><div class="inline-field">
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="moderator" id="CheckboxGroup4_0">
Moderator(s)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="recruiting" id="CheckboxGroup4_1">
Recruiting</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="reporting" id="CheckboxGroup4_2">
Reporting and Analysis</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="quantitative" id="CheckboxGroup4_3">
Quantitative</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="other" id="CheckboxGroup4_4">
Other Research Services (please specify in the notes section)</label>
<br>
<label>
<input type="checkbox" name="CheckboxGroup4[]" value="na" id="CheckboxGroup4_5">
N/A</label>
<br>
</div></p>
<h6>Additional Information</h6>
<h4>Please include any additional information or document that would help us facilitate your request.</h4>
<textarea name="additionalinfo"> </textarea>
<h4>Document Upload</h4>
<input type="file">
<input type="submit" name="submit" value="Submit">
<div class="clear"> </div>
</form>
and this is the PHP:
<?php
$statusMsg = '';
$msgClass = '';
if(isset($_POST['submit'])){
// Get the submitted form data
$name = $_POST['name'];
$lname = $_POST['lname'];
$compname = $_POST['compname'];
$phone = $_POST['phone'];
$ardate = $_POST['ardate'];
$website = $_POST['website'];
$email = $_POST['email'];
$depdate = $_POST['depdate'];
$estimated_time = $_POST['estimated_time'];
$groups = $_POST['groups'];
$mparticipants = $_POST['mparticipants'];
$scheduled = $_POST['scheduled'];
$additionalinfo = $_POST['additionalinfo'];
$attending = $_POST['attending'];
$CheckboxGroup1 = (implode("," , $_POST['CheckboxGroup1']));
$CheckboxGroup2 = (implode("," , $_POST['CheckboxGroup2']));
$CheckboxGroup3 = (implode("," , $_POST['CheckboxGroup3']));
$CheckboxGroup4 = (implode("," , $_POST['CheckboxGroup4']));
// Check whether submitted data is not empty
if(!empty($name) && !empty($lname) && !empty($compname) && !empty($phone) && !empty($ardate) && !empty($website) && !empty($email) && !empty($depdate) && !empty($estimated_time) && !empty($groups) && !empty($mparticipants) && !empty($scheduled) && isset($CheckboxGroup1) && isset($CheckboxGroup2) && isset($CheckboxGroup3) && isset($CheckboxGroup4) && !empty($attending) && !empty($additionalinfo)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$statusMsg = 'Please enter your valid email.';
$msgClass = 'errordiv';
}else{
// Recipient email
$toEmail = 'osek2112#gmail.com';
$emailSubject = 'Facility Request Submitted by '.$name;
$htmlContent = '<h2>Facility Request Form</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Last Name</h4><p>'.$lname.'</p>
<h4>Company Name</h4><p>'.$compname.'</p>
<h4>Phone Number</h4><p>'.$phone.'</p>
<h4>Date of Arrival</h4><p>'.$ardate.'</p>
<h4>Website URL</h4><p>'.$website.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Date of Departure</h4><p>'.$depdate.'</p>
<h4>What is your estimated time of arrival?</h4><p>'.$estimated_time.'</p>
<h4>How many groups will you be hosting?</h4><p>'.$groups.'</p>
<h4>How many participants per group do you need to accommodate?</h4><p>'.$mparticipants.'</p>
<h4>What are the date/time scheduled for each group?</h4><p>'.$scheduled.'</p>
<h4>What kind of set-up will you like?</h4><p>'.$CheckboxGroup1.'</p>
<h4>Which technology services are you interested in?</h4><p>'.$CheckboxGroup2.'</p>
<h4>How many individuals/clients from your company will be attending?</h4><p>'.$attending.'</p>
<h4>Will you need meal(s) to be provided?</h4><p>'.$CheckboxGroup3.'</p>
<h4>Do you need research service(s)?</h4><p>'.$CheckboxGroup4.'</p>
<h4>Additional Information</h4><p>'.$additionalinfo.'</p>';
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Additional headers
$headers .= 'From: '.$name.'<'.$email.'>'. "\r\n";
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'errordiv';
}
}
}else{
$statusMsg = 'Please fill all the fields.';
$msgClass = 'errordiv';
}
}
?>
Any help pointing out my error would be appreciated.
If you are getting this message:
Your contact request submission failed, please try again.
Then this portion of the code is throwing that message:
// Send email
if(mail($toEmail,$emailSubject,$htmlContent,$headers)){
$statusMsg = 'Your contact request has been submitted successfully !';
$msgClass = 'succdiv';
}else{
$statusMsg = 'Your contact request submission failed, please try again.';
$msgClass = 'errordiv';
}
Which means that the mail() function is returning false. Make sure that the mail() function works by passing in parameters you know are valid, such as those from the other contact form you say is working. Also, maybe try removing the $headers and see if that helps, since that part is optional.
i have checked your code and every time it's working but you don't handle it if checked box not selected that time occurring error just handle it,
may be use miss it checked boxed selection .
other wise what type of error occurring ?

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

PHP Array Output Error

When I submitted the form data, I received this error on Line 13 (array_push($order,$add_order);) of my PHP code: "Warning: Invalid argument supplied for foreach()..."
What's the best way to get this PHP code working?.
Here is the current email output (None of the data seems to be sending properly except for the Name & Phone number field):
Name: Alex
Phone: 5104545778
Item: Array
Quantity:
Add:
Message:
PHP:
<?php
if(isset($_POST['submit'])) {
$to = "test#mywebsite.com";
$subject = "New Order";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$order = array();
foreach($_POST['item'] as $item => $name) {
if ($_POST['quantity_'.$name] > 0) {
$add_order = array('pretty'=>$_POST['pretty-name_'.$name],'qty'=>$_POST['quantity_'.$name],'message'=>$_POST['message_'.$name]);
array_push($order,$add_order);
}
}
$body = "From: $name_field\nE-Mail: $email_field\n";
$body .= "Their Order:\n";
foreach ($order as $item){
$body .= "--".$item['qty']."x ".$item['pretty']."\n
Extra: ".$item['message']."\n\n";
}
echo "Data has been submitted to $to!";
mail($to, $subject, $body);
}
?>
HTML:
<form method="POST" action="neworder.php">
<div class ="item_left">
<img src="images/mexicantortas.jpg" border="2" width="200px" height="150px"><br>
Mexican Torta - $8.50<input name="item[]" type="hidden" value="torta"/>
<input name="pretty-name_torta" type="hidden" value="Mexican Torta"/><br>
How Many? <input name="quantity_torta" type="text" /><br>
<input name="message_torta" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_LEFT -->
<br />
<div class ="item_center">
<img src="images/fishsandwich.jpg" border="2" width="200px" height="150px"><br>
Fish Sandwich - $8.50<input name="item[]" type="hidden" value="fish"/>
<input name="pretty-name_fish" type="hidden" value="Fish Sandwhich"/><br>
How Many? <input name="quantity_fish" type="text" /><br>
<input name="message_fish" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_CENTER -->
<br />
<div class ="item_right">
<img src="images/hamburgers.jpg" border="2" width="200px" height="150px"><br>
Hamburger w/ Fries - $7.00<input name="item[]" type="hidden" value="hamburger"/>
<input name="pretty-name_hamburger" type="hidden" value="Hamburger"/><br>
How Many? <input name="quantity_hamburger" type="text" /><br>
<input name="message_hamburger" type="text" value="Enter special order instructions here..." />
</div><!-- ITEM_RIGHT -->
<br />
<div class="horizontal_form">
<div class="form">
<h2>Place Your Order Now: <font size="3"><font color="#037B41">Fill in the form below, and we'll call you when your food is ready to be picked up...</font></font></h2>
<p class="name">
<input type="text" name="name" id="name" style="text-align:center;" onClick="this.value='';" value="Enter your name"/>
</p>
<p class="phone">
<input type="text" name="phone" id="phone" style="text-align:center;" onClick="this.value='';" value="Enter your phone #"/>
</p>
<p class="submit">
<input type="submit" value="Place Order" name="submit"/>
</p>
</div><!-- FORM -->
</div><!-- HORIZONTAL_FORM -->
</form>
If item has no values anywhere it wont get posted to the receiving page.
Use isset to check if the value exists and is_array to check if it's an array.

PHP insert into database en check errors

Hi I'm doing a small project for learning php, and I wrote a few functions :
In the this piece of code, I basically check all the fields in my form and change values in a hash from false to true if they are filled in, at the end I loop the hash, and add the key (which is the error) to the array if its value is false.
function testBabysitterForAllElements(){
global $db;
$errorArray;
$naamarray["naam"]=false;
$naamarray["voornaam"]=false;
$naamarray["adres"]=false;
$naamarray["woonplaats"]=false;
$naamarray["postcode"]=false;
$naamarray["telefoonnummer"]=false;
$naamarray["geboortedatum"]=false;
$naamarray["adres"]=false;
$naamarray["wachtwoord"]=false;
$naamarray["email"]=false;
if(isset($_POST['element_1'])){
$naamarray["naam"]=true;
}
if(isset($_POST['element_2'])){
$naamarray["voornaam"]=true;
}
if(isset($_POST['element_3'])){
$naamarray["adres"]=true;
}
if(isset($_POST['element_4'])){
$naamarray["woonplaats"]=true;
}
if(isset($_POST['element_5'])){
$naamarray["postcode"]=true;
}
if(isset($_POST['element_6'])){
$naamarray["telefoonnummer"]=true;
}
if(isset($_POST['element_7'])){
$naamarray["email"]=true;
connectToDB();
$sql='SELECT inlognaam FROM kauffman.login WHERE inlognaam like \''.$_POST['element_7'].'\';';
$rows=$db->queryRow($sql);
if($rows){
array_push($errorArray,"email adres is reeds geregistreerd.");
}
}
if(isset($_POST['element_8_1']) && isset($_POST['element_8_2']) && isset($_POST['element_8_3'])){
$naamarray["geboortedatum"]=true;
}
if(isset($_POST['element_10']) && isset($_POST['element_10_1'])){
$naamarray['wachtwoord']=true;
}
foreach($naamarray as $key => $value){
if($value == false){
array_push($errorArray,$key);
}
}
if(!empty($errorArray)){return $errorArray;}
}
Here I add the fields to the DB, it calls the previous function to check if all fields are filled in.
function babysitterToevoegenAanDB(){
global $db;
if(isset($_POST['submit'])){
$errorArray=testBabysitterForAllElements();
echo $errorArray;
$succes=('succesvol toegevoegd');
$result;
if(!empty($errorArray)){
connectToDB();
$sql = "INSERT into kauffman.login(inlognaam,functie,paswoord) VALUES ('".antiInjectie($_POST['element_7'])."','gezin','".pwHashGenerator(antiInjectie($_POST['element_10_1']))."');";
$sql2 = 'SELECT serialKey from kauffman.login WHERE inlognaam='.antiInjectie($_POST['element_7']).';' ;
$result= $succes;
}else{
$result= $errorArray;
}
echo $result;
}
}
Here I create my form that also has the babysitterToevoegenAanDB() function. This and the form are returned and can be called on a page.
function babysitterForm(){
return babysitterToevoegenAanDB().' <form id="babysitForm" class="appnitro" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">
<div class="form_description">
<h2>Babysitter</h2>
<p>Gelieve je hier in te schrijven als babysitter</p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="element_1">Naam </label>
<div>
<input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value="'.
((isset($_POST['element_1']))? htmlentities($_POST['element_1']):'')
. '"/>
</div><p class="guidelines" id="guide_1"><small>Voer uw naam in.</small></p>
</li> <li id="li_2" >
<label class="description" for="element_2">Voornaam </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_2']))? htmlentities($_POST['element_2']):'').'"/>
</div><p class="guidelines" id="guide_2"><small>Vul uw voornaam in.</small></p>
</li> <li id="li_3" >
<label class="description" for="element_3">Adres </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_3']))? htmlentities($_POST['element_3']):'').'"/>
</div><p class="guidelines" id="guide_3"><small>vul uw straat en huisnummer in.</small></p>
</li> <li id="li_4" >
<label class="description" for="element_4">Woonplaats </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_4']))? htmlentities($_POST['element_4']):'').'"/>
</div><p class="guidelines" id="guide_4"><small>vul uw woonplaats in.</small></p>
</li> <li id="li_5" >
<label class="description" for="element_5">Postcode </label>
<div>
<input id="element_5" name="element_5" class="element text small" type="text" maxlength="255" value="'.((isset($_POST['element_5']))? htmlentities($_POST['element_5']):'').'"/>
</div><p class="guidelines" id="guide_5"><small>Vul uw Postcode hier in</small></p>
</li> <li id="li_6" >
<label class="description" for="element_6">telefoonnummer </label>
<div>
<input id="element_6" name="element_6" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_6']))? htmlentities($_POST['element_6']):'').'"/>
</div><p class="guidelines" id="guide_6"><small>Vul uw telefoonnummer in.</small></p>
</li> <li id="li_7" >
<label class="description" for="element_7">email </label>
<div>
<input id="element_7" name="element_7" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_7']))? htmlentities($_POST['element_7']):'').'"/>
</div><p class="guidelines" id="guide_7"><small>vul uw email in.</small></p>
</li> <li id="li_8" >
<label class="description" >Geboortedatum </label>
<span>
<input id="element_8_1" name="element_8_1" class="element text" size="2" maxlength="2" value="'.((isset($_POST['element_8_1']))? htmlentities($_POST['element_8_1']):'').'" type="text"> /
<label for="element_8_1">MM</label>
</span>
<span>
<input id="element_8_2" name="element_8_2" class="element text" size="2" maxlength="2" value="'.((isset($_POST['element_8_2']))? htmlentities($_POST['element_8_2']):'').'" type="text"> /
<label for="element_8_2">DD</label>
</span>
<span>
<input id="element_8_3" name="element_8_3" class="element text" size="4" maxlength="4" value="'.((isset($_POST['element_8_3']))? htmlentities($_POST['element_8_3']):'').'" type="text">
<label for="element_8_3">YYYY</label>
</span>
<li id="li_10" >
<label class="description" for="element_10">Wachtwoord </label>
<div>
<input id="element_10" name="element_10" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_10']))? htmlentities($_POST['element_10']):'').'"/>
<input id="element_10_1" name="element_10_1" class="element text medium" type="text" maxlength="255" value="'.((isset($_POST['element_10_1']))? htmlentities($_POST['element_10_1']):'').'"/>
</div><p class="guidelines" id="guide_10"><small>Vul uw wachtwoord twee maal in. Hierdoor voorkomt men typfouten door validatie.</small></p>
</li>
<li id="li_9" >
<label class="description" for="element_9">Opmerkingen </label>
<div>
<textarea id="element_9" name="element_9" class="element textarea medium" ></textarea>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="206335" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form> ';
}
What basically happens is :
I call the last function babysitteForm which shows a form to apply
BabysitterForm includes the babySitterToevoegenAanDB function that checks if submit is set
If so it checks all the fields if there are errors it has to return an array with errors
However if I click submit without filling anyhting in, there is nothing added to the errorArray.
Its normal that nothing gets added to your error array.
The function isset($_POST['anything']) will only check if that field is set in your post var.
The post var gets filled up by your form.
Basicly, if your form contains 7 input fields, all these input fields their value will be placed into
$_POST.
A print of your $_POST var would be something like:
print_r($_POST);
result:
array('element_1'=>'','element_2'=>'' ...
so your isset function will check if the value 'element_x' is set, wich it is, it just contains nothing.
u need to re-write your checks so they check the content, not the existance. Isset is only of
use when checking f.e. $_GET vars if u are not sure they are set and not post vars in a form (since these will always be set).
I would suggest re-writing your checks in the form of:
$naamarray["naam"]=true;
$naamarray["voornaam"]=true;
$naamarray["adres"]=true;
$naamarray["woonplaats"]=true;
$naamarray["postcode"]=true;
$naamarray["telefoonnummer"]=true;
$naamarray["geboortedatum"]=true;
$naamarray["adres"]=true;
$naamarray["wachtwoord"]=true;
$naamarray["email"]=true;
if($_POST['element_1'] == ""){
$naamarray["naam"]=false;
}
do this for all your checks.
also reverse your foreach loop:
foreach($naamarray as $key => $value){
if($value == true){
array_push($errorArray,$key);
}
}
I also have some doubts with the returning of the function before your form. I think its clearer
to create a function and just include the php page instead of this.
then u would get code like:
require_once('inc/functions.php');
if ($_SERVER['REQUEST_METHOD'] == "POST") {
babysitterToevoegenAanDB();
} else {
babysitterForm();
}
And the final tip: give your input field clear names, not just element_x ... .

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