php.css and html undefined index and uncaught exemption - php

this is the error i get after i reload the browser (Notice: Undefined index:) and also this (Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens')
THIS IS MY CODE:
addform.php
<input type="text" name="EMPLOYEENO" placeholder="Employee No">
<input type="text" name="NAME" placeholder="Name">
<input type="text" name="OCCUPATION" placeholder="Occupation">
<input type="text" name="GENDER" placeholder="Gender"><br><br>
<input type="text" name="AGE" placeholder="Age">
<input type="text" name="ADDRESS" placeholder="Address">
<input type="text" name="CELLPHONE" placeholder="Cellphone No">
<input type="text" name="DATEOFBIRTH" placeholder="Date of Birth"><br><br>
<input type="text" name="CITIZEN" placeholder="Citizenship">
<input type="text" name="RELIGION" placeholder="Religion">
<input type="text" name="HEIGHT" placeholder="Height">
<input type="text" name="WEIGHT" placeholder="Weight"><br><br>
<input type="text" name="ELEMENTARY" placeholder="Elementary">
<input type="text" name="ELEMENTARYYEAR" placeholder="Year Graduated"><br><br>
<input type="text" name="HIGHSCHOOL" placeholder="High School">
<input type="text" name="HIGHYEAR" placeholder="Year Graduated"><br><br>
<input type="text" name="COLLEGE" placeholder="College">
<input type="text" name="COLLEGEYEAR" placeholder="Year Graduated"><br>
<button>SUBMIT</button>
</div>
AND ADDFORM_PROCESS.PHP
$idno = $_POST['EMPLOYEENO'];
$name = $_POST['NAME'];
$occ = $_POST['OCCUPATION'];
$gender = $_POST['GENDER'];
$age = $_POST['AGE'];
$address = $_POST['ADDRESS'];
$cell = $_POST['CELLPHONE'];
$birth = $_POST['DATEOFBIRTH'];
$citizen = $_POST['CITIZEN'];
$religion = $_POST['RELIGION'];
$height = $_POST['HEIGHT'];
$weight = $_POST['WEIGHT'];
$elementary = $_POST['ELEMENTARY'];
$elementaryyear = $_POST['ELEMENTARYYEAR'];
$highschool = $_POST['HIGHSCHOOL'];
$highschoolyear = $_POST['HIGHYEAR'];
$college = $_POST['COLLEGE'];
$collegeyear = $_POST['COLLEGEYEAR'];
if ($idno="" || $name="" || $occ="" || $gender="" || $age="" || $address="" || $cell="" || $birth="" || $citizen="" ||
$religion="" || $height="" || $weight="" || $elementary="" || $elementaryyear="" || $highschool="" ||
$highschoolyear="" || $college="" || $collegeyear=""){
echo "<script>
alert('Dont leave the textbox empty!');
window.location='addform.php';
</script>";
}else{
$stmt=$db->prepare("INSERT INTO tbl_employee_information(EmployeeNo,Name,Occupation,Gender,Age,Address,CellphoneNo,DateofBirth,Citizenship,Religion,Height,Weight,Elementary,YearGraduatedinElementary,HighSchool,YearGraduatedinHighSchool,College,YearGraduatedinCollege) VALUES(:EMPLOYEENO,:NAME,:OCCUPATION,:GENDER,:AGE,:ADDRESS,:CELLPHONE,
:DATEOFBIRTH,:CITIZEN,:RELIGION,:HEIGHT,:WEIGHT,:ELEMENTARY,:ELEMENTARYYEAR,:HIGHSCHOOL,:HIGHYEAR,:COLLEGE,:
COLLEGEYEAR)");
$stmt->bindParam(':EMPLOYEENO',$idno);
$stmt->bindParam(':NAME',$name);
$stmt->bindParam(':OCCUPATION',$occ);
$stmt->bindParam(':GENDER',$gender);
$stmt->bindParam(':AGE',$age);
$stmt->bindParam(':ADDRESS',$address);
$stmt->bindParam(':CELLPHONE',$cell);
$stmt->bindParam(':DATEOFBIRTH',$birth);
$stmt->bindParam(':CITIZEN',$citizen);
$stmt->bindParam(':RELIGION',$religion);
$stmt->bindParam(':HEIGHT',$height);
$stmt->bindParam(':WEIGHT',$weight);
$stmt->bindParam(':ELEMENTARY',$elementary);
$stmt->bindParam(':ELEMENTARYYEAR',$elementaryyear);
$stmt->bindParam(':HIGHSCHOOL',$highschool);
$stmt->bindParam(':HIGHYEAR',$highschoolyear);
$stmt->bindParam(':COLLEGE',$college);
$stmt->bindParam(':COLLEGEYEAR',$collegeyear);
$stmt->execute();
header("location:main.php");

Maybe if you put the names of the parameters in an array and use foreach loop to build your request text and multiple calls of bindParam it would be easyer to debug ?

Related

Undefined index error when uploading image into database

I have a problem when uploading image into database using mysql.
When I click button for upload the image, the error comes out like this:
"Undefined index image in C:\xampp\htdocs\fyp\kemaskinipemandu.php" at line 29 and 30.
I've tried other solution from StackOverflow but it didn't work out at all. So, here is my html and my php code.
if (isset($_POST['kemaskini'])){
$email = $_SESSION['driverEmail'];
$nama = $_POST['nama'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
$kp = $_POST['kp'];
$tel = $_POST['tel'];
$alamat = $_POST['alamat'];
$exdate = date('Y-m-d', strtotime($_POST['lesen']));
$class = $_POST['jenislesen'];
$image = $_FILES['image']['name'];//imageUpload
move_uploaded_file($_FILES['image']['tmp_name'], "img/".$_FILES['image']['name']);
if($password === $cpassword){
$query = "UPDATE driver SET driverName='$nama', driverPassword='$password', cpassword='$cpassword', driverICNum='$kp', contNum='$tel',
address='$alamat', licenseExDate='$exdate', class='$class', image='$image' WHERE driverID='$driverID';";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
if($result)
{
?>
<script>
alert('Kemas Kini Pemandu Berjaya. ');
window.location.href="kemaskinipemandu.php";
</script>
<?php
}
}else {
?>
<script>
alert('Kata laluan tidak sama. ');
window.location.href="kemaskinipemandu.php";
</script>
<?php
}
}
and the line that have error are :
$image = $_FILES['image']['name'];//imageUpload
move_uploaded_file($_FILES['image']['tmp_name'], "img/".$_FILES['image']['name']);
Select image to upload:
<form action="kemaskinipemandu.php" method = "POST" enctype="multipart/form-data">
<center>
<?php
$email = $_SESSION['driverEmail'];
$query=" SELECT * from driver where driverID='$driverID'";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
$count = mysqli_num_rows($result);
if (isset($count) and ($count > 0)) {
while($row = mysqli_fetch_assoc($result)){
$name = $row['driverName'];
$password = $row['driverPassword'];
$cpassword = $row['cpassword'];
$ic = $row['driverICNum'];
$contNum = $row['contNum'];
$address = $row['address'];
$exdate = $row['licenseExDate'];
$class = $row['class'];
$image = $row['image'];
?>
<div class="col-md-6">
<div class="form-group">
<img src="<?php echo $image ?>" height="200" width="200"/><br><br>
<div class="form-inline">
Select image to upload:
<input type="file" name="image" id="image" style="margin-left:2%"><br><br>
</div>
<label for="nama">Nama :</label>
<input type="nama" class="form-control" name="nama" value="<?php echo $name ?>" ><br>
<label for="email">Email :</label>
<input type="email" class="form-control" name="email" value="<?php echo $email ?>" ><br>
<label for="password">Kata Laluan :</label>
<input type="password" class="form-control" name="password" value="<?php echo $password ?>" ><br>
<label for="password">Pengesahan Kata Laluan :</label>
<input type="password" class="form-control" name="cpassword" value="<?php echo $cpassword ?>" ><br>
<label for="kp">No. Kad Pengenalan :</label>
<input type="kp" class="form-control" name="kp" value="<?php echo $ic ?>" ><br>
<label for="tel">No. Tel :</label>
<input type="tel" class="form-control" name="tel" value="<?php echo $contNum ?>" ><br>
<label for="alamat">Alamat :</label>
<textarea type="alamat" class="form-control" name="alamat" rows="6"><?php echo $address ?></textarea><br>
<label for="lesen">Tamat Tempoh Lesen Memandu :</label>
<input type="date" class="form-control" name="lesen" value="<?php echo $exdate ?>" ><br>
<label for="jenislesen">Lesen/Kelas:</label>
<input type="text" class="form-control" name="jenislesen" value="<?php echo $class ?>" >
</div>
</div>
<?php } }?>
<input type="submit" class="btn btn-primary" name="kemaskini" value="Kemas Kini"></br>
</center>
</form>
I hope someone can help me. Thank you.

Copying a value from POST into a variable

In a page I have a form that submits with Post method. In another page I'm just trying to retreive and echo those values. If I just:
var_dump($_POST)
I see every value I entered in the form.
If I do something like:
echo $_POST['name'];
echo $_POST['surname'];
echo $_POST['telephone'];
I see the correct values.
But if, at the beginning of the page, use:
$name = $_POST['name'];
$surname = $_POST['surname'];
$telephone = $_POST['telephone'];
echo $name . ' ' . $surname . ' ' . $telephone;
I notice that for example name and telephone are always empty.
HTML form:
<form id="dom_com_sci" class="form" name="dom_com_sci" action="./ricevuta-fiscale-2convention.php" method="post" enctype="application/x-www-form-urlencoded" target="_self" style="height: 2200px; width: 619px;">
<p>
<label for="name">name:</label>
<input id="name" class="focus capitalize" name="name" type="text" size="26" value="" maxlength="70" placeholder="es. Mario" autocomplete="off" />
</p>
<p>
<label for="surname">surname:</label>
<input id="surname" class="focus capitalize" name="surname" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<p>
<label for="telephone">telephone:</label>
<input id="telephone" class="focus capitalize" name="telephone" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<input type="submit" name="Send" value="Send" tabindex="34" />
</form>
The php script on the result page:
<?php
require_once('./function/generic_function.php');
if(isset($_POST['submit']) && $_POST['submit'] == "Send") {
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
}
?>
generic_function.php:
<?php
function replace($stringa) {
$stringa_r = str_replace('\\','',$stringa);
return $stringa_r;
}
function accesso($percorso,$opzione = "N") {
($opzione == "Y") ? $blank = "target=_blank" : $blank = "";
if(isset($_SESSION['access_level']) && $_SESSION['access_level'] > 1 && isset($_SESSION['logged']) && $_SESSION['logged'] == 1 && isset($_SESSION['user_id'])) {
print('href="'.$percorso.'"'.$blank);
} else {
print('href="'.$dir.'access.php"');
}
}
?>
you are missing on quote (') in these line
$surname= $_POST['surname];
$name = $_POST['name];
$telephone = $_POST['telephone];
it should be this
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
<?php
//error_reporting($level=NULL);
?>
<form id="dom_com_sci" class="form" name="dom_com_sci" action="" method="post" enctype="application/x-www-form-urlencoded" target="_self" >
<p>
<label for="name">name:</label>
<input id="name" class="focus capitalize" name="name" type="text" size="26" value="" maxlength="70" placeholder="es. Mario" autocomplete="off" />
</p>
<p>
<label for="surname">surname:</label>
<input id="surname" class="focus capitalize" name="surname" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<p>
<label for="telephone">telephone:</label>
<input id="telephone" class="focus capitalize" name="telephone" type="text" size="26" value="" maxlength="70" placeholder="es. Rossi" autocomplete="off" />
</p>
<input type="submit" name="submit" value="submit" tabindex="34" />
</form>
<?php
//require_once('./function/generic_function.php');
if(isset($_POST['submit']) && $_POST['submit'] == "submit") {
$surname= $_POST['surname'];
$name = $_POST['name'];
$telephone = $_POST['telephone'];
echo $name . ' ' . $surname . ' ' . $telephone;
}
?>
Comment:: To style your form. You can put it inside the div and than can give that div style.
Thanks let me know it work or not.

PHP form handling, checkbox array issue

I have a simple PHP script that handles an HTML form, and everything dumps to email correctly except for a multiple checkbox input. Instead of displaying the checkboxes a user selected, it displays Array.
<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}
$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $gender = $_POST['gender']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $shirtsize = $_POST['shirt_size']; $country = $_POST['country']; $dayarea = $_POST['daytime_phone_area']; $dayphone = $_POST['daytime_phone']; $shirtsizeother = $_POST['other']; $dates = $_POST['dates']; $signature = $_POST['signature']; $signday = $_POST['signature_day']; $signmonth = $_POST['signature_month']; $signyear = $_POST['signature_year']; $position = $_POST['position']; $contactmethod = $_POST['contactmethod']; $other_size = 'n/a'; $_POST['other_size'] = 'n/a';
//Validate first
/*
Simple form validation
check to see if required fields were entered */ if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['gender'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['daytime_phone_area'] == "" || $_POST['daytime_phone'] == "" || $_POST['dates'] == "" || $_POST['signature'] == "" || $_POST['signature_day'] == "" || $_POST['signature_month'] == "" || $_POST['shirt_size'] == "" || $_POST['signature_year'] == "" || $_POST['position'] == "" || $_POST['contactmethod'] == "" ) {
var_dump($_POST);
echo "Please fill in all required boxes.";
}
else {
$email_from = 'chris569x#gmail.com';//<== update the email address $email_subject = "New Volunteer Registration"; $email_body = "You have received a new volunteer registration.\n".
"Volunteer: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Gender: $gender \n".
"Cell Phone: ($areacode) $cellphone \n".
"Daytime Phone: ($dayarea) $dayphone \n".
"T-Shirt Size: $shirtsize $shirtsizeother \n".
"Volunteer Position: $position \n".
"Dates availible to volunteer: $dates \n".
"Electronic Signature: $signature $signmonth/$signday/$signyear \n".
"Preferred contact method: $contactmethod \n"; $to = "chris569x#gmail.com";//<== update the email address $headers = "From: $email_from \r\n"; //Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thanks2.htm');
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?>
and the email output (problem is with dates availible to volunteer):
You have received a new volunteer registration.
Volunteer: xxxxxx
Address: xxxx
xxxxxx
Email: xxxx
Date of Birth: xxxx
Gender: xxxx
Cell Phone: xxxx
Daytime Phone: xxxx
T-Shirt Size: xxxx
Dates available to volunteer: Array
Electronic Signature: xxxx
Preferred contact method: xxxx
And here is the HTML form if you need to see it:
<form name="volunteer" action="form-to-email3.php" method="post">
<label>Last Name*: </label><input type="text" name="lastname" /><br /> <label>First Name*: </label><input type="text" name="firstname" /><br /> <label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br /> <label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("month", "day", 2); autojump("day", "year", 2); //--> </SCRIPT><br /><br /> <label>Gender*:</label><input type="radio" name="gender" value="Male"> Male <input type="radio" name="gender" value="Female"> Female </input> <br /> <br /> <label>Street Address*: </label><input type="text" name="streetaddress" /><br /> <label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input type="text" name="city" /><br />
<label>State/Province/Region*: </label><input type="text" name="state" /><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)"/><br />
<label>Country: </label>
<select class="element select medium" id="element_3_6" name="country">
</select> <br /><br />
<label>Email*: </label><input type="email" name="email" /><br /><br />
<label>Cell Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Daytime Phone*: </label>(<INPUT NAME="daytime_phone_area" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="daytime_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("daytime_phone_area", "daytime_phone", 3); //--> </SCRIPT>
<label>T-Shirt Size*: </label><input type="radio" name="shirt_size" value="Small"> S <input type="radio" name="shirt_size" value="Medium"> M <input type="radio" name="shirt_size" value="Large"> L <input type="radio" name="shirt_size" value="XL"> XL <input type="radio" name="shirt_size" value="2XL"> 2XL <input type="radio" name="shirt_size" value="Other"> Other (Please provide)<br /> </input>
<label>Other: </label><input type="text" name="other size" /><br /><br /> <label>In which area will you be helping*: </label> <select name="position"> <option name="position" value= "Pastor">Pastor</option> <option name="position" value="Nursing Staff">Nursing Staff</option> <option name="position" value="Camp Staff">Camp Staff (Including Huddle Leaders)</option> <option name="position" value="Other Staff">Other Staff</option> </select><br /><br /> <label>Dates Attending*: <br />(Please check all dates you can be present)</label> <input type="checkbox" name="dates[]" value="7/6">Jul. 6 <input type="checkbox" name="dates[]" value="7/7">Jul. 7 <input type="checkbox" name="dates[]" value="7/8">Jul. 8 <input type="checkbox" name="dates[]" value="7/9">Jul. 9 <input type="checkbox" name="dates[]" value="7/10">Jul. 10 <input type="checkbox" name="dates[]" value="7/11">Jul. 11 <input type="checkbox" name="dates[]" value="7/12">Jul. 12<br /><br /><br /><br /></input> <label>What is the best way to contact you*? </label><input type="text" name="contactmethod" /><br /><br /><br />
<b>Electronic Submission</b>
<p>I understand that by filling in my name here, it shall act as a binding, legal signagure. </p> <label>Electronic Signagure*: </label><input type="text" name="signature" /><br />
<label>Date*: </label><INPUT NAME="signature_month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("signature_month", "signature_day", 2); autojump("signature_day", "signature_year", 2); //--> </SCRIPT><br /><br /><br /><br /><br />
<input type="submit" value="Submit" />
</form>
Try using PHP's implode() function and changing
$dates = $_POST['dates'];
to
$dates = implode(", ",$_POST['dates']);
This will convert the array into a string where each date is separated by a comma.
When you used the name dates[] in your HTML forms, you specified that the data should be in an array format. The [] syntax tells the form to create a new element in the array of your POST data.
To step through the values of your array, you'd use foreach:
foreach($dates as $v) {
echo $v . "<br />";
}

php form validation issue, "fill in all required boxes"

I have created a simple HTML form, and process it using PHP.
When I try to submit the form, it is giving me the error message I created for not filling in all the required boxes. I have checked and rechecked the variables, and I cannot figure out why it is giving me that message with all the fields filled in.
HTML:
<form name="volunteer" action="form-to-email3.php" method="post">
<label>Last Name*: </label><input type="text" name="lastname" /><br />
<label>First Name*: </label><input type="text" name="firstname" /><br />
<label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br />
<label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)">
<SCRIPT TYPE="text/javascript">
<!--
autojump("month", "day", 2); autojump("day", "year", 2);
//-->
</SCRIPT><br /><br />
<label>Gender*:</label><input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<br /> <br />
<label>Street Address*: </label><input type="text" name="streetaddress" /><br />
<label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input type="text" name="city" /><br />
<label>State/Province/Region*: </label><input type="text" name="state" /><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)"><br />
<label>Country: </label>
<select class="element select medium" id="element_3_6" name="country">
<option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
</select>
<br /><br />
<label>Email*: </label><input type="email" name="email" /><br /><br />
<label>Cell Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)">)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Daytime Phone*: </label>(<INPUT NAME="daytime_phone_area" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)">)<INPUT NAME="daytime_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("daytime_phone_area", "daytime_phone", 3);
//-->
</SCRIPT>
<label>T-Shirt Size*: </label><input type="radio" name="shirt_size" value="Small"> Small
<input type="radio" name="shirt_size" value="Medium"> Medium
<input type="radio" name="shirt_size" value="Large"> Large
<input type="radio" name="shirt_size" value="XL"> XL
<input type="radio" name="shirt_size" value="2XL"> 2XL
<input type="radio" name="shirt_size" value="Other"> Other (Please provide)<br />
<label>Other:</label><input type="text" name="other size" /><br /><br />
<p>In which area will you be helping*:</p>
<select>
<option name= "position" value= "Pastor">Pastor</option>
<option name= "position" value="Nursing Staff">Nursing Staff</option>
<option name= "position" value="Camp Staff">Camp Staff (Including Huddle Leaders)</option>
<option name= "position" value="Other Staff">Other Staff</option>
</select><br /><br />
<label>Dates Attending*: <br />(Please check all dates you can be present)</label>
<input type="checkbox" name="dates" value="7/6">July 6
<input type="checkbox" name="dates" value="7/7" />July 7
<input type="checkbox" name="dates" value="7/8">July 8
<input type="checkbox" name="dates" value="7/9">July 9
<input type="checkbox" name="dates" value="7/10">July 10
<input type="checkbox" name="dates" value="7/11">July 11
<input type="checkbox" name="dates" value="7/12">July 12<br /><br /><br /><br />
<label>What is the best way to contact you*? </label><input type="text" name="contactmethod" /><br /><br /><br />
<b>Electronic Submission</b>
<p>I understand that by filling in my name here, it shall act as a binding, legal signagure. </p>
<label>Electronic Signagure*: </label><input type="text" name="signature" /><br />
<label>Date*: </label><INPUT NAME="signature_month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="signature_day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="signature_year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)">
<SCRIPT TYPE="text/javascript">
<!--
autojump("signature_month", "signature_day", 2); autojump("signature_day", "signature_year", 2);
//-->
</SCRIPT><br /><br /><br /><br /><br />
<input type="submit" value="Submit">
</form>
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST')
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}
$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $gender = $_POST['gender']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $shirtsize = $_POST['shirt_size']; $country = $_POST['country']; $dayarea = $_POST['daytime_phone_area']; $dayphone = $_POST['daytime_phone']; $shirtsizeother = $_POST['other']; $dates = $_POST['dates']; $signature = $_POST['signature']; $signday = $_POST['signature_day']; $signmonth = $_POST['signature_month']; $signyear = $_POST['signature_year']; $position = $_POST['position']; $contactmethod = $_POST['contactmethod'];
//Validate first
/*
Simple form validation
check to see if required fields were entered
*/
if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['gender'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['daytime_phone_area'] == "" || $_POST['daytime_phone'] == "" || $_POST['dates'] == "" || $_POST['signature'] == "" || $_POST['signature_day'] == "" || $_POST['signature_month'] == "" || $_POST['shirt_size'] == "" || $_POST['signature_year'] == "" || $_POST['position'] == "" || $_POST['contactmethod'] == "" ) {
echo "Please fill in all required boxes.";}
else {
$email_from = 'chris569x#gmail.com';//<== update the email address
$email_subject = "New Volunteer Registration"; $email_body = "You have received a new volunteer registration.\n".
"Volunteer: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Gender: $gender \n".
"Cell Phone: ($areacode) $cellphone \n".
"Daytime Phone: ($dayarea) $dayphone \n".
"T-Shirt Size: $shirtsize $shirtsizeother \n".
"Dates availible to volunteer: $dates \n".
"Electronic Signature: $signature $signmonth/$signday/$signyear \n".
"Preferred contact method: $contactmethod \n";
$to = "chris569x#gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thanks2.htm');
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?>
I believe that you need to change your select for position.
It appears that the name is not coming over to the post page because you put the name in instead of the . Fix that and you should be good to go.
Congratulations, your server seems to be properly configured! The variables $city should not hold any data. You have to use $_POST['city'] or $_REQUEST['city'] instead.
Some hosts may still support submitting variables directly to the $variables, but this is not a safe configuration.
For instance:
...
if (password_check($username,$password)) {
$access = 'yes'
}
...
if ('yes'==$access) { ... }
Could be hacked by submitting script.php&access=yes
So you have to use $_POST['variable'] directly or copy it in the $variable, first.

PHP - Form error handling issue

I am trying to create error messages if certain conditions aren't met. So the user fills out a form and if a field is empty or doesn't pass my validation it returns the error message.
This is the form:
if (isset($_POST)) {
if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) {
mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email','$surName') ") or die(mysql_error());
header('Location: thanks.php');
}
else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="' .$_POST['name'].'" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example#domain.com" value="' .$_POST['email']. '" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
} else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example#domain.com" value="" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
So what I tried is adding an array to display the error messages like so:
$errorMessage = array();
And add this to the html form field with the proper message:
$error[] = "Error Message";
Now what I am stuck with is that I want to have the error show only if a user doesn't meet the conditions
if ($name == NULL) {$error[] = "Error Message";}
if ($surName == NULL) {$error[] = "Error Message 2";}
if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";}
But I can't make it work. When I tried to implement this logic it will parse the page fine and the validation works as well but the error messages wont show up if I leave a required field blank. My guess is that I didn't loop through it properly.
Help is much appreciated!
EDIT:
I tried the answer that was posted by Frosty Z and this is what I have at the moment:
if (isset($_POST)) {
$errorMessage = array();
if ($name == '') { $errors[] = "Input name please." }
if ($surName == '') { $errors[] = "Input last name please." }
if (!checkEmail($email)) { $errors[] = "Email address not valid." }
if (count($error) == 0) {
mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email', '$surName') ") or die(mysql_error());
header('Location: thanks.php');
exit;
else {
if (count($errors) > 0)
echo "<p>Sorry, there are problems with the information you have provided:</p>";
foreach($errors as $error)
echo '<p class="error">'.$error.'</p>';
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="' .$_POST['name'].'" />
<span class="required">*</span>
<label for="surName">Last name</label>
<input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example#domain.com" value="' .$_POST['email']. '" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
} else {
echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" />
<span class="required">*</span>
<label for="surName">Achternaam</label>
<input type="text" name="surName" id="surName" value="" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example#domain.com" value="" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
}
With this my page won't be parsed. I have error reporting on but it doesn't show anything besides a
Internal server error 500
in my console log(Firebug)
Here is some rewriting of your work with a minimal handling of error messages.
BTW, you should consider adopting a decent PHP framework which will help you to handle a lot of common development tasks.
$name = '';
$surName = '';
$email = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$surName = $_POST['surName'];
$email = $_POST['email'];
$errors = array();
if ($name == '') { $errors[] = "Please type your name."; }
if ($surName == '') { $errors[] = "Please type your surname."; }
if (!checkEmail($email)) { $errors[] = "Wrong email format."; }
if (count($errors) == 0) {
// tip: use PDO or mysqli functions instead of mysql ones to bind variables.
// currently there is a risk of SQL injection here
mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email','$surName') ") or die(mysql_error());
header('Location: thanks.php');
exit;
}
}
if (count($errors) > 0)
echo '<p>Sorry, there are problems with the information you have provided:</p>';
foreach($errors as $error)
echo '<p class="error">'.$error.'</p>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example#domain.com" value="'.htmlspecialchars($email).'" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';

Categories