Form validation issue PHP - php

I need to create a form, which will check if the input is empty or not. If it is empty there should be a text like "Required field". There is a notice saying, that surName has an undefined index.
Here is my PHP code
<?php
$name_error="";
$sname_error="";
$f_name="";
$s_name="";
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}
else {
$name_error="Required Field *";
}
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else {
$sname_error="Please fill this out";
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?
php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
</form>
</body>
</html>

well for starters, you could use the built-in HTML5 input validator required
<input type="text" required>
This will provide that nice user message on submit asking them to please fill out that field.
Secondly, you can check on the server using empty instead of if ($_POST['surName']!=='')
if ( !empty($_POST['surName']) ) {
// your logic here
}

The functionality is basically there already...
I guess the } bracket for if (isset($_POST['submit_button'])) { should be just before ?>
There is missing something like <input type="submit" name="submit_button" value="Register">
And considering what Jeff Puckett II says
So, if I do the changes 1 and 2 to your code, it might look like this:
<?php
$name_error="";
$sname_error="";
$f_name="";
$s_name="";
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name = $_POST['firstName'];
} else {
$name_error="Required Field *";
}
if ($_POST['surName']!=='') {
$s_name = $_POST['surName'];
} else {
$sname_error="Please fill this out";
}
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
<input type="submit" name="submit_button" value="Send">
</form>
</body>
</html>

It is because you've put the surName validation outsite of submit conditional;
`
if (isset($_POST['submit_button'])) {
if ($_POST['firstName']!=='') {
$f_name=$_POST['firstName'];
}else{
$name_error="Required Field *";
}
if ($_POST['surName']!=='') {
$s_name=$_POST['surName'];
}
else{
$sname_error="Please fill this out";
}
}
?>`
Your can improve this a little bit by using an associative array like this errors = array(); and then $errors['surName'] = "this is field is required";

You don't have a submit button. Another thing is that you didn't use your if statement of $_POST['surName'] after submit. You can use it like this :
<?php
error_reporting(1);
$name_error = "";
$sname_error = "";
$f_name = "";
$s_name = "";
if (isset($_POST['submit_button']))
{
if ($_POST['firstName'] != '')
{
$f_name = $_POST['firstName'];
}
else
{
$name_error = "Required Field *";
}
if ($_POST['surName'] != '')
{
$s_name = $_POST['surName'];
}
else
{
$sname_error = "Please fill this out";
}
}
?>
<html>
<head>
<title>Registration form</title>
</head>
<body>
<div class="head">
<p>Registration Form</p>
</div>
<form action="Register/final.php" method="POST">
<label for="firstName">First Name</label><br>
<input type="text" name="firstName" placeholder="First Name" value="<?php echo $f_name; ?>"><br><br>
<p style="color: red;"><?php echo $name_error; ?></p>
<label for="surName">Last Name</label><br>
<input type="text" name="surName" placeholder="Last Name" value="<?php echo $s_name;?>"><br><br>
<p style="color: red;"><?php echo $sname_error;?></p>
<input type="submit" name="submit_button" value="Register">
</form>
</body>
</html>

Related

PHP isset($_POST["Submit1"])) is not working

So I am trying to run this code and at the if (isset($_POST["Submit1"])) statement for isset, the form will not give the error message whenever I leave my form empty. It is supposed to give the $nameErr whenever the form is left empty, but it is not giving that error message. Whenever I delete the if (isset($_POST["Submit1"])) statement, it will run the else statement fine.
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
Here is the submit button code.
<input type="submit" name="Submit1" value="Calculate">
As you can see, the names are the same so I don't think that is the problem.
Here is the rest of my code if you wish to take a look.
<!DOCTYPE html>
<html lang="en">
<head>
<title>GPA Improvement Calculator</title>
<style>
.error {
color: #FF0000;
}
</style>
</head>
<body>
<h1>GPA Improvement Calculator</h1>
<p><span class="error">All form fields must be completed for the GPA calculator to function.</span></p>
<?php
function sanitizeString($var)
{
$var = stripslashes($var);
$var = strip_tags($var);
$var = htmlentities($var);
return $var;
}
$name = "";
$nameErr = "";
if (isset($_POST["Submit1"]))
{
if (isset($_POST['name']))
{
$name = sanitizeString($_POST['name']);
} else {
$nameErr = "* Your name must consist of letters and whitespace.";
}
}
?>
<form method="post" action="improveGPA.php">
Name: <input type="text" size="35" name="name" value="<?php echo $name; ?>">
<span class="error"><?php echo $nameErr; ?></span>
<br><br>
E-mail: <input type="text" size="35" name="email" value="">
<span class="error"></span>
<br><br>
<input type="checkbox" name="agree" >
I agree to the terms and conditions of this website.
<span class="error"></span>
<br><br>
Current GPA: <input type="text" size="4" name="currentGPA" value="">
<span class="error"></span>
<br><br>
Current Total Credits: <input type="text" size="3" name="currentCredits" value="">
<span class="error"></span>
<br><br>
I am taking <input type="text" size="3" name="newCredits" value="">
<span class="error"></span> credits this semester.
If I want to raise my GPA
<input type="text" size="4" name="GPAincrease" value="">
<span class="error"></span> points,
I need a <span style="font-weight: bold;">????</span> GPA on my courses this semester.
<br><br>
<input type="submit" name="Submit1" value="Calculate">
</form>
</body>
</html>
It will only start messing up if I have that one if statement in and just won't show that error message at all, when the form is empty or filled in.

PHP Form Validation and showing error message beside Input Fields

I am trying to Show an error message besides the input fields but I am not able to do so. I am not able to find what mistake I am making here. Below is the code of form and PHP. My code looks to me right but in browser I am not getting desired output as I am stuck on it. I would be thankful if some would help me.
<?php
$Name_Error = "";
$Email_Error="";
$Website_Error="";
$Gender_Error="";
function Test_User_Input($User_Data){
return $User_Data;
}
if(isset($_POST['Submit'])){
if(empty($_POST["Name"])){
$Name_Error = "Kindly Enter the Name!";
}
else {
$Name = Test_User_Input($_POST["Name"]);
}
if(empty($_POST["Email"])){
$Email_Error = "Kindly Enter the Eamil Address!";
}
else {
$Email = Test_User_Input($_POST["Email"]);
}
if(empty($_POST["Website"])){
$Website_Error = "Kindly Enter the Website URL!";
}
else {
$Website = Test_User_Input($_POST["Website"]);
}
if(empty($_POST["Gender"])) {
$Gender_Error = "Kindly Select your Gender!";
}
else {
$Gender = Test_User_Input($_POST["Gender"]);
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Simple Form</title>
</head>
<body>
<form>
<label>Enter your Name</label>
<br>
<input type="text" name="Name">*<?php echo $Name_Error ?>
<br>
<label>Enter your Email Address</label>
<br>
<input type="text" name="Email">*<?php echo $Email_Error ?>
<br>
<label>Enter your Website</label>
<br>
<input type="text" name="Website">*<?php echo $Website_Error ?>
<br>
<label>Select your Gender</label>
<br>
<input type="radio" name="Gender" value="Male"> Male
<input type="radio" name="Gender" value="Female">Female *<?php echo $Gender_Error ?>
<br>
<label>Comments
<br>
<textarea name="Comment"></textarea>
<br>
<input type="Submit" name="Submit">
</form>
</body>
</html>
You need to call a specific page to trigger your PHP, in this case it's the page itself, the method is POST
change <form> to <form action="" method="POST">
Add form action="" and method="POST". That will fix your problem.
<?php
$Name_Error = $Email_Error = $Gender_Error = $Website_Error = "";
$Name = $Email = $Gender = $Website = "";
if(isset($_POST['Submit'])){
if (empty($_POST["Name"])) {
$Name_Error = "Name is required";
} else {
$Name = test_input($_POST["Name"]);
}
if (empty($_POST["Email"])) {
$Email_Error = "Email is required";
} else {
$Email = test_input($_POST["Email"]);
}
if (empty($_POST["Website"])) {
$Website_Error = "Kindly Enter the Website URL!";
} else {
$Website = test_input($_POST["Website"]);
}
if (empty($_POST["Gender"])) {
$Gender_Error = "Gender is required";
} else {
$Gender = test_input($_POST["Gender"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="Name">
<span class="error">* <?php echo $Name_Error;?></span>
<br><br>
E-mail: <input type="text" name="Email">
<span class="error">* <?php echo $Email_Error;?></span>
<br><br>
Website: <input type="text" name="Website">
<span class="error"><?php echo $Website_Error;?></span>
<br><br>
Comment: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="Gender" value="female">Female
<input type="radio" name="Gender" value="male">Male
<input type="radio" name="Gender" value="other">Other
<span class="error">* <?php echo $Gender_Error;?></span>
<br><br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $Name;
echo "<br>";
echo $Email;
echo "<br>";
echo $Website;
echo "<br>";
echo $Gender;
?>
</body>
</html>

How to automatically focus cursor on input field after PHP validation

How can I automatically focus the cursor on an input field after PHP validation. If user enters an incorrect answer, I want to focus on that field when the page loads.
I'm new to PHP. How can I do this in PHP?
Contact Form:
<form method="post" action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<fieldset style="width:960px; background-color:#FFF; border-radius:10px; padding:50px;">
<legend><h2>Contact Us</h2></legend>
<br />
<fieldset style="border-radius:10px; padding:50px 0px 50px 50px;;";>
<div style="width:275px; height:300px; float:right; padding:30px"><img src="images/contactus.jpg" width="275" height="300" alt="contactus" /></div>
<label>Name:</label>
<input class="txt" type="text" name="name" value="<?php echo isset($_POST['name']) ? htmlentities($_POST['name']) : ''; ?>"/><span class="error"><?php echo $nameErr?></span><br /><br />
<label>Email:</label>
<input class="txt" type="text" name="email" value="<?php echo isset($_POST['email']) ? htmlentities($_POST['email']) : ''; ?>"/><span class="error"><?php echo $emailErr?></span><br /><br />
<label>Mobile No:</label>
<input class="txt" type="text" name="mobileno" value="<?php echo isset($_POST['mobileno']) ? htmlentities($_POST['mobileno']) : ''; ?>"/><span class="error"><?php echo $mobilenoErr?></span><br /><br />
<label>Message:</label>
<textarea class="txt" name="message"rows="5"><?php echo $message?></textarea><span class="error"><?php echo $messageErr?></span><br /><br />
<input style="margin-left:100px; padding:5px 20px 5px 20px; border-radius:5px" type="submit" value = "Submit" />
</fieldset>
</fieldset>
</form>
Contact form validation in PHP:
<?php
if(isset($_POST['name']))
{
include_once("config.php");
$name=$email=$mobileno=$message=="";
$nameErr=$emailErr=$mobilenoErr=$messageErr="";
function test_input($data)
{
$data=trim($data);
$data-stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
if($_SERVER['REQUEST_METHOD']=="POST")
{
$valid=true;
//name validaton
if(empty($_POST["name"]))
{
$nameErr="* Name is Required";
$valid=false;
}
else
{
$name=test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = " Only letters and white space allowed";
$valid=false;
}
}
//Email Address validaton
if(empty($_POST["email"]))
{
$emailErr="* Email is Required";
$valid=false;
}
else
{
$email=test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr=" Enter a valid Email ID";
$valid=false;
}
}
//Mobile no validaton
if(empty($_POST["mobileno"]))
{
$mobilenoErr="* Mobile no is Required";
$valid=false;
}
else
{
$mobileno=test_input($_POST["mobileno"]);
if(!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$mobileno))
{
$mobilenoErr="*Enter a valid contact no";
$valid=false;
}
}
//bank name validation
if(empty($_POST["message"]))
{
$messageErr="* Message is Required";
$valid=false;
}
else
{
$message=test_input($_POST["message"]);
}
}
if($valid)
{
echo "Send mail code";
}
}
?>
You can try autofocus (http://diveintohtml5.info/forms.html):
<input class="txt" type="text" name="name"
value="<?php echo isset($_POST['name']) ? htmlentities($_POST['name']) : ''; ?>"
<?php if(!empty($nameErr)) { ?> autofocus <?php } ?>
/>
<span class="error"><?php echo $nameErr?></span><br /><br />
This is just the html option autofocus.
Here you can find a good explanation
<input type="text" name="fname" autofocus>
Or you can do it with javascript, if the IE makes some problems or you arn't coding with html5:
<body OnLoad='document.getElementById("txt").focus();'>
But if you want to do it like this, you must set an id to your inputfield.
The HTML5 autofocus feature with if logic as suggested by Ramesh is correct; however the code is not quite right for an if statement mixed with html. Instead of the normal braces, you need to use a colon and add the php endif statement. Try this:
<input class="txt" type="text" name="name"
value="<?php echo isset($_POST['name']) ? htmlentities($_POST['name']) : ''; ?>"
<?php if(!empty($nameErr)): ?> autofocus <?php endif; ?>
/>
<span class="error"><?php echo $nameErr; ?></span><br /><br />

php contact form validation which stops showing form on no errors and success

Here's my code:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["yourname"])) {
$yournameErr = "Name is required";
} else {
$yourname = test_input($_POST["yourname"]);
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["message"])) {
$messageErr = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
I have got to the point where it doesn't show errors but I probably didn't explain myself too clearly. After the point at which it doesn't show error messages anymore, I would like the form to no longer appear and then I can put something down like "Successful." However I can't seem to achieve this.
my form is :
<form action="contact.php" name="Form1" id="Form1" method="post">
<div>
<label>Your Name:</label>
<br />
<input type="text" name="yourname" id="yourname" placeholder="Full Name"
style="border:1; border-color:#000000; " />
<span class="error">* <?php echo $yournameErr;?></span>
</div>
<br />
<br />
<div>
<label> Email :</label> <br />
<input name="email" type="text" id="email" size="20" placeholder="Email"
style="border:1; border-color:#000000; " />
<span class="error">* <?php echo $emailErr;?></span>
</div>
<br />
<br />
<div>
<label> Subject : </label><br />
<input name="subject" type="text" id="subject" size="20" placeholder="Subject"
style="border:1; border-color:#000000; " />
</div>
<br />
<br />
<div>
<label> Message :<br /> </label>
<textarea rows="5" cols="40" name="message" type="text" id="message"
placeholder="The message you want to send to us." style="border:1; border-
color:#000000 " >
</textarea>
<span class="error">* <?php echo $messageErr;?></span>
</div>
<br />
<br />
<div>
<input type="submit" name="button" id="button" style="border:1; border-
color:#999999; " value="SEND"/>
</div>
</form>
What if you put your errors in an array and put a condition that check that array size and if it is 0 (no errors) echo the success message and don't show the form else join the errors and print it out.
Maybe like this:
contact.php
<?php
$error = array();
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["yourname"])) {
$error['name'] = "Name is required";
} else {
$yourname = test_input($_POST["yourname"]);
}
if (empty($_POST["email"])) {
$error['email'] = "Email is required";
} else {
$email = test_input($_POST["email"]);
// check if e-mail address syntax is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$error['email'] = "Invalid email format";
}
}
if (empty($_POST["message"])) {
$error['message'] = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
if (!count($error)) {
$noError = true;
}
}
$successMessage = isset($noError) ? 'Successful.' : '';
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function getErrorMessage($type, $error)
{
return isset($error[$type]) ? $error[$type] : '';
}
if ($successMessage) {
echo $successMessage;
} else {
?>
<form action="contact.php" name="Form1" id="Form1" method="post">
<div>
<label>Your Name:</label>
<br/>
<input type="text" name="yourname" id="yourname" placeholder="Full Name"
style="border:1px; border-color:#000000; "/>
<span class="error">* <?php echo getErrorMessage('name', $error); ?></span>
</div>
<br/>
<br/>
<div>
<label> Email :</label> <br/>
<input name="email" type="text" id="email" size="20" placeholder="Email"
style="border:1px; border-color:#000000; "/>
<span class="error">* <?php echo getErrorMessage('email', $error); ?></span>
</div>
<br/>
<br/>
<div>
<label> Subject : </label><br/>
<input name="subject" type="text" id="subject" size="20" placeholder="Subject"
style="border:1px; border-color:#000000; "/>
</div>
<br/>
<br/>
<div>
<label> Message :<br/> </label>
<textarea rows="5" cols="40" name="message" type="text" id="message"
placeholder="The message you want to send to us." style="border:1px; border-
color:#000000 "></textarea>
<span class="error">* <?php echo getErrorMessage('message', $error); ?></span>
</div>
<br/>
<br/>
<div>
<input type="submit" name="button" id="button" style="border:1px; border-
color:#999999; " value="SEND"/>
</div>
</form>
<?php } ?>
According to your code, I'm going to assume that your contact.php is posting to itself. In other words, your PHP code is located above your HTML --as a result of your question that the contact form no longer renders after a request. That is, once the server renders the page, the form tag will not display because there are no errors in the submission or the super global $_POST has been set.
I've slightly altered your code for readability. I included an array that will hold all your error messages throughout your form validation. If there are no errors, then we can simulate a successful submission and thus reflect this result on response. Your form will only display if there are errors OR the post has not been submitted.
Inside your form, you need an input tag of submit. Furthermore, only if errors are indeed set, that we want to display an error specific message. That is why a condition is set for your span tags. Lastly, I included the same condition in your values -an additional feature for remembering what you have entered.
If the form is successfully submitted, then don't render the form tag and, instead, display a message of success along with all the POST data!
<?php
if (isset($_POST['submit'])) {
$error_message = array();
if (empty($_POST["yourname"])) {
$error_message['yournameErr'] = "Name is required";
} else {
$yourname = test_input($_POST["yourname"]);
}
if (empty($_POST["email"])) {
$error_message['emailErr'] = "Email is required";
} elseif (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $_POST["email"])) {
$error_message['emailErr'] = "Invalid email format";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["message"])) {
$error_message['messageErr'] = "Message is required";
} else {
$message = test_input($_POST["message"]);
}
if(empty($error_message)) {
// process data from post
$successful = true;
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php if(!empty($error_message) || !isset($_POST['submit'])) : ?>
<form action="contact.php" name="Form1" id="Form1" method="post">
<div>
<label>Your Name:</label>
<br />
<input type="text" name="yourname" id="yourname" placeholder="Full Name"
style="border:1; border-color:#000000; " value="<?php if(isset($yourname)) {echo $yourname; }?>" />
<span class="error">* <?php if(isset($error_message['yournameErr'])){echo $error_message['yournameErr']; }?></span>
</div>
<br />
<br />
<div>
<label> Email :</label> <br />
<input name="email" type="text" id="email" size="20" placeholder="Email"
style="border:1; border-color:#000000; " value="<?php if(isset($email)) { echo $email;}?>" />
<span class="error">* <?php if(isset($error_message['emailErr'])) {echo $error_message['emailErr'];}?></span>
</div>
<br />
<br />
<div>
<label> Subject : </label><br />
<input name="subject" type="text" id="subject" size="20" placeholder="Subject"
style="border:1; border-color:#000000; " />
</div>
<br />
<br />
<div>
<label> Message :<br /> </label>
<textarea rows="5" cols="40" name="message" type="text" id="message" placeholder="The message you want to send to us." style="border:1; border-color:#000000"></textarea>
<span class="error">* <?php if(isset($error_message['messageErr'])) {echo $error_message['messageErr']; }?></span>
<br>
<input type="submit" name="submit" value="Submit">
</div>
<?php endif; ?>
<?php if(isset($successful)) : ?>
<p>Successful</p>
<p>Your name: <?=$yourname;?></p>
<p>Your email: <?=$email;?></p>
<p>Your subject: <?=$_POST['subject']?></p>
<p>Your message: <?=$message;?></p>
Back to form
<?php endif; ?>

Form PHP code is output to the screen

<?php
function VerifyForm(&$values, &$errors)
{
if (strlen($values['fname']) == 0)
$errors['fname'] = 'Enter First Name';
if (strlen($values['lname']) == 0)
$errors['lname'] = 'Enter Last Name';
if (strlen($values['mname']) == 0)
$errors['mname'] = 'Enter Middle Name';
if (strlen($values['address']) == 0)
$errors['address'] = 'Enter Address';
if (strlen($values['terms']) == 0)
$errors['terms'] = 'Please Read Terms and Agreement and Check the box.';
if (!ereg('.*#.*\..{2,4}', $values['email']))
$errors['email'] = 'Email address invalid';
else if (strlen($values['email']) < 0)
$errors['email'] = 'Enter Email Address';
return (count($errors) == 0);
}
function DisplayForm($values, $errors)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GIA Soap » Products » Customer Informations</title>
<link href="stylesheet/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js_files/jquery.js"></script>
<script type="text/javascript" src="js_files/sliding_effect.js"></script>
<script type="text/javascript" src="js_files/slideshow.js"></script>
</head>
<body>
<div class="bg_top">
<div class="bg_bottom">
<div class="wrapper">
<div class="header">
<div class="logo">
</div>
<div class="logo_text">
<div class="logo_head_text">Gia Soap Making</div>
<div class="logo_sub_text">Sub text here</div>
</div>
</div>
<div class="h_nav">
<div class="h_nav_dash">
</div>
</div>
<div class="container">
<div class="content_term">
<div class="content_terms">
<br />
<h1><p>Customer Information</p></h1><br />
<p>Please the following correctly.</p>
<div class="customer_info">
<?php
if (count($errors) > 0)
echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
?>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<!-- hidden values -->
<input type="hidden" value="<?php echo $papaya; ?>" name="papaya" />
<input type="hidden" value="<?php echo $carrot; ?>" name="carrot" />
<input type="hidden" value="<?php echo $guava; ?>" name="guava" />
<label for="customer_fname">First Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['fname']) ?>" />
<span class="error_msg"><?= $errors['fname'] ?></span>
<label for="customer_lname">Last Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['lname']) ?>" />
<span class="error_msg"><?= $errors['lname'] ?></span>
<label for="customer_mname">Middle Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['mname']) ?>" />
<span class="error_msg"><?= $errors['mname'] ?></span>
<label for="customer_add">Address (<i>Required : Complete Address Please</i>)</label>
<input type="text" class="textbox" id="customer_add" name="customer_add1" value="<?= htmlentities($values['address']) ?>" /><br />
<input type="text" class="textbox" id="customer_add" name="customer_add2" /><br />
<input type="text" class="textbox" id="customer_add" name="customer_add3" />
<span class="error_msg"><?= $errors['address'] ?></span>
<label for="customer_email">Email Address (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_email" name="customer_email" value="<?= htmlentities($values['email']) ?>" />
<span class="error_msg"><?= $errors['email'] ?></span>
<label for="customer_phone">Phone Number </label>
<input type="text" class="textbox" id="customer_phone" name="customer_phone" />
<label for="customer_mobile">Mobile Number </label>
<input type="text" class="textbox" id="customer_mobile" name="customer_mobile" />
<br /><br />
<div class="terms">
<center>
<h1>Terms and Agreement</h1><br />
<p>Please read the following.</p><br />
</div>
<br />
<input type="checkbox" name="terms" value="<?= htmlentities($values['terms']) ?>" /> I Read the Terms and Agreement<br /><br />
<span class="error_msg"><?= $errors['terms'] ?></span>
<input type="submit" value="Send Order" class="prod_subbtn" />
</center>
</form>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<?php include ('includes/footer.php'); ?>
</div>
</div>
</div>
</body>
</html>
<?php
}
function ProcessForm($values)
{
$papaya = $_POST['papaya'];
$carrot = $_POST['carrot'];
$guava = $_POST['guava'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mname = $_POST['mname'];
$address = $_POST['address'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$formValues = $_POST;
$formErrors = array();
if (!VerifyForm($formValues, $formErrors))
DisplayForm($formValues, $formErrors);
else
ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>
The output is:
Problem
The PHP code that is supposed to put in the field values can be seen by users.
Chances are short_open_tags is off. Use <?php echo ...; ?> instead of <?=... ?>, like this:
<?php echo htmlentities($values['lname']); ?>
<?= $errors['fname'] ?> is equal to <?php echo $errors['fname'] ?>.
<?= are called 'short tags', which were removed (deprecated) from php.
Use <?php echo $errors['fname']; ?> to see the actual variable value.
The directive short tags is set to off in the php.ini. That disallows <? $phpcode ?> and <?=$monkey?>
The only one allowed is <?php $monkeybusiness ?>
either change <?= to <?php echo or turn short_open_tags = on in the php.ini

Categories