php function not being applied [duplicate] - php

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
I have been following the w3schools php tutorial to produce a form. The form collects and displays data but none of the error checking is working. I have run the php and html through lint with no errors and double checked my use of quotes.
Specifically the script is not applying htmlspecialchars, trim or split. None of the regex checking is being applied and the error messages are not appearing. I tried replacing an error message with a simple if / not echo message and that worked, although the message was placed at the head of the web page, not next to the field.
I am also getting the following on execution.
Notice: Undefined index: numChild in /volume1/homes/richard/www/action.php on line 22
Line 22 is:
$totalGuests = $_POST['numAdults'] =+ $_POST['numChild'];
The difference between this and other undefined index questions is that the function 'test_input' is not being applied. I have included #Poiz suggestion below and this resolves the undefined index problem but the function is still now working.
My code is:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
// define variables and set to empty values
$guestName = $guestEmail = $guestPhone = $startDate = $endDate = $numAdults = $numChild = "";
$guestNameErr = $guestEmailErr = $guestPhoneErr = $startDateErr = $endDateErr = $numAdultsErr = $numChildErr = "";
$maxAllowed = 4;
$totalGuests = $_POST['numAdults'] =+ $_POST['numChild'];
if ($_SERVER['REQUEST_METHOD'] == "POST") {
if (empty($_POST['guestName'])) {
$guestNameErr = "guestName is required";
} else {
$guestName = test_input($_POST['guestName']);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$guestName)) {
$guestNameErr = "Only letters and white space allowed";
}
}
if (empty($_POST['guestEmail'])) {
$guestEmailErr = "Email is required";
} else {
$guestEmail = test_input($_POST['guestEmail']);
// check if e-mail address is well-formed
if (!filter_var($guestEmail, FILTER_VALIDATE_EMAIL)) {
$guestEmailErr = "Invalid guest email format";
}
}
if (empty($_POST['guestPhone'])) {
$guestPhoneErr = "Phone number is required";
} else {
$guestPhone = test_input($_POST['guestPhone']);
// check if phone number is UK format and valid
if (!preg_match("/^(\+44\s?7\d{3}|\(?07\d{3}\)?)\s?\d{3}\s?\d{3}$/",$guestPhone)) {
$guestPhoneErr = "Please enter a valid phone number";
}
}
if (empty($_POST['startDate'])) {
$startDateErr = "Start date is required";
} else {
$startDate = $_POST['startDate'];
}
if (empty($_POST['endDate'])) {
$endDateErr = "Start date is required";
} else {
$endDate = $_POST['endDate'];
if ($_POST['endDate'] < $_POST['startDate']) {
$sendDateErr = "End date must be before start date";
}
}
if ($totalGuests > $maxAllowed) {
$bookingErr = "A maximum of 4 guests can be accommodated at Mariner's Loft";
} else {
$numAdults = ($_POST['numAdults']);
$numChild = ($_POST['numChild']);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<br><input type="text" class="form-control" id="guestName" name="guestName" value="<?php echo $guestName;?>" placeholder="Full name" autofocus required>
<br>
<br><input type="email" class="form-control" id="guestEmail" name="guestEmail" value="<?php echo $guestEmail;?>" placeholder="email">
<br>
<br><input type="phone" class="form-control" id="guestPhone" name="guestPhone" value="<?php echo $guestPhone;?>" placeholder="Phone number">
<p><strong>How many guests?</strong></p>
<p>How many adults?</p>
<br><input type="text" class="form-control" id="numAdults" name="numAdults" value="<?php echo $numAdults;?>" placeholder="number of adults">
<p>How many children?</p>
<br><input type="text" class="form-control" id="numChild" name="numChild" value="<?php echo $numChild;?>" placeholder="number of children">
<br>
<br><input type="date" class="form-control" id="startDate" name="startDate" value="<?php echo $startDate;?>" placeholder="Arrival date">
<br>
<br><input type="date" class="form-control" id="endDate" name="endDate" value="<?php echo $endDate;?>" placeholder="Leaving date">
<br><br>
<input type="submit" name="submit" value= "Submit"/>
<br><br>
<input type="reset" name="submit" value="Cancel"/>
</form>
<?php
echo "<h2>Guest info:</h2>";
echo $guestName;
echo "<br>";
echo $guestEmail;
echo "<br>";
echo $guestPhone;
echo "<br>";
echo $totalGuests;
echo "<br>";
echo $startDate;
echo "<br>";
echo $endDate;
?>
</body>
</html>
Any help gratefully received.

The Code $totalGuests = $_POST['numAdults'] =+ $_POST['numChild']; should be moved into the Conditional Block or redeclared to have a Default: (1, for example)...
$maxAllowed = 4;
$totalGuests = ( isset($_POST['numAdults'] ) &&
isset($_POST['numChild']) ) ?
( $_POST['numAdults'] + $_POST['numChild'] )
:1;
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// REST OF THE CODES
}

Related

PHP session array and input validation

Currently I am sending error messages and storing the value of my input fields in sessions.
Form example
<label class="errormsg"><?php echo $_SESSION['msgProductPrice']; unset($_SESSION['msgProductPrice']); ?></label>
<input type="text" name="product_price" value="<?php echo $_SESSION['val_ProductPrice']; unset($_SESSION['val_ProductPrice']); ?>" />
PHP
$Price = $_POST['product_price'];
$errorcount = 0;
if(empty($Price) === true){
$PriceErrormsg = "empty";
$errorcount++;
}
if($errorcount === 0) {
// success
} else {
$_SESSION['val_ProductPrice'] = $Price;
$_SESSION['msgProductPrice'] = $PriceErrormsg;
}
This works perfectly with one of a kind input field. If I try with multiple input fields with the same name, it doesn't work.
Form example
<label class="errormsg"><?php echo $_SESSION['msgProductAmount']; unset($_SESSION['msgProductAmount']); ?></label>
<input type="text" name="product_amount[]" value="<?php echo $_SESSION['val_ProductAmount']; unset($_SESSION['val_ProductAmount']); ?>" />
<label class="errormsg"><?php echo $_SESSION['msgProductAmount']; unset($_SESSION['msgProductAmount']); ?></label>
<input type="text" name="product_amount[]" value="<?php echo $_SESSION['val_ProductAmount']; unset($_SESSION['val_ProductAmount']); ?>" />
This is where I'm unsure on how to validate all the input fields, how to keep the value in each input field when you hit submit and how to send an errormsg about each field?
PHP
$Amount= $_POST['product_amount'];
$errorcount = 0;
if(empty($Amount) === true){
$AmountErrormsg = "empty";
$errorcount++;
}
if($errorcount === 0) {
// success
} else {
$_SESSION['val_ProductAmount'] = $Amount;
$_SESSION['msgProductAmount'] = $AmountErrormsg;
}
If I understand your problem, multiple product amounts are being submitted, and you want to validate each one individually and display the error message next to the appropriate textbox?
Because you are receiving an array of values, you need to create a corresponding array of error messages.
It's a while since I've done any PHP, so this might not be 100% correct, but I think you need something along these lines...
$AmountErrorMessage = Array();
foreach ($Amount as $key => $value) {
if (empty($value)) {
$AmountErrorMessage[$key] = 'empty';
}
}
if ($AmountErrorMessage->count() > 0) {
// success
} else {
$_SESSION['val_ProductAmount'] = $Amount;
$_SESSION['msgProductAmount'] = $AmountErrorMessage;
}
You would then also need to iterate through the array in order to generate the HTML for your form, creating a label and input box for each value submitted.
This code help you to do it as per your wish..
<?php
session_start();
?>
<html>
<head>
<title></title>
<style>
.errormsg{
color:red;
}
</style>
</head>
<body>
<?php
if(isset($_POST['product_amount']))
{
$errorcount = 0;
for($i=0;$i<count($_POST['product_amount']);$i++){
$Amount[$i] = $_POST['product_amount'][$i];
if(empty($Amount[$i]) === true){
$_SESSION['msgProductAmount'][$i] = "empty";
$errorcount++;
}
else
$_SESSION['val_ProductAmount'][$i] = $Amount[$i];
}
if($errorcount === 0) {
unset($_SESSION['msgProductAmount']);
echo "success";
}
}
?>
<form action="" method="POST">
<?php
$cnt = 10;
for($i=0;$i<$cnt;$i++){
?>
<input type="text" name="product_amount[<?=$i?>]" value="<?php echo isset($_SESSION['val_ProductAmount'][$i]) ? $_SESSION['val_ProductAmount'][$i] : '';?>" />
<label class="errormsg"><?php echo $res = isset($_SESSION['msgProductAmount'][$i]) ? $_SESSION['msgProductAmount'][$i] : '' ; ?></label>
<br/>
<?php
}
?>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
unset($_SESSION['msgProductAmount'],$_SESSION['val_ProductAmount']);
?>

PHP file not running on local host

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Book A Table</title>
</head>
<body>
<h1>Book A Table</h1>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $numErr=$dateErr = $timeErr = personsErr="";
$name = $email = $num= $date = $time = $persons = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["num"])) {
$numErr = "Number is required";
} else {
$num = test_input($_POST["num"]);
}
if (empty($_POST["date"])) {
$dateErr = "Date is required";
} else {
$date = test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "Time is required";
} else {
$time = test_input($_POST["time"]);
}
if (empty($_POST["persons"])) {
$personsErr = "Number of persons is required";
} else {
$persons = test_input($_POST["persons"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Full Name<br> <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail<br> <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Contact Number <input type="text" name="num">
<span class="error"><?php echo $numErr;?></span>
<br><br>
Reservation Date*<br> <input type="date" name="date"><br><br>
<span class="error"><?php echo $dateErr;?></span>
<br><br>
Reservation Time*<br>(Mon - Thur: 18:00 - 23:00 Fri - Sun: 12:00 - 00:00)<br> <input type="time" name="time"><br><br>
<span class="error"><?php echo $timeErr;?></span>
<br><br>
Number of Persons*<br> <input type="text" name="persons"><br><br>
<span class="error"><?php echo $personsErr;?></span>
<br><br>
Comments<br><textarea name="comment" rows="5" cols="40"></textarea><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $num;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $persons;
echo "<br>";
?>
</body>
</html>
I just started learning PHP today so im a beginner and im not very
familiar with it. trying to use php on a local host to make a booking
reservation page for a restaurant. What i was trying to do is make a
form and validate it so that the user wont be able to leave out any
required fields. however it doesnt seem to work. can anyone tell me
where i went wrong please ?
if there aren't any errors you have to check the name of the php file? does it have the extension ".php"?
Are you referring the right directory?
is the server running?

Undefined variable error (for a placeholder) is shown on html file, even when it's defined in php file [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I'm having a N00bish problem, working on a simple form validation.
I've created a form (html), which should perform a simple data validation, when submitting it to a seperate php file.
The html contains a placeholder for the error message, which is defined in the php file.
However, when i load the form (before submitting it), i'm getting the dreaded "Notice: Undefined variable: fnameError in C:\xampp\htdocs\practice\form.html on line 19" error.
But the variable is declared and initialized on the php file...
I've also tried to add a reference to the php file in the html, using "include".
However, i then get a different error message: "Parse error: syntax error, unexpected end of file in the line"
The html file contains the following code:
<!DOCTYPE html>
<html>
<head>
<title>Form Validation</title>
</head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Form validation with PHP</h2>
</div>
<form action="action_page.php" method="post" name="form_name" id="form_id" class="form_class">
<h3>Form:</h3>
<span class="error">* required field.</span><br>
<label>First Name:</label>
<input type="text" name="firstname" id="fname" placeholder="First Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $fnameError;?></span>
<label>Last Name:</label>
<input type="text" name="lastname" id="lname" placeholder="Last Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php echo $lnameError;?></span>
<label>Email Address:</label>
<input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
<span class="error">* <?php echo $emailError;?></span>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>
And the PHP file contains the following code:
<?php
// Initialize variables to null.
$fnameError = "";
$lnameError = "";
$emailError = "";
// On submitting the form, the below functions will execute.
if(isset($_POST['submit'])){
if(empty($_POST["firstname"])){
$fnameError = "First name is required";
} else {
$fname = test_input($_POST["firstname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameError = "Only letters and whitespace allowed";
}
}
if(empty($_POST["lastname"])){
$lnameError = "Last name is required";
} else {
$lname = test_input($_POST["lastname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameError = "Only letters and whitespace allowed";
}
}
if (empty($_POST["emailaddress"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>
What am i doing wrong here?
Thanks!
You need to do two things:-
close your first if statement in your action_page.php file.
write:- <?php if(isset($fnameError)) {echo $fnameError;}?> and do the same for other two.
You didn't close the first php conditional: if(isset($_POST['submit'])){". Close this with a} and your code seems good to go!
There are two errors in your code:
Parse error: syntax error, unexpected end of file in the line
You have missed the closing braces of if(isset($_POST['submit'])) as other mate already mentioned.
Notice: Undefined variable: fnameError in
C:\xampp\htdocs\practice\form.html on line 19
You are using PHP variable before initialize.
Solution:
For ist point, add ending } before function start.
For second point, move your PHP code before HTML.
1) As, I'm clearly seeing the error
"Notice: Undefined variable: fnameError in C:\xampp\htdocs\practice\form.html on line 19"
Means, you are having page name form.html So, Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP.
Either
change form.html to form.php
Or
Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website
AddType application/x-httpd-php .html
2) Put your php code before <form></form> for displaying the respective error.
3) Write
<span class="error">* <?php if(isset($fnameError)) {echo $fnameError;}?></span>
<span class="error">* <?php if(isset($lnameError)) {echo $lnameError;}?></span>
<span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>
for not getting
"Notice: Undefined variable:"
4) You didn't closed } for if(isset($_POST['submit'])){ . So, close it for not getting
Parse error: syntax error
Updated Code:
<?php
// On submitting the form, the below functions will execute.
if(isset($_POST['submit'])){
if(empty($_POST["firstname"])){
$fnameError = "First name is required";
}
else {
$fname = test_input($_POST["firstname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$fname)) {
$fnameError = "Only letters and whitespace allowed";
}
}
if(empty($_POST["lastname"])){
$lnameError = "Last name is required";
} else {
$lname = test_input($_POST["lastname"]);
// Check firstname contains only letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$lname)) {
$lnameError = "Only letters and whitespace allowed";
}
}
if (empty($_POST["emailaddress"])) {
$emailError = "Email is required";
} else {
$email = test_input($_POST["emailaddress"]);
// check if e-mail address syntax is valid or not
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email)) {
$emailError = "Invalid email format";
}
}
}
function test_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
//php code ends here
?>
<!DOCTYPE html>
<html>
<head><title>Form Validation</title></head>
<body>
<div class="maindiv">
<div class="form_div">
<div class="title">
<h2>Form validation with PHP</h2>
</div>
<form action="action_page.php" method="post" name="form_name" id="form_id" class="form_class" >
<h3>Form:</h3>
<span class="error">* required field.</span><br>
<label>First Name:</label>
<input type="text" name="firstname" id="fname" placeholder="First Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php if(isset($fnameError)) {echo $fnameError;}?></span>
<label>Last Name:</label>
<input type="text" name="lastname" id="lname" placeholder="Last Name" value="" pattern="[a-zA-Z]+" required><br>
<span class="error">* <?php if(isset($lnameError)) {echo $lnameError;}?></span>
<label>Email Address:</label>
<input type="email" name="emailaddress" id="email" placeholder="Email Address" value="" required><br>
<span class="error">* <?php if(isset($emailError)) {echo $emailError;}?></span>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
</div>
</div>
</body>
</html>

Can't get code to work in PHP 5.5.9 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This code works fine in PHP 5.3, but I can't figure out what to do to make it working in PHP 5.5.9 on a LAMP server. I have tried searching around, but haven't found any solution. Any ideas of to fix it?
So; the users sees the from correctly, without the fact it always says the mail is sent, even befor they hit the submit button. Second, the mail that it should send in the end, will not be sent, nothing happens.
<body>
<?php
//Variabals and validastions
$firstnameErr = $secondnameErr = $surenameErr = $emailErr = $dateErr = $gateErr = $pnumErr = "";
$firstname = $secondname = $surename = $email = $date = $gate = $pnum = "";
$Err = "1";
$date = date("d/m/Y");
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["firstname"])) {
$firstnameErr = "Firstname is needed";
$Err = "2";
} else {
$firstname = test_input($_POST["firstname"]);
if (!preg_match("/^[a-åA-Å ]*$/",$firstname)) {
$firstnameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["secondname"])) {
$secondname = " ";
} else {
$secondname = test_input($_POST["secondname"]);
if (!preg_match("/^[a-åA-Å ]*$/",$secondname)) {
$secondnameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["surename"])) {
$surenameErr = "Surename is needed";
$Err = "2";
} else {
$surename = test_input($_POST["surename"]);
if (!preg_match("/^[a-åA-Å ]*$/",$surename)) {
$surenameErr = "Only letter and spaces";
$Err = "2";
}
}
if (empty($_POST["email"])) {
$emailErr = "E-postadresse is needed";
$Err = "2";
} else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Not valid email";
$Err = "2";
}
}
if (empty ($_POST["date"])) {
$dateErr = "Fødselsdate is needed";
$Err = "2";
} else {
$date = test_input ($_POST["date"]);
}
if (empty($_POST["gate"])) {
$gateErr = "Gateadresse is needed";
$Err = "2";
} else {
$gate = test_input($_POST["gate"]);
if (!preg_match("/^[a-åA-Å0-9. ]*$/",$gate)) {
$gateErr = "Invalid characters, only letters, numbers and space";
$Err = "2";
}
}
if (empty($_POST["pnum"])) {
$pnumErr = "Post number is needed";
$Err = "2";
} else {
$pnum = test_input($_POST["pnum"]);
if (!preg_match("/^\d{4}$/",$pnum)) {
$pnumErr = "Invalid post number";
$Err = "2";
}
}
if ($Err == "1") {
$Err = "";
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!--Form-->
<div>
<div>
<img src="https://wiki.piratpartiet.no/images/1/18/Ole.png" alt="uPir logo" width="400px">
<h1 style="text-align:center">Application form</h1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<table style="width:100%">
<tr>
<td>
Firstname: <br>
<input name="firstname" type="text" value="<?php echo $firstname;?>">
<span class="error">* <br>
<?php echo $firstnameErr;?></span>
<br><br>
</td>
<td>
Secondname:<br>
<input name="secondname" type="text" value="<?php echo $secondname;?>">
<span class="error"><br>
<?php echo $secondnameErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
Surename:<br>
<input name="surename" type="text" value="<?php echo $surename;?>">
<span class="error">* <br>
<?php echo $surenameErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
E-mail:<br>
<input name="email" type="mail" value="<?php echo $email;?>">
<span class="error">* <br>
<?php echo $emailErr;?></span>
<br><br>
</td>
<td>
Brithdate (dd.mm.yyyy):<br>
<input name="date" type="date" min="1900-01-01" max="2015-12-31" value="<?php echo $date;?>">
<span class="error">* <br>
<?php echo $dateErr;?></span>
<br><br>
</td>
</tr>
<tr>
<td>
Adress:<br>
<input name="gate" type="text" value="<?php echo $gate;?>">
<span class="error">* <br>
<?php echo $gateErr;?></span>
<br><br>
</td>
<td>
Post number:<br>
<input name="pnum" type="number" value="<?php echo $pnum;?>">
<span class="error">* <br>
<?php echo $pnumErr;?></span>
<br><br>
</td>
</tr>
</table>
<?php
//Controll and mailsender
if (empty($Err["Err"])) {
$to = 'email#adress.com';
$subject = 'subject';
$message = "$firstname $secondname $surename ønsker å bli medlem.
E-post: $email
Fødselsdate: $date
Gateadresse: $gate
Postnummer: $pnum
Sendt: $date
--massages info here--";
mail($to, $subject, $message);
echo '<p class="sucsess">Messages sent</p>';
}
?>
<p style="text-align:center"><input type="submit" name="sumbit" value="Register" id="submit"></p>
</div>
</form>
</div>
</body>
The issue is that on 5.5.9 it dosn't send the info mail (to me) and the user gets up the info that the mail is sent before they have hit the sumbit button, and even if it's wrongly filed out.
action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" -> action=""
if (empty($Err["Err"])) { -> $Err might still be "1" (no array) wich will throw a warning
You might have issues with UTF-8 here (I see no html meta tag / or xml notification or character encoding tag inside html)
But please tell us what goes wrong first!
Turn on displaying errors/warnings at the beginning of the code:
ini_set('error_reporting', E_ALL);
ini_set("display_errors", "1");
My first guess would be that while your PHP 5.3 server is configured to send email, your PHP 5.5.9 is not. Simple test - call mail() with hardcoded values.
even before they hit the submit button
I'm not sure what you mean by that bit, but maybe your doing something with AJAX. Look in your browser's console log to see all your HTTP requests and responses.
The issue where in the last test if (empty($Err["Err"])) { it should have been if (empty($Err)) {.
Problem solved.

line 75 : if (is_array($errors)) [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 9 years ago.
Error!
The following error(s) occurred:
Notice: Undefined variable: errors in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\scripts\ladies_paradise\checkout.php on line 75
Please try again.
`
Untitled Document
<body>
<?php
$page_title='ORDER FORM';
if(isset($_POST['submitted'])){
$errors= array();
$price=($_POST['price']);;
$quantity=($_POST['quantity']);
$total = $quantity * $price;
$total = number_format ($total, 2);
// Validate the name and combat Magic Quotes, if necessary.
if (empty ($_POST['product'])) {
$errors[]='You forgot to enter product.';
}else{
$pr=trim($_POST['product']);
}
// Validate the price.
if (empty ($_POST['price'])) {
$errors[] ='You forgot to enter price.';
} else {
$p=trim($_POST['price']);
}
// Validate the quantity.
if (empty ($_POST['quantity'])) {
$errors[] ='You forgot to enter quantity.';
} else {
$q=trim($_POST['quantity']);
}
if(empty($errors)){
require_once('checkout_connect.php');
$query="INSERT INTO customers(product,price,quantity)VALUES('$pr','$p','$q')";
$result=#mysql_query($query);//Run the query.
if($result){
echo 'You are purchasing <b>', $c. '</b>.
Total price is <b>$', $total, '</b>.';
} else { // One from element was not filled out properly.
echo '<p><font color="orange">Please go back and fill out the form again.</font></p>';
}
exit();
}else{ //If it did not run OK.
echo '<h1 id="mainhead">System Error</h1>
<p class="error">You could not registered due to system error.We apologize for any inconvenience.</p>';//Public message.
echo '<p>'.mysql_error().'<br/><br/>'.$query.'</p>'; //Debugging message.
exit();
}
mysql_close(); //Close the database connection.
}else{ //Report the errors.
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
if (is_array($errors)) {
foreach ($errors as $msg){ //Print each error.
echo " - $msg<br />\n";
}
}
echo '</p><p>Please try again.</p><p><br /></p>';
}//End of if (empty($errors)) IF.
?>
<h2>ORDER FORM:</h2>
<form action="checkout.php" method="post">
<p>Product: <input type="number" name="code_item" size="15" maxlength="15" value"<?php if (isset($_POST['product'])) echo $_POST['product']; ?>" /></p>
<p>Price: <input type="number" name="price" size="15" maxlength="30" value"<?php if (isset($_POST['price'])) echo $_POST['price']; ?>" /></p>
<p>Quantity: <input type="number" name="quantity" size="30" maxlength="50" value"<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>``
Define it at the top of the document.
<?php
$errors = false;
Alternatively, check if it is set using a ternary where assignment is necessary.
echo isset($errors) ? $errors: '';
Where '' can be replaced with whatever assignment correlates to the requirements.

Categories