pkrishna42
11-24-2017, 10:31 AM
Hi this is a simple form i have when i submit the form it will redirect to mail.php
and i want to restrict direct access to mail.php so i added some script here...
// <?php
session_start();
// kill the page if the access variable doesn't exists
// or if the access variable does exist but is not set to true
if(!isset($_SESSION['State']) || (isset($_SESSION['State']) && $_SESSION['State'] !== true))
{
header("Location: index.php");
// kill the page display error
}
?>...//
now the problem is my form is not getting submitted. Your help will be appreciated(i am beginer to php)
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
// some 'helper' functions -
// apply html htmlentities to a value
function _ent($val)
{
return htmlentities($val);
}
// return an element from an array
function _element($arr,$index)
{
return isset($arr[$index]) ? $arr[$index] : '';
}
$errors = []; // define an array to hold errors
$data = []; // define an array to hold a working copy of the data being operated on (if editing existing data, this is needed. for commonality, use if just submitting new data too.)
// define a list of option choices - both city/state are using the same list. if they were different lists, create two separate defining arrays.
$options = ['1','2','3'];
$options1 = ['4','5','6'];
// form processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$data = array_map('trim',$_POST); // get a trimmed copy of all 1st level (non-array) data
// validate all the inputs here
// state and city - select/option, consisting of Member, New, one.
// name - text field, consisting of letters and spaces only.
// phone - unknown if used, but using this recommend code layout, adding code for a field is straight forward.
if($data['state'] == '')
{
$errors['state'] = "State is required.";
}
else
{
if(!in_array($data['state'],$options))
{
$errors['state'] = "Invalid choice for State."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['city'] == '')
{
$errors['city'] = "City is required.";
}
else
{
if(!in_array($data['city'],$options1))
{
$errors['city'] = "Invalid choice for City."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['name'] == '')
{
$errors['name'] = "Name is required.";
}
else
{
if (!preg_match("/^[a-zA-Z ]*$/",$data['name']))
{
$errors['name'] = "Only letters and spaces are allowed in Name."; // allowing actual white space, which the original message stated uses a different value in the regex pattern
}
}
if($data['textarea1'] == '')
{
$errors['textarea1'] = "Message is required.";
}
// if no errors, use the submitted data
if(empty($errors))
{
header('location:mail.php'); // note: success was misspelled in the original code
die;
}
}
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST") { $name = $_POST["name"]; $_SESSION['name']=$name; }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
// if you want to display all the errors at once, before the start of the form, do that here...
if(!empty($errors))
{
echo implode('<br>',$errors); // add any html markup and css to display them the way you want
}
?>
<form method="post">
<fieldset>
<legend><?php echo $error; ?> </legend>
<select name="state">
<option value="">-- Please select a state --</option>
<?php
foreach($options as $choice)
{
$sel = isset($data['state']) && $data['state'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<select name="city">
<option value="">-- Please select a city --</option>
<?php
foreach($options1 as $choice)
{
$sel = isset($data['city']) && $data['city'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<?= _element($errors,'name'); ?> <input type="text" name="name" value="<?= _ent(_element($data,'name')); ?>">
<textarea class="textarea" id="textarea1" name="textarea1" type="text">
<?php if(isset($_POST['textarea1'])) {
echo htmlentities ($_POST['textarea1']); }?>
</textarea>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
my mail.php script ..............
<?php
session_start();
// kill the page if the access variable doesn't exists
// or if the access variable does exist but is not set to true
if(!isset($_SESSION['State']) || (isset($_SESSION['State']) && $_SESSION['State'] !== true))
{
header("Location: index.php");
// kill the page display error
}
?>
and i want to restrict direct access to mail.php so i added some script here...
// <?php
session_start();
// kill the page if the access variable doesn't exists
// or if the access variable does exist but is not set to true
if(!isset($_SESSION['State']) || (isset($_SESSION['State']) && $_SESSION['State'] !== true))
{
header("Location: index.php");
// kill the page display error
}
?>...//
now the problem is my form is not getting submitted. Your help will be appreciated(i am beginer to php)
<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php
// some 'helper' functions -
// apply html htmlentities to a value
function _ent($val)
{
return htmlentities($val);
}
// return an element from an array
function _element($arr,$index)
{
return isset($arr[$index]) ? $arr[$index] : '';
}
$errors = []; // define an array to hold errors
$data = []; // define an array to hold a working copy of the data being operated on (if editing existing data, this is needed. for commonality, use if just submitting new data too.)
// define a list of option choices - both city/state are using the same list. if they were different lists, create two separate defining arrays.
$options = ['1','2','3'];
$options1 = ['4','5','6'];
// form processing
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$data = array_map('trim',$_POST); // get a trimmed copy of all 1st level (non-array) data
// validate all the inputs here
// state and city - select/option, consisting of Member, New, one.
// name - text field, consisting of letters and spaces only.
// phone - unknown if used, but using this recommend code layout, adding code for a field is straight forward.
if($data['state'] == '')
{
$errors['state'] = "State is required.";
}
else
{
if(!in_array($data['state'],$options))
{
$errors['state'] = "Invalid choice for State."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['city'] == '')
{
$errors['city'] = "City is required.";
}
else
{
if(!in_array($data['city'],$options1))
{
$errors['city'] = "Invalid choice for City."; // if you see this error, either there is a mistake in the coding or someone is submitting their own values.
}
}
if($data['name'] == '')
{
$errors['name'] = "Name is required.";
}
else
{
if (!preg_match("/^[a-zA-Z ]*$/",$data['name']))
{
$errors['name'] = "Only letters and spaces are allowed in Name."; // allowing actual white space, which the original message stated uses a different value in the regex pattern
}
}
if($data['textarea1'] == '')
{
$errors['textarea1'] = "Message is required.";
}
// if no errors, use the submitted data
if(empty($errors))
{
header('location:mail.php'); // note: success was misspelled in the original code
die;
}
}
?>
<?php
if($_SERVER["REQUEST_METHOD"]=="POST") { $name = $_POST["name"]; $_SESSION['name']=$name; }
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
// if you want to display all the errors at once, before the start of the form, do that here...
if(!empty($errors))
{
echo implode('<br>',$errors); // add any html markup and css to display them the way you want
}
?>
<form method="post">
<fieldset>
<legend><?php echo $error; ?> </legend>
<select name="state">
<option value="">-- Please select a state --</option>
<?php
foreach($options as $choice)
{
$sel = isset($data['state']) && $data['state'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<select name="city">
<option value="">-- Please select a city --</option>
<?php
foreach($options1 as $choice)
{
$sel = isset($data['city']) && $data['city'] == $choice ? ' selected' : '';
echo "<option value='$choice'$sel>$choice</option>\n";
}
?>
</select>
<?= _element($errors,'name'); ?> <input type="text" name="name" value="<?= _ent(_element($data,'name')); ?>">
<textarea class="textarea" id="textarea1" name="textarea1" type="text">
<?php if(isset($_POST['textarea1'])) {
echo htmlentities ($_POST['textarea1']); }?>
</textarea>
<input type="submit" name="submit" value="submit">
</fieldset>
</form>
</body>
</html>
my mail.php script ..............
<?php
session_start();
// kill the page if the access variable doesn't exists
// or if the access variable does exist but is not set to true
if(!isset($_SESSION['State']) || (isset($_SESSION['State']) && $_SESSION['State'] !== true))
{
header("Location: index.php");
// kill the page display error
}
?>