PHP insert into database en check errors - php

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 ... .

Related

PHP Missing User Input from form when sent to email

I created a form using phpform.org And with some help from Mohamed Athif have now got a partially working mail.php , I am now receiving emails but I cant get the user input into the email. I Understand That I have to add the elements in for each field in the .php file. If someone can give some guidance to getting these few element working withing the email so I can then apply them to the all the other elements myself here is the html code.
For Element 1,8,6 in the email I want it to be as follows or similar to this
What alias do you want to use - (User Input)
Is this about - (whatever the user clicked either good Job or bad Job)
What is the name of the workplace.. (user Input)
Currently in the email Im getting this
From: alias
Message: Is this about a , Where is this Business Located? element6,
so you can see that the user input is missing.
<form method="post" action="mail.php">
<div class="form_description">
<h2>GoodJob - BadJob</h2>
<p>some text here</p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="element_1">What is the Alias you want to use </label>
<div>
<input id="element_1" name="element_1" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_1"><small>You don't have to use your real name - Just remember your Alias for Comments</small></p>
</li> <li id="li_8" >
<label class="description" for="element_8">Is this about a ...... </label>
<span>
<input id="element_8_1" name="element_8" class="element radio" type="radio" value="1" />
<label class="choice" for="element_8_1">Bad job</label>
<input id="element_8_2" name="element_8" class="element radio" type="radio" value="2" />
<label class="choice" for="element_8_2">Good Job</label>
</span>
</li> <li id="li_6" >
<label class="description" for="element_6">What is the name of the workplace you want to tell us about? </label>
<div>
<input id="element_6" name="element_6" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_6"><small>Type in the Name of the Business</small></p>
</li> <li id="li_16" >
<label class="description" for="element_16">Where is this Business Located? </label>
<div>
<input id="element_16" name="element_16" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_16"><small>Type in the town, or you can find the business on google maps and click on share and copy the link and paste it here.</small></p>
</li> <li id="li_7" >
<label class="description" for="element_7">If you want to mention the bosses name, Put it here. </label>
<div>
<input id="element_7" name="element_7" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_7"><small>Type in the Name of the Boss</small></p>
</li> <li id="li_15" >
<label class="description" for="element_15">If you know the personal page you can copy and paste it in here </label>
<div>
<input id="element_15" name="element_15" class="element text large" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_15"><small>Go to the top address bar of the boss/owner you want to tag for this post. Select all the text and paste it in the box.</small></p>
</li> <li id="li_9" >
<label class="description" for="element_9">How many hours a week do you work? </label>
<span>
<input id="element_9_1" name="element_9" class="element radio" type="radio" value="1" />
1 to 10
10 to 20
20 to 30
30 to 40
40 to 50
50 hours Plus
</span><p class="guidelines" id="guide_9"><small>Keep it REAL. </small></p>
</li> <li id="li_18" >
<label class="description" for="element_18">Exactly how many hours do you work? </label>
<div>
<input id="element_18" name="element_18" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li> <li id="li_11" >
<label class="description" for="element_11">Does your Job Pay Extra Hours? </label>
<span>
<input id="element_11_1" name="element_11" class="element radio" type="radio" value="1" />
Yes
No
</span>
</li> <li id="li_10" >
<label class="description" for="element_10">What type of contract do you have? </label>
<span>
<input id="element_10_1" name="element_10" class="element radio" type="radio" value="1" />
Part Time
Full Time
Hourly per week
Dont know
</span><p class="guidelines" id="guide_10"><small>DON'T GUESS..This is crucial. Every worker MUST have a copy of their contract. If you don't have one ask your boss or go to the Employment office with ALL your credentials and they will tell you there what contract you have.</small></p>
</li> <li id="li_12" >
<label class="description" for="element_12">Did you get Holiday pay? </label>
<span>
<input id="element_12_1" name="element_12" class="element radio" type="radio" value="1" />
Yes
No
</span>
</li> <li id="li_17" >
<label class="description" for="element_17">How much holiday pay did you get? </label>
<div>
<input id="element_17" name="element_17" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_17"><small>A NUMBER ONLY no symbols or text</small></p>
</li> <li id="li_13" >
<label class="description" for="element_13">Did you know That </label>
<span>
<input id="element_13_1" name="element_13" class="element radio" type="radio" value="1" />
Yes
No
</span><p class="guidelines" id="guide_13"><small>Its true, In Spain ins a 14 month pay scheme, where you get a double pay in summer and another Christmas. Most employers will actually incorporate these two payments within your monthly wage</small></p>
</li> <li id="li_14" >
<label class="description" for="element_14">Got a story to tell about this Job? Bad or good - let it rip here!</label>
<div>
<textarea id="element_14" name="element_14" class="element textarea large"></textarea>
</div>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="1136056" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
and the PHP is as follows
<?php
$element1 = $_POST['element_1'];
$element8 = $_POST['element_8'];
$element6 = $_POST['element_6'];
$formcontent="From: $element1 \n Message: Is this about a $element81, Where is this Business Located? element6, blah blah blah";
$recipient = "me#gmail.com";
$subject = "Contact Form";
$mailheader = "From: $element1 \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "<script type='text/javascript'>alert('Mail sent successfully!')</script>";
?>
In your original code, you
1) called an non-existing variable $element81
2) there is a $ sign missing before element6
that's why you didn't get the required content.
The code below will give you the following content in your email (as per your request)
What alias do you want to use - (User Input)
Is this about - (whatever the user clicked either good Job or bad Job)
What is the name of the workplace.. (user Input)
replace
$formcontent="From: $element1 \n Message: Is this about a $element81, Where is this Business Located? element6, blah blah blah";
with
$formcontent="What alias do you want to use - $element1\n";
$formcontent.="Is this about - $element8\n";
$formcontent.="Where is this Business Located? - $element6\n";

PHP form isn't working

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">

I am not able to save my form data to mysql database using PHP

I am a bit newbie in PHP and having real trouble here, I dont know the reason why it does not save form data, please help me if you find something wrong. i had tried a lot to edit but it does not work at all. Thanks in advance.
<?php
session_start();
if(!$_SESSION['username'])
{
header("Location: index.php");//redirect to login page to secure the welcome page without login access.
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Registeration Form</title>
<link rel="stylesheet" type="text/css" href="css/view.css" media="all">
<script type="text/javascript" src="js/view.js"></script>
<script type="text/JavaScript" src="js/forms.js"></script>
</head>
<body id="main_body" >
<img id="top" src="images/top.png" alt="">
<div id="form_container">
<h1>Registeration Form</h1>
<div id="wrap">
<!-- Here's all it takes to make this navigation bar. -->
<ul id="nav">
<li>Home</li>
<li>Register</li>
<li>Edit</li>
<li>View</li>
<li>Delete</li>
<li>Logout</li>
</ul>
<!-- That's it! -->
</div>
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST" class="appnitro">
<div class="form_description">
<h2>Registeration Form</h2>
<p>Please fill the form</p>
</div>
<label class="description">Name </label>
<input name="name" class="element text medium" type="text" maxlength="255" value=""/>
<label class="description">Email </label>
<input name="email" class="element text medium" type="text" maxlength="255" value=""/>
<label class="description" >Department </label>
<input name="department" class="element text medium" type="text" maxlength="255" value=""/>
<label class="description">Respective Professor</label>
<input name="professor" class="element text medium" type="text" maxlength="255" value=""/>
<label class="description">Position</label>
<select class="element select medium" name="position">
<option value="" selected="selected"></option>
<option value="Student-Bachelors" >Student-Bachelors</option>
<option value="Student-Masters" >Student-Masters</option>
<option value="Student-PhD" >Student-PhD</option>
<option value="Internee" >Internee</option>
<option value="Employee" >Employee</option>
</select>
<label class="description">Duration</label>
<span>
<input name= "duration_start" class="element text" maxlength="255" size="14" value=""/>
<label>Start</label>
</span>
<span>
<input name= "duration_end" class="element text" maxlength="255" size="14" value=""/>
<label>End</label>
</span>
<label class="description">Room & Building</label>
<span>
<input name= "room" class="element text" maxlength="255" size="14" value=""/>
<label>Room</label>
</span>
<span>
<input name= "building" class="element text" maxlength="255" size="14" value=""/>
<label>Building</label>
</span>
<label class="description" >Address </label>
<input name="address" class="element text large" value="" type="text">
<label class="description" >Phone Number </label>
<input name="phone_number" class="element text" maxlength="255" value="" type="text">
<input type="submit" name="submit" id="submit" value="Create my account" title="" border="0">
</form>
<div id="footer"> Login as:
<?php
echo $_SESSION['username'];
?>,
</div>
</div>
<img id="bottom" src="images/bottom.png" alt="">
</body>
</html>
<?php
require_once("db_connect.php");//make connection here
if(isset($_POST['submit']))
{
$name=$_POST['name'];//here getting result from the post array after submitting the form.
$email=$_POST['email'];//same
$department=$_POST['department'];//same
$professor=$_POST['professor'];//same
$position=$_POST['position'];//same
$duration_start=$_POST['duration_start'];//same
$duration_end=$_POST['duration_end'];//same
$room=$_POST['room'];//same
$building=$_POST['building'];//same
$address=$_POST['address'];//same
$phone_number=$_POST['phone_number'];//same
if($name=='')
{
//javascript use for input checking
echo"<script>alert('Please enter the name')</script>";
exit();//this use if first is not work then other will not show
}
if($email=='')
{
echo"<script>alert('Please enter the email')</script>";
exit();
}
if($department=='')
{
echo"<script>alert('Please enter the department')</script>";
exit();
}
if($professor=='')
{
echo"<script>alert('Please enter the professor')</script>";
exit();
}
if($position=='')
{
echo"<script>alert('Please enter the position')</script>";
exit();
}
if($duration_start=='')
{
echo"<script>alert('Please enter the duration end')</script>";
exit();
}
if($duration_end=='')
{
echo"<script>alert('Please enter the duration start')</script>";
exit();
}
if($room=='')
{
echo"<script>alert('Please enter the room')</script>";
exit();
}
if($building=='')
{
echo"<script>alert('Please enter the building')</script>";
exit();
}
if($address=='')
{
echo"<script>alert('Please enter the address')</script>";
exit();
}
if($phone_number=='')
{
echo"<script>alert('Please enter the phone number')</script>";
exit();
}
//here query check weather if user already registered so can't register again.
$check_email_query="select * from new_members WHERE email='$email'";
$run_query=mysqli_query($mysqli,$check_email_query);
if(mysqli_num_rows($run_query)>0)
{
echo "<script>alert('Email $email is already exist in our database, Please try another one!')</script>";
exit();
}
//insert the user into the database.
$insert_user="insert into new_members(name,email,professor,department,position,duration_start,duration_end,room,building,address,phone_number) VALUES ('$name','$email','$professor','$department','$position','$duration_start','$duration_end','$room','$building','$address','$phone_number')";
if(mysqli_query($mysqli,$insert_user))
{
header("Location:manage.php");
exit();
//echo "<script>window.open('manage.php','_self')</script>";
}
}
?>

Issue with PHP Email script

I have a HTML form within one of my webpages, and i am now having trouble configuring it to the sendmail.php. When I fill in the form, and click submit, its asking me to save the file 'sendmail.php' but I want it to send to the email specified in the 'sendmail.php' file.
in my 'sendmail.php' file I currently have:
<?php
$element_1_1 = $_POST['element_1_1'];
$element_1_2 = $_POST['element_1_2'];
if(isset($_POST['submit'])){
$to = "sales#atechdigital.co.uk"; // this is your Email address
$from = $_POST['element_4']; // this is the sender's Email address
$first_name = $_POST['element_1_1'];
$last_name = $_POST['element_1_2'];
$subject = "Form submission";
$message = $element_1_1 . " " . $element_1_2 . " Has Sent A Quote Request";
$element_2 = $_POST['element_2'];
$element_3 = $_POST['element_3'];
$element_4 = $_POST['element_4'];
$element_5_1 = $_POST['element_5_1'];
$element_5_2 = $_POST['element_5_2'];
$element_5_3 = $_POST['element_5_3'];
$element_5_4 = $_POST['element_5_4'];
$element_5_5 = $_POST['element_5_5'];
$element_5_6 = $_POST['element_5_6'];
mail ($to,$element_4,$element_2,$element_3,$element_4,$element_5,$element_5_1,$element_5_2,$element_5_3,$element_5_4,$element_5_5,$element_5_6);
echo " Thank you " . $first_name . ", One of our sales team is dealing with you're request! We will contact you shortly.";
}
?>
and the html form is as follows:
<form id="form_824601" class="appnitro" method="post" action="sendmail.php">
<ul >
<li id="li_1" >
<label class="description" for="element_1">Name </label>
<span>
<input id="element_1_1" name= "element_1_1" class="element text" maxlength="255" size="8" value=""/>
<label>First</label>
</span>
<span>
<input id="element_1_2" name= "element_1_2" class="element text" maxlength="255" size="14" value=""/>
<label>Last</label>
</span>
</li>
<li id="li_2" >
<label class="description" for="element_2">Company Name </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_3" >
<label class="description" for="element_3">Enter Contact Telephone Number </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_4" >
<label class="description" for="element_4">Enter Email Address </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_5" >
<label class="description" for="element_5">Request A Quote For ... </label>
<span>
<input id="element_5_1" name="element_5" class="element radio" type="radio" value="1" />
<label class="choice" for="element_5_1">Web Design</label>
<input id="element_5_2" name="element_5" class="element radio" type="radio" value="2" />
<label class="choice" for="element_5_2">SEO</label>
<input id="element_5_3" name="element_5" class="element radio" type="radio" value="3" />
<label class="choice" for="element_5_3">Social Media</label>
<input id="element_5_4" name="element_5" class="element radio" type="radio" value="4" />
<label class="choice" for="element_5_4">Graphic Design</label>
<input id="element_5_5" name="element_5" class="element radio" type="radio" value="5" />
<label class="choice" for="element_5_5">Video Marketing</label>
<input id="element_5_6" name="element_5" class="element radio" type="radio" value="6" />
<label class="choice" for="element_5_6">Maintenance</label>
</span>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="824601" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
I Hope someone can shed light on the issue I'm having!!
This will never work while you are running the script locally. You need to run it on a server that can handle PHP. The only way you could run it locally and that is if you have a local web server set up.
I hope this helps!
You can't run .php files just from the folder, you will need a web server to handle it for you. if you are running windows or mac , you can set one up using software like XAMPP (win + osx) , WAMP (win) , MAMP (osx).
If you happen you run a linux distribution, you can set up a LAMP server.
Here are some links:
https://www.apachefriends.org/index.html
http://www.wampserver.com/en/
http://www.mamp.info/en/
From the links above, xampp has always worked out the best for me performance wise. parsing a huge xml feed and putting it into a mysql database takes like half the time on xampp compared to wamp.

how to insert multiple fields into mysql

Hi I'm trying to submit a webform and insert the data into mysql. The problem I have is that I have setup the fields to have multiple input boxes. For example instead of just "name" I have "first name", "last name" and my address is divided into many fields to ensure the end user does not miss certain parts of their address.
The problem I have is that I can't submit those multiple fields into mysql. If I wanted to simply submit the "name" field I can, but not first name + last name. To be more clear, I have a column named "name" and I want the "firstname" and "lastname" both to be stored there. Same thing with the address.
Here is my html code
<ul>
<li id=name">
<label class="description" for="element_1">Name </label>
<span>
<input id="first" name= "first" class="element text" maxlength="255" size="8" value=""/>
<label>First</label>
</span>
<span>
<input id="last" name= "last" class="element text" maxlength="255" size="14" value=""/>
<label>Last</label>
</span>
</li>
<li id="phone">
<label class="description" for="element_2">Phone </label>
<span>
<input id="parea" name="parea" class="element text" size="3" maxlength="3" value="" type="text" /> -
<label for="parea">(###)</label>
</span>
<span>
<input id="pfirst" name="pfirst" class="element text" size="3" maxlength="3" value="" type="text" /> -
<label for="pfirst">###</label>
</span>
<span>
<input id="plast" name="plast" class="element text" size="4" maxlength="4" value="" type="text" />
<label for="plast">####</label>
</span>
</li>
<li id="email">
<label class="description" for="element_3">Email </label>
<div>
<input id="email1" name="email1" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="address">
<label class="description" for="element_4">Address </label>
<div>
<input id="street" name="street" class="element text large" value="" type="text">
<label for="element_4_1">Street Address</label>
</div>
<div>
<input id="element_4_2" name="element_4_2" class="element text large" value="" type="text" />
<label for="element_4_2">Address Line 2</label>
</div>
<div class="left">
<input id="element_4_3" name="element_4_3" class="element text medium" value="" type="text" />
<label for="element_4_3">City</label>
</div>
<div class="right">
<input id="element_4_4" name="element_4_4" class="element text medium" value="" type="text" />
<label for="element_4_4">State / Province / Region</label>
</div>
<div class="left">
<input id="element_4_5" name="element_4_5" class="element text medium" maxlength="15" value="" type="text" />
<label for="element_4_5">Postal / Zip Code</label>
</div>
</li>
</ul>
And my PHP variables are as such:
$name=$_POST['name'];
$email=$_POST['email'];
$address=$_POST['address'];
$pod=$_POST['pod'];
$sku=$_PST['sku'];
$description=$_POST['description'];
$problem=$_POST['problem'];
$phone=$_POST['phone'];
$name=$_POST['first'] ." ". $_POST['last'];
$email=$_POST['email'];
$address=$_POST['address'];
$pod=$_POST['pod'];
$sku=$_PST['sku'];
$description=$_POST['description'];
$problem=$_POST['problem'];
$phone=$_POST['phone'];
Ideally you need to redesign your table but for making it work with the existing schema, you need to serialize and de-serialize data. see
http://php.net/manual/en/function.serialize.php
So in name column you store something like {firstname:lastname}
$name[0] = $firstname;
$name[1] = $lastname;
$ser = serialize($name); \\--->put this in db

Categories