Sessions Struggles - PHP - php

For my application, there are three levels of users:
top level (00)
mid "district" level
lower level
The interface built allows users to create messages that will be distributed to a mobile app.
I had it working fine, but was then later tasked to add the mid-level. Now, even though the messages appear to update properly, I am encountering an issue that, instead of displaying "Message Updated" and the form after a message is submitted, I am receiving the "You do not have permission to access this page" message.
This does NOT occur with the mid/district level, only the lower and upper levels. Some reason, for these two, it is not properly reading $_SESSION['store'] after the form is submitted (though it works as expected when the page is loaded normally, not via POST).
I would greatly appreciate any guidance:
<?php
session_start();
function format($input) {
$input = trim($input);
$input = stripcslashes($input);
$input = htmlspecialchars($input);
return $input;
}
$con = new PDO("sqlite:managers.db");
$store = $_SESSION['store'];
$stores;
$file;
$district;
$file = "messages/" . $store . ".txt";
if(!file_exists($file)) {
$temp = fopen($file, "w"); // create file
fclose($temp);
}
if(strpos("d", $store) == 0) {
$district = true;
$sql = "SELECT district FROM managers WHERE store = '$store'";
$statement = $con->query($sql);
$row = $statement->fetch();
$storesArray = explode(",", $row[0]);
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$newMessage = format($_POST['message']);
$writer = fopen($file, "w");
fwrite($writer, $newMessage);
fclose($writer);
if($district) {
foreach($storesArray as $store) {
$fileName = "messages/d" . $store . ".txt";
if(!file_exists($fileName)) {
$temp = fopen($fileName, "w"); // create file
fclose($temp);
}
$writer = fopen($fileName, "w");
fwrite($writer, $newMessage);
fclose($writer);
}
}
}
$handler = fopen($file, "r");
$currentMessage = fread($handler, filesize($file));
fclose($handler);
?>
// some code omitted //
<?php
if($store == "" || $store == null) {
echo "<p>You do not have permission to view this page</p>";
} else {
echo "<h2>Manage Messages"; if($store == "00") {
echo "<a href='admin.php'><input type='button' id='adminBack' value='Back' /></a></h2>";
} else {
echo "<a href='adminUI.php'><input type='button' id='adminBack' value='Back' /></a></h2>";
}
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<h2>Message Updated!</h2>";
}
echo "<form class='admin' class='col-md-6' method='post' action='manageMessages.php'>
<div class='form-group'>
<label for='message'> Message: </label>
<textarea class='form-control' id='message' name='message' >$currentMessage</textarea>
<input type='submit' value='Post Message' />
</div>
</form>";
}
?>
</div>
<!-- end page specific content -->
The login page that sets the session:
<?php
session_start();
function format($input) {
$input = trim($input);
$input = stripslashes($input);
$input = htmlspecialchars($input);
return $input;
};
$store; $pass; $valid;
echo "<script>function redirect() {
location.assign('manageMessages.php');
}
function adminRedirect() {
location.assign('admin.php');
}</script>";
if($_GET['logout']) {
session_unset();
session_destroy();
}
if($_SERVER['REQUEST_METHOD'] == "POST") {
if(!empty($_POST['store']) && !empty($_POST['pass'])) {
$store = format($_POST['store']);
$pass = format($_POST['pass']);
$con = new PDO("sqlite:managers.db");
$sql = "SELECT *FROM managers WHERE store = '$store' AND password = '$pass'";
$statement = $con->query($sql);
$rows = $statement->fetchAll();
$count = count($rows);
if($count != 1) {
$valid = false;
} else {
$valid = true;
}
}
else {
$valid = false;
}
}
?>
// excess code //
<?php
$location;
if($valid) {
$_SESSION['store'] = $store;
if($store == "00") {
echo "<script>setTimeout(adminRedirect(), 1);</script>";
} else {
echo "<script>setTimeout(redirect(), 1);</script>";
} } elseif ($valid === false) {
echo "<h3>Please enter a valid store/password combination!</h3>";
}
?>
<h2>Admin Login</h2>
<form class="admin" method="post" action="adminUI.php">
<div class="form-group">
<label for="store">Store Number: </label>
<input type="text" class="form-control" name="store" id="store" />
<label for="pass">Password:</label>
<input type="text" class="form-control" name="pass" id="pass" />
<input type="submit" value="Login" />
</div>
</form>

Your $store variable is being overwritten by your foreach:
foreach($storesArray as $store)
You must use a different name for that foreach, something like:
foreach($storesArray as $store2)

Related

How do I make my fake login page display "Access Granted" with no other content after a successful login?

I have an assignment that needs me to create a simple login page that asks for a username and password. Once entered, it checks a text file and if the username and password match the ones on file, it's supposed to display the words "Access Granted" with no other content on the page.
How do I make it so my form shows up normally on load, and then when a unsuccessful login attempt is made, it displays "Access Denied" on the same page, but when a successful login attempt is made, "Access Granted" is displayed along with no other content?
Here is my code:
<?php
$fs = fopen('includes/users.txt', 'r');
$contents = fread($fs, filesize('includes/users.txt'));
$words = explode('||>><<||', $contents);
$msg = "";
if(isset($_POST['Submit']))
{
foreach($words as $word)
{
$names = explode(",", $word);
for($x = 0; $x < sizeof($names); $x++)
{
if($x == 0)
{
$username = $names[$x];
}
else
{
$password = $names[$x];
}
}
if($_POST['user'] == $username && $_POST['pass'] == $password)
{
$msg = "<p>Access Granted!</p>";
break;
}
else
{
$msg = "<p>Access Denied!</p>";
break;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Insecure</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div>
<form method="post">
<input placeholder="Username" type="text" name="user"><br>
<input placeholder="Password" type="password" name="pass"><br><br>
<input type="submit" value="Log In" name="Submit">
<input type="reset">
</form><br><br>
</div>
<?php echo $msg; ?>
</body>
</html>
Echo the $msg variable immediately it is declared in your if and else statements, rather than after your form.
To ensure nothing else is printed to the screen after $msg is echoed, a crude way to do this will be with “die()”.
This way, the form won’t show up because the script has been killed after echoing $msg in either the if or else statement.
<?php
$fs = fopen('includes/users.txt', 'r');
$contents = fread($fs, filesize('includes/users.txt'));
$words = explode('||>><<||', $contents);
$msg = "";
if(isset($_POST['Submit']))
{
foreach($words as $word)
{
$names = explode(",", $word);
for($x = 0; $x < sizeof($names); $x++)
{
if($x == 0)
{
$username = $names[$x];
}
else
{
$password = $names[$x];
}
}
if($_POST['user'] == $username && $_POST['pass'] == $password)
{
$msg = "<p>Access Granted!</p>";
break;
}
else
{
$msg = "<p>Access Denied!</p>";
break;
}
//Print the $msg variable and kill the script
die($msg);
}
}
?>
Your HTML becomes:
<form method="post">
<input placeholder="Username" type="text" name="user"><br>
<input placeholder="Password" type="password" name="pass"><br><br>
<input type="submit" value="Log In" name="Submit">
<input type="reset">
</form>
You can use Die(""); or Exit("")
Like this :
Die("Access granted");
The die kills the whole page and you can set a Message to it !
For better Risults :
die("<div class='YOUR CLASSES'>Your message</div>");
Exit is really die.
exit("MESSAGE");
exit("<div class='YOUR CLASSES'>Your message</div>");

php login form display errors from array

I'm having some trouble displaying my errors on this login form.
The login works but I can't figure out how to display those errors.
I just need to display them between the login field and the footer. I suppose the problem should be the last part of the foreach that should go true the error array.
<!DOCTYPE html>
<html lang="en">
<body>
<?php
include ('includes/header.php');
?>
<div class="nav">
<?php
include ('includes/menu.php');
$error= logInData();
?>
</div>
<section role="main">
<div class="logIn">
<h3>Intranet Login</h3>
</div>
<form action="" method="post">
<fieldset>
<legend>Student Log in</legend>
<div>
<label for="username">Enter username: </label>
<input type='text' id="userN" name="userN" value = "<?php if (isset($error['usern'])){echo $error['usern'];} ?>">
</div>
<div>
<label for="password">Enter password: </label>
<input type='password' id="pass" name="pass" value = "">
</div>
<div>
<p class="red"><?php if (isset($error['both'])) {
echo $error['both'];
} ?></p>
</div>
<div>
<input type="submit" name="submit" value="Log-In">
</div>
</fieldset>
</form>
</section>
<?php
function logInData (){
$error = array();
$validated = array();
$clean = array();
$pass = false;
if (isset($_POST['submit']) && $pass == true) {
$inputPass = ($_POST['pass']);
$trimPass = trim($inputPass);
$inputUsern = ($_POST['userN']);
$trimUsern = trim($inputUsern);
if(!empty($trimPass)){
if (!ctype_alpha($trimPass)) {
$error['passw'] = 'No special characters allowed on password';
$pass = false;
}else{
if(empty($trimPass)){
$error['passw'] = 'password field empty';
$pass = false;
}else{
$clean['passw'] = $trimUsern;
$pass = true;
}
}
}if ($pass == true) {
return $clean;
}else {
return $error;
}
if(!empty($trimUsern)){
if (!ctype_alpha($trimUsern)) {
$error['userN'] = 'No special characters allowed on username';
$pass = false;
}else{
if(empty($trimPass)){
$error['userN'] = 'username field empty';
$pass = false;
}else{
$clean['userN'] = $trimUsern;
$pass = true;
}
}
}if ($pass == true) {
return $clean;
}else {
return $error;
}
$dir = '/home/sbau01/public_www/php/fma/data';
if (is_dir($dir)){
$handleDir = opendir('/home/sbau01/public_www/php/fma/data');
$path = "/home/sbau01/public_www/php/fma/data/data.txt";
if(is_file($path)){
$handle = fopen($path, 'r');
while(!feof($handle)){
$dataRow = fgets($handle);
if(!empty($dataRow)){
$separate = explode(' ',$dataRow);
$storedUsern = trim($separate[3]);
$storedPassword = trim($separate[4]);;
if (($clean['userN'] == $storedUsern) && ($clean['passw'] && $storedPassword)){
$match = true;
header('location: intranet.php');
}else{
$error['match']='<span >Username/Password is incorrect!!</span>';
$pass = false;
}
}
}fclose($handle);
}else{
$error['data']='<span >Data not found</span>';
$pass = false;
}closedir($HandleDir);
}else{
$error['data']='<span >Data not found</span>';
$pass = false;
}
}else {
$errmsg = '';
foreach($error as $key => $value){
echo "ERROR: $value<br />\n";
}
}
}
?>
<footer>
<?php include ('includes/footer.php');?>
</footer>
</body>
</html>
Its a simple brackets error:
$errmsg = '';
foreach($error as $key => $value){
echo "ERROR: $value<br />\n";
}
The part above is in the else condition of if (isset($_POST['submit']) && $pass == true) {
Thats why this will never execute. Simply remove the bracket above this part and add it after the foreach.
Saving Passwords in text files is NOT a great idea!
In line 101 you have probably an little mistake:
You just check if there are the variables, you dont check if they are equal ($clean['passw'] && $storedPassword)){
A couple of issues identified.
Do you have display errors turned on? https://stackoverflow.com/a/21429652/1246494
You are calling $error= logInData(); at the top, but have your function logInData() { ... } created down below.
I think what you want to do it put the whole function in an include file at the top like:
include ('includes/header.php');
include ('includes/logInFunction.php');
You then want to call logInData(); down in the body.
Another issue is your function puts data in an array and echos data. If you are going to have $error= logInData(); at the top of your page try moving this out of your function and into your body where you want to output the errors.
if(count($error) > 0)
{
foreach($error as $key => $value)
{
echo "ERROR: $value<br />\n";
}
}

PHP Display post variables

I'm looking for a bit of help and a bit of an explanation here.
I have created an HTML form with several input fields and some very basic PHP validation when POSTing the inputs. My validation just checks to see if the field has data and if not, it prompts the user to enter data in the field by displaying an error. My hope is ultimately to POST these inputs, check them against a database, and if they aren't there, then add them to the database. But, this is not my issue at hand.
Currently, my objective is to take all of the inputs in my field that I want to POST and to display them in a field below my error display area. I had hoped to just echo the data but for some reason, not all of the entered data appears.
Of the 5 entry fields in the code below, 4 are basic input fields and one is a text area. If i enter anything into the basic input fields, only the last input will echo in my display area. If i enter something into field 1 and leave the rest blank, field 1 will display. Also, if I enter something into the text area, it will always display. Finally, my PHP validation does not appear to work with my textarea type input (labeled 'note') and will not return an error if the 'note' input is left blank.Can anyone explain: (1) How do I fix it so that all 5 inputs display in the display div?; (2) Why does this happen?; (3) why is no error returned if the text area (labeled 'note') is left blank?
Thank you.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
//form validation for general entry form
// define variables and set to empty values
$clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
$client = $matter = $date = $time = $note = "";
//on post, check to see if variable is empty. if not empty
//parse it and assign value back to variable name
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$client = test_input($_POST["matter"]);
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$client = test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$client = test_input($_POST["time"]);
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label for="client">Client:</label>
<input type="text" placeholder = "Enter Client Name" name="client"> *
<label for="matter">Matter:</label>
<input type="text" placeholder = "Enter Matter Name" name="matter"> *
<label for="date">Date:</label>
<input type="text" placeholder = "Enter Date" name="date"> *
<label for="time">Time:</label>
<input type="text" placeholder ="Time to nearest tenth hour" name="time"> *
<label for="note">Note:</label>
<textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea>
<input type="submit" name="submit" value="Submit" class="submitbutton">
</form>
<div class="errorDiv">
<?php
echo $clientErr;
echo $matterErr;
echo $dateErr;
echo $timeErr;
?>
</div>
<div class ="displayDiv">
<?php
echo "<h2>Your Input:</h2>";
echo $client;
echo "<br>";
echo $matter;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $note;
?>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<?php
//form validation for general entry form
// define variables and set to empty values
$clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
$client = $matter = $date = $time = $note = "";
//on post, check to see if variable is empty. if not empty
//parse it and assign value back to variable name
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$matter = test_input($_POST["matter"]); //remove $client and assign value to $matter
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$date = test_input($_POST["date"]); //remove $client and assign value to $date
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$time = test_input($_POST["time"]); //remove $client and assign value to $time
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >
<label for="client">Client:</label>
<input type="text" placeholder = "Enter Client Name" name="client"> *
<label for="matter">Matter:</label>
<input type="text" placeholder = "Enter Matter Name" name="matter"> *
<label for="date">Date:</label>
<input type="text" placeholder = "Enter Date" name="date"> *
<label for="time">Time:</label>
<input type="text" placeholder ="Time to nearest tenth hour" name="time"> *
<label for="note">Note:</label>
<textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea>
<input type="submit" name="submit" value="Submit" class="submitbutton">
</form>
<div class="errorDiv">
<?php
echo $clientErr;
echo $matterErr;
echo $dateErr;
echo $timeErr;
echo $noteErr; // echo noteerror here.........
?>
</div>
<div class ="displayDiv">
<?php
echo "<h2>Your Input:</h2>";
echo $client;
echo "<br>";
echo $matter;
echo "<br>";
echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $note;
?>
</div>
</body>
</html>
Here you assign all values to single variable $client and echo all different variable so assign value to particular variable like $matter, $date, $time. And you forget to echo $noteerror in error messsages.
You copy and pasted your else block, didn't you? In each block you assign the value to $client and thereby overwrite it with each new $_POST value. Change the other assignments to $client to $time or appropriate and try that.
For example this
$client = test_input($_POST["date"]);
should probably be
$date = test_input($_POST["date"]);
Why are you assigning all the values to same $client variable? It has to be replaced with different variables.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$complete = true;
$postArray = [];
if (empty($_POST["client"])) {
$clientErr = "*A client name is required. ";
$complete = false;
}else {
$client = test_input($_POST["client"]);
//$postArray[] = $client;
}
if (empty($_POST["matter"])) {
$matterErr = "*A matter name is required. ";
$complete = false;
}else {
$matter= test_input($_POST["matter"]);
}
if (empty($_POST["date"])) {
$dateErr = "*A date is required. ";
$complete = false;
}else {
$date= test_input($_POST["date"]);
}
if (empty($_POST["time"])) {
$timeErr = "*A time entry is required. ";
$complete = false;
}else {
$time= test_input($_POST["time"]);
}
if (empty($_POST["note"])) {
$noteErr = "*A note is required. ";
$complete = false;
} else {
$note = test_input($_POST["note"]);
}

How to check line in file (php)

I'm new to php and wanted to make a simple php script to check a form of my html site.
To answer the questions:
I have a file, that's the Name of the User and I want to check if the password that is in there (line 1) is the same as the one in the "password" field on my site. And when it's like this it should open a site.
Maybe a check if the file exists would be nice :D
This is my php-file, it's named "check.php":
<?php
$f = fopen($_POST["name"], "r");
$theData = fgets($f);
if ($_POST["pw"] == $theData) {
$ch = curl_init("site.com");
curl_exec($ch);
}
fclose($f);
?>
This is my html-file:
<h2>Check</h2>
<form action="check.php" method='post'>
<b>Name: </b><input name="name" type="text" value="Name"> <br>
<b>Password: </b><input name="pw" type="text" value="Passwort"> <br>
<input type="submit" value="Check">
<input type="reset" value="Reset">
</form>
I hope one can help me ^^
I've tried a lot of things now, nothing really worked.
To process a form fields you should do like this in your check.php file(simplest one)
if(isset($_POST['submit']))
{
$name = $_PSOT['name'];
$password = $_POST['password'];
if($name == 'admin' && $password == 'admin')
{
header('Location:admin.php');exit;
}else{
echo 'Wrong user name or password';
}
}
may be you are asking to do like this
if(isset($_POST['submit']))
{
$name = $_PSOT['name'];
$password = $_POST['password'];
$file_type = '.txt';
$path = 'path to folder/'.$name.$file_type;
if(file_exists($path))
{
$user_pass = fopen($path, "r");
$flag = 0;
while(!feof($user_pass))
{
$p = fgets($user_pass);
if($password == $p)
{
$flag = 1;
}
}
fclose($user_pass);
if($flag == 1)
{
header('Location:to your page link/weblink');exit;
}else{
echo 'Wrong password';
}
}else{
echo 'User does not exists';
}
}

Strange validation error for form

The error i got was:
Notice: Undefined index: visible in C:\xampp\htdocs\introducingphp\includes\validation_function.php on line 22
It should not happen since i already instantiated all the variables including visible
Validation_function.php
<?php
$errors = array();
function fieldname_as_text($fieldname) {
$fieldname = str_replace("_", " ", $fieldname);
$fieldname = ucfirst($fieldname);
return $fieldname;
}
// * presence
// use trim() so empty spaces don't count
// use === to avoid false positives
// empty() would consider "0" to be empty
function has_presence($value) {
return isset($value) && $value !== "";
}
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
$value = trim($_POST[$field]);
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field) . " can't be blank";
}
}
}
// * string length
// max length
function has_max_length($value, $max) {
return strlen($value) <= $max;
}
function validate_max_lengths($fields_with_max_lengths) {
global $errors;
// Expects an assoc. array
foreach($fields_with_max_lengths as $field => $max) {
$value = trim($_POST[$field]);
if (!has_max_length($value, $max)) {
$errors[$field] = fieldname_as_text($field) . " is too long";
}
}
}
// * inclusion in a set
function has_inclusion_in($value, $set) {
return in_array($value, $set);
}
?>
new_page.php (the page that has the one-page submit form that does validation)
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/validation_function.php"); ?>
<?php find_selected_page(); ?>
<?php
// Can't add a new page unless there is a subject as a parent
if (!$current_subject) {
// subject ID was missing or invalid or
//subject couldn't be found in database
redirect_to("manage_content.php");
}
?>
<?php
if (isset($_POST['submit'])) {
// Process the form
//validations
$required_fields = array("menu_name", "position", "visible",
"content");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 60);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors)) {
// perform Create
//add the subject_id
$subject_id = $current_subject["id"];
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
//escape content
$content = mysql_prep($_POST["content"]);
// 2. Perform database query
$query .= "INSERT INTO pages (";
$query .= " subject_id, menu_name, position, visible,
content";
$query .= ") VALUES (";
$query .= " {$subject_id}, '{$menu_name}', {$position},
{$visible}, '{$content}'";
$query .= ")";
$result = mysqli_query($connection, $query);
if ($result ) {
// Success
$_SESSION["message"] = "Page Created.";
redirect_to("manage_content.php?subject=" .
urlencode($current_subject["id"]));
}else {
// Failure
$_SESSION["message"] = "Page creation failed.";
}
}
} else {
// This is probably a GET request
} // End: If(isset($_POST['submit']))
?>
<?php $layout_context = "admin"; ?>
<?php include("header.php"); ?>
<div id="main">
<div id="navigation">
<?php echo navigation($current_subject, $current_page); ?>
</div>
<div id="page">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
<h2>Create Page</h2>
<form action="new_page.php?subject=<?php echo
urlencode($current_subject["id"]); ?>" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="" />
</p>
<p>Position:
<select name="position">
<?php
$page_set =
find_all_pages_for_subject($current_subject["id"], false);
$page_count = mysqli_num_rows($page_set);
for($count=1; $count <= ($page_count + 1); $count++) {
echo "<option value=\"{$count}\">{$count}</option>";
}
?>
</select>
</p>
<p>Visible
<input type="radio" name="visible" value="0" /> NO
<input type="radio" name="visible" value="1" /> Yes
</p>
<p>Content:<br />
<textarea name="content" rows="20" cols="80"></textarea>
</p>
<input type="submit" name="submit" value="Create Page" />
</form>
<br />
<a href="manage_content.php?subject=<?php echo
urlencode($current_subject["id"]); ?>">Cancel</a>
</div>
</div>
<?php include("includes/footer.php"); ?>
You probably have a typo on the input HTML field. You can use:
if (isset($_POST[$field])) {
on validate_presences() function to be sure that the value exists.
When you try to do trim($_POST[$field]); you assume, the field exists in the $_POST array - for visible it does not in this case. You could move the trim to has_presence()
function has_presence($value) {
return isset($value) && trim($value) !== "";
}
function validate_presences($required_fields) {
global $errors;
foreach($required_fields as $field) {
if (!has_presence($value)) {
$errors[$field] = fieldname_as_text($field) . " can't be blank";
}
}
}
Now when you will only have the trim if the variable exists.
Okay, marking the radio check button makes it work now. Thanks for all your inputs guys. It has helped me a great deal.

Categories