PDA

View Full Version : Form validation



pkrishna42
09-23-2017, 06:38 PM
how to send form to next page after validation through header('location:sucess.php');
please suggest me
below find my code



<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
// define variables and set to empty values
$nameErr = "";
$name = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select state.</p>';
} }

if(isset($_POST['city'])) {
if($_POST['city'] == 'NULL') {
echo '<p>Please select city.</p>';
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="Member" <?php if(isset($_POST['state']) && $_POST['state']=="Member") { ?>selected<?php } ?>>Member</option>
<option value="New" <?php if(isset($_POST['state']) && $_POST['state']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['state']) && $_POST['state']=="one") { ?>selected<?php } ?>>one</option>

</select>

<select name="city">

<option value="NULL">-- Please select a state --</option>

<option value="Member" <?php if(isset($_POST['city']) && $_POST['city']=="Member") { ?>selected<?php } ?>>Member</option>
<option value="New" <?php if(isset($_POST['city']) && $_POST['city']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['city']) && $_POST['city']=="one") { ?>selected<?php } ?>>one</option>


</select>
<input type="text" name="phone">

<input type="tel" name="name" id="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>"> <?php echo $nameErr;?>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>

jscheuer1
09-24-2017, 02:11 AM
I don't see "header" anywhere in your code. So obviously it's not going to:


header('location:sucess.php');

if that's not in the code.

One thing I can tell you is that you need to send headers before you write to the page, so do all your calculation/evaluation that may result in:


header('location:sucess.php');

before the opening <!doctype html> tag. Do them also before any echo statements, etc., before anything that would cause the server to send text or formatting data to the browser.

jscheuer1
09-24-2017, 02:25 AM
Something like:


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" and !empty($_POST["name"]) and isset($_POST['state']) and ($_POST['state'] !== 'NULL') and isset($_POST['city']) and $_POST['city'] !== 'NULL'){
header('location:sucess.php');
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
// define variables and set to empty values
$nameErr = "";
$name = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
}
if(isset($_POST['state'])) {
if($_POST['state'] == 'NULL') {
echo '<p>Please select state.</p>';
} }

if(isset($_POST['city'])) {
if($_POST['city'] == 'NULL') {
echo '<p>Please select city.</p>';
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Please select a state</legend>
<select name="state">
<option value="NULL">-- Please select a state --</option>
<option value="Member" <?php if(isset($_POST['state']) && $_POST['state']=="Member") { ?>selected<?php } ?>>Member</option>
<option value="New" <?php if(isset($_POST['state']) && $_POST['state']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['state']) && $_POST['state']=="one") { ?>selected<?php } ?>>one</option>

</select>

<select name="city">

<option value="NULL">-- Please select a state --</option>

<option value="Member" <?php if(isset($_POST['city']) && $_POST['city']=="Member") { ?>selected<?php } ?>>Member</option>
<option value="New" <?php if(isset($_POST['city']) && $_POST['city']=="New") { ?>selected<?php } ?>>New</option>
<option value="one" <?php if(isset($_POST['city']) && $_POST['city']=="one") { ?>selected<?php } ?>>one</option>


</select>
<input type="text" name="phone">

<input type="tel" name="name" id="name" value="<?php echo isset($_POST["name"]) ? $_POST["name"] : ''; ?>"> <?php echo $nameErr;?>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>

But that won't send any data anywhere. What exactly do you want to have happen?

You probably want the action of the form to be sucess.php but have it send the user back to the form page if the information is incomplete.

There are advanced methods whereby you can send the post data along, or you could send it relatively easily as get data. But generally what is done is to use javascript to validate the form, if it passes, send the data along in the normal fashion to the action page/php script.

pkrishna42
09-24-2017, 09:02 AM
hey thanks for your help its working great..........thanks a lot