I just saw this codes from this website and thinking to implement it to my project however I cannot get my data on the next page. I am using the test.php as a index and submit.php as a page that will catch the post
here is the codes
test.php
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// Initialize variables and set to empty strings
$firstName=$lastName="";
$firstNameErr=$lastNameErr="";
// Validate input and sanitize
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method.
if (empty($_POST["firstName"])) {
$firstNameErr = "First name is required";
$valid = false; //false
}
else {
$firstName = test_input($_POST["firstName"]);
}
if (empty($_POST["lastName"])) {
$lastNameErr = "Last name is required";
$valid = false;
}
else {
$lastName = test_input($_POST["lastName"]);
}
//if valid then redirect
if($valid){
header('Location: submit.php');
exit();
}
}
// Sanitize data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Find Customer</h2>
<p><span class="error">* required</span></p>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
First Name: <input type="text" name="firstName" value="<?php echo $firstName; ?>"><span class="error">* <?php echo $firstNameErr; ?></span><br><br>
Last Name: <input type="text" name="lastName" value="<?php echo $lastName; ?>"><span class="error">* <?php echo $lastNameErr; ?><br><br>
<input type="submit">
</form>
</body>
</html>
submit.php
<?php
$test=$_POST['lastName'];
echo $test;
?>
Your form action should be the file where you want to get the form submitted values .
change this <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post"> to <form action="submit.php" method="post">
Related
I am attempting to display the results without the form being shown at the same time. So, initially when they go to the URL they see the form, and after they fill out the form and the form validation and required fields and URL is valid. Here is what I started with.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$TXTlinknameErr = $TXTurlErr = "";
$TXTlinkname = $TXTurl = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["TXTlinkname"])) {
$TXTlinknameErr = "Name is required";
} else {
$TXTlinkname = test_input($_POST["TXTlinkname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$TXTlinkname)) {
$TXTlinknameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["TXTurl"])) {
$TXTurl = "";
} else {
$TXTurl = test_input($_POST["TXTurl"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$TXTurl)) {
$TXTurlErr = "Invalid URL";
}
}
if (empty($_POST["TXTurl"])) {
$TXTurlErr = "URL is required";
} else {
$TXTurl = test_input($_POST["TXTurl"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Create HTML Link</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="TXTlinkname" value="<?php echo $TXTlinkname;?>">
<span class="error">* <?php echo $TXTlinknameErr;?></span>
<br><br>
URL: <input type="text" name="TXTurl" value="<?php echo $TXTurl;?>">
<span class="error"><?php echo $TXTurlErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your HTML Code:</h2>";
echo "<br>";
echo '<textarea name="htmlcode" rows="10" cols="60">' . $TXTlinkname . '</textarea>';
?>
</body>
</html>
Here is what I have tried.
I've tried adding else statements after body and before results. I'd like the results not to show until after form submitted.
Here's what I have so far...
I tried to add the below after the body
<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])||(($_POST['name']) == "")){
?>
Then I added:
<?php
} else {
//Retrieve show string from form submission.
Just after
// define variables and set to empty values
Finally added:
<?php
} ?>
Before /body
Here is what I tried.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
//If form not submitted, display form.
if (!isset($_POST['submit'])||(($_POST['TXTlinkname'] && $_POST['TXTurl']) == "")){
?>
<?php
// define variables and set to empty values
$TXTlinknameErr = $TXTurlErr = "";
$TXTlinkname = $TXTurl = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["TXTlinkname"])) {
$TXTlinknameErr = "Name is required";
} else {
$TXTlinkname = HTML_input($_POST["TXTlinkname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$TXTlinkname)) {
$TXTlinknameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["TXTurl"])) {
$TXTurl = "";
} else {
$TXTurl = HTML_input($_POST["TXTurl"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$TXTurl)) {
$TXTurlErr = "Invalid URL";
}
}
if (empty($_POST["TXTurl"])) {
$TXTurlErr = "URL is required";
} else {
$TXTurl = HTML_input($_POST["TXTurl"]);
}
}
function HTML_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Create HTML Link</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="TXTlinkname" value="<?php echo $TXTlinkname;?>">
<span class="error">* <?php echo $TXTlinknameErr;?></span>
<br><br>
URL: <input type="text" name="TXTurl" value="<?php echo $TXTurl;?>">
<span class="error"><?php echo $TXTurlErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
} else {
echo "<h2>Your HTML Code:</h2>";
echo "<br>";
echo '<textarea name="htmlcode" rows="10" cols="60">' . $TXTlinkname . '</textarea>';
?>
<button onclick="location = location.href">Go Back</button>
<?php
} ?>
</body>
</html>
So, initially when they go to the URL they see the form, and after they fill out the form and the form validation and required fields and URL is valid.
The big issue I see with your code is the if statement. The variables are not defined unless form hasn't been submitted. What I've changed is moved the function to be defined globally along with the variable names, and inverted the if statement. PHP Tags, you don't need them everywhere. One wrapper is good enough.
I'm not sure about what your result is, but for what you asked, I shall provide.
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
function HTML_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$TXTlinkname = $TXTurl = "";
$TXTlinknameErr = $TXTurlErr = "";
//If form not submitted, display form.
if (isset($_POST['submit'])){
if (empty($_POST['TXTurl'])) {
$TXTurlErr = "URL is required";
} else {
$TXTurl = HTML_input($_POST['TXTurl']);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$TXTurl)) {
$TXTurlErr = "Invalid URL";
}
}
if (empty($_POST['TXTname'])) {
$TXTlinknameErr = "Name is required";
} else {
$TXTlinkname = HTML_input($_POST['TXTname']);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$TXTlinkname)) {
$TXTlinknameErr = "Only letters and white space allowed";
}
}
}
if (empty($TXTurlErr) && empty($TXTlinknameErr) && isset($_POST['submit'])) {
echo "<h2>Your HTML Code:</h2>";
echo "<br>";
echo '<textarea name="htmlcode" rows="10" cols="60">' . $TXTlinkname . '</textarea>';
echo '<button onclick="location = location.href">Go Back</button>';
} else {
echo '<h2>Create HTML Link</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'">
Name: <input type="text" name="TXTname" value="'.$TXTlinkname.'">
<span class="error">* '. $TXTlinknameErr .'</span>
<br><br>
URL: <input type="text" name="TXTurl" value="'.$TXTurl.'">
<span class="error">'.$TXTurlErr.'</span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>';
}
?>
</body>
</html>
Tested locally on XAMPP
You have to have a variable that tells you if you're going to show the textarea or not...
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$TXTlinknameErr = $TXTurlErr = "";
$TXTlinkname = $TXTurl = "";
$show_textarea = false; //DEFAULT
if (isset($_POST['submit'])) { //The form is sent...
$show_textarea = true; //Then this is DEFAULT!!
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["TXTlinkname"])) {
$show_textarea = false; //DON'T SHOW TEXTAREA
$TXTlinknameErr = "Name is required";
} else {
$TXTlinkname = test_input($_POST["TXTlinkname"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$TXTlinkname)) {
$show_textarea = false; //DON'T SHOW TEXTAREA
$TXTlinknameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["TXTurl"])) {
$show_textarea = false; //DON'T SHOW TEXTAREA
$TXTurl = "";
} else {
$TXTurl = test_input($_POST["TXTurl"]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$TXTurl)) {
$show_textarea = false; //DON'T SHOW TEXTAREA
$TXTurlErr = "Invalid URL";
}
}
if (empty($_POST["TXTurl"])) {
$show_textarea = false; //DON'T SHOW TEXTAREA
$TXTurlErr = "URL is required";
} else {
$TXTurl = test_input($_POST["TXTurl"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($show_textarea === false) {
?>
<h2>Create HTML Link</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="TXTlinkname" value="<?php echo $TXTlinkname;?>">
<span class="error">* <?php echo $TXTlinknameErr;?></span>
<br><br>
URL: <input type="text" name="TXTurl" value="<?php echo $TXTurl;?>">
<span class="error"><?php echo $TXTurlErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
}
if (isset($_POST['submit'])) { //The form is sent...
if ($show_textarea === true ) { //...AND the form has valid values
echo "<h2>Your HTML Code:</h2>";
echo "<br>";
echo '<textarea name="htmlcode" rows="10" cols="60">
' . $TXTlinkname . '
</textarea>';
}
}
?>
</body>
</html>
try this one:
if (!$_POST) || $_POST['TXTlinkname'] == "" && $_POST['TXTurl']) == "")){
i have a problem with the validation my input and select fields.
If the inputs are empty and click on submit the warning "this field is required" appears.
But when i fill the ClientID input and the selection box is empty the validation will fail. Or invers, when the select box is selected and the input is empty.
here my code:
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$nameErr = $katalogErr = $selectKatalog = "";
$kdn = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["kdn"])) {
$nameErr = "This field is required";
} else {
$kdn = test_input($_POST["kdn"]);
// check if kdn only contains letters, numbers and whitespace
if (!preg_match("/^[a-zA-Z0-9 ]*$/", $kdn)) {
$nameErr = "Only letters, numbers and white space allowed";
}
}
if($_POST["selectKatalog"] == 'default'){
$katalogErr = "This field is required";
}
else {
$selectKatalog = $_POST["selectKatalog"];
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>Order a Catalog</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
ClientID: <input type="text" name="kdn">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Catalog: <select name="selectKatalog">
<option value="default">Bitte wählen:</option>
<option value="Catalog1">Catalog1</option>
<option value="Catalog2">Catalog2</option>
<option value="Catalog3">Catalog3</option>
</select>
<span class="error">* <?php echo $katalogErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>Your Input:</h2>";
echo $kdn;
echo "<br>";
echo $selectKatalog;
?>
</body>
</html>
I am trying to do form field validation with php. I am checking if the submit button isset first and then if the form fields are empty, save it in a error variable, else if its not empty I have a function doing som form cleanup and setting the value to the textfield.Then submitted to another page.
But my error handling never happens and the form with empty fields are submitted to my action page url.
Whats wrong with code:
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$fnameErr = "";
$lnameErr = "";
$Fname = "";
$Lname = "";
if(isset($_POST["submit"])) {
if (empty($_POST["FirstName"])) {
$fnameErr = "First name is required";
} else {
$Fname = form_input($_POST["FirstName"]);
}
if (empty($_POST["LastName"])) {
$lnameErr = "Last name is required";
} else {
$Lname = form_input($_POST["LastName"]);
}
}
function form_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 action="intro.html.php" method="POST">
Förnamn: <input type="text" name="FirstName">
<span class="error">* <?php echo $fnameErr;?></span>
<br>
Efternamn: <input type="text" name="LastName">
<span class="error">* <?php echo $lnameErr;?></span>
<br>
<input type="submit" name="submit" value="Skicka">
</form>
</body>
</html>
Thank you
UPDATE
if file of action form is different with the form file you need to check the input on action.
here the index.php
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
/* get the return value when is not valid on into.html.php*/
$fname = $_GET['fName']; $lname = $_GET['lName'];
if($_GET['fName']=='false'){
$fnameErr = "First name is required"; /*set value to show notif*/
$fname ="";
}
if($_GET['lName']=='false'){
$lnameErr = "First name is required"; /*set value to show notif*/
$lname ="";
}
?>
<h2>PHP Form Validation Example</h2>
<form action="intro.html.php" method="POST">
Förnamn: <input type="text" name="FirstName" value="<?php echo $fname;?>">
<span class="error">* <?php echo $fnameErr;?></span>
<br>
Efternamn: <input type="text" name="LastName" value="<?php echo $lname;?>">
<span class="error">* <?php echo $lnameErr;?></span>
<br>
<input type="submit" name="submit" value="Skicka">
</form>
</body>
</html>
and then set your intro.html.php like this
<?php
function form_input($data) {
if($data!=""){ /*add condition if data != "" then run so we don't need more else on section if $_POST is empty*/
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
}
return $data;
}
if(isset($_POST["submit"])) { /*check the submit*/
$Fname = form_input($_POST["FirstName"]); /* get post data from input val to variable $Fname*/
$Lname = form_input($_POST["LastName"]); /* get post data from input val to variable $Lname*/
if(!empty($_POST["FirstName"]) and !empty($_POST["LastName"])!=""){ /*cek if FirstName and LasName is not empty*/
/* do what you want to do here*/
echo ' FName : '.$Fname.' | LName : '.$Lname;
}
else{
/*we will redirect to index.php using javascript*/
echo '
<script>
alert("all input must be field!");
window.location.href="index.php?fName='.($_POST["FirstName"]==""?'false':$Fname).'&lName='.($_POST["LastName"]==""?'false':$Lname).'"; /* this ridect to index.php and set variable $_GET fName and variable lName */
</script>
';
}
}
else{
echo 'submit is undefined!'; /*echo submit empty, */
}
?>
if you want to check input before submit, you can use HTML <input> required Attribute here the documentation
or you can use jquery to proses ajax submit or just check the input value you can see more in here
hope this help.
here is my php form code...
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Page Title Goes Here</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="form1.css"/>
</head>
<body>
<?php
// define variables and set to empty value
$firstNameError = "";
$lastNameError = "";
$error = false;
// if firstName is empty, make it NULL, else, test_input() the data.
$firstName = empty($_POST["firstName"]) ? NULL : test_input($_POST["firstName"]);
// if lastName is empty, make it NULL, else, test_input() the data.
$lastName = empty($_POST["lastName"]) ? NULL : test_input($_POST["lastName"]);
if (isset($_POST["submittingForm"])) {
/// CHECK FIRST NAME ERRORS
if ($firstName === NULL) {
// firstName is empty
$firstNameError = "First name is required!";
$error = true;
} else {
// check characters
if (!preg_match("/^[a-zA-Z ]*$/", $firstName)) {
$firstNameError = "Only letters and white spaces allowed!";
$error = true;
}
}
/// CHECK LAST NAME ERRORS
if (!preg_match("/^[a-zA-Z ]*$/", $lastName)) {
// check characters
$lastNameError = "Only letters and white spaces allowed!";
$error = true;
}
// if no error then redirect
if (!$error) {
$_SESSION['fistName'] = $firstName;
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
} else {
// user did not submit form!
}
// clean input
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="wrapper">
<h1>Welcome to Chollerton Tearoom! </h1>
<form id="userdetail" method="POST">
<fieldset id="aboutyou">
<legend id="legendauto">user information</legend>
<p>
<label for="firstName">First Name: </label>
<input type="text" name="firstName" id="firstName" value="<?php echo $firstName; ?>">
<span class="error">* <?php echo $firstNameError;?></span>
<label for="lastName">Last Name: </label>
<input type="text" name="lastName" id="lastName" value="<?php echo $lastName; ?>">
<span class="error">* <?php echo $lastNameError;?></span>
</p>
<p>
<input type="submit" name="submittingForm" value="submit">
</p>
</fieldset>
</form>
</div>
</body>
</html>
and so here is my testing2.php which get the data after submited...
<?php
session_start();
$firstName = $_SESSION['firstName'];
$lastName = $_SESSION['lastName'];
echo "<h1>Successfull submission :</h1>";
echo "<p>fitstName : $firstName <p/>";
echo "<p>lastName : $lastName <p/>";
?>
when the form is submited , Notice: Undefined index: firstName in F:\xampp\htdocs\en407b\assigment\testing2.php on line 5 is appear...
i've tried to fix it but still cnt get it right...
pls help me....
change like this
if (!$error) {
$_SESSION['firstName'] = $firstName;//error here
$_SESSION['lastName'] = $lastName;
header('Location: testing2.php');
exit();
}
You have a simple typo -
if (!$error) {
$_SESSION['fistName'] = $firstName;
should be:
if (!$error) {
$_SESSION['firstName'] = $firstName;
I'd also recommend giving your form an action:
<form id="userdetail" action="testing2.php" method="POST">
and then move all the form processing logic over into that file. That would eliminate the need for a header redirect.
You have typo in your session name. You are assigning session to $_SESSION['fistName'] and trying to access it with $_SESSION['firstName'];
I would like to validate the information before its send it to me. For instance that an email address has an # on it.
I have the following code to introduce: Name, LastName and email. I validated it that they are not empty, but:
How do I send a message to the user to let them know that they need to fill it up? I tried: if ($nameErr == ''){echo "Need to introduce a name"}
but it doens't work
How do I make validation of type: making sure that email address has an # or that a telephone is numeric and has 9 digits?
Thank you so much
<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// define variables and set to empty values
$nameErr = $emailErr = $surnameErr = "";
$name = $email = $surname = "";
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["surname"])) {
$surname = "";
} else {
$surname = test_input($_POST["surname"]);
}
}
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 $nameErr;?></span>
<br><br>
Last Name: <input type="text" name="surname">
<span class="error">*<?php echo $surnameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
you don't actually need to code all validation yourself. It is more convenient if you use a library like http://respect.github.io/Validation/ this.