Here is the html and Php code i have used:
<?php
require_once('include_function.php');
require_once('validation_functions.php');
$errors = array();
$message ="";
if(isset($_POST['submit'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (array_key_exists('submit', $_POST)){
}
$fields_required = array("username","password");
foreach($fields_required as $field){
$value = trim($_POST[$field]);
if(!has_presence($value)){
$errors[$field]= ucfirst($field). " Cant Be blank";
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Single page Submission</title>
</head>
<body>
<?php echo $message;?>
<?php echo form_error($errors)?>
<form action="form_with_validation.php" method="post">
Username<input type="text" name="username" value=""/><br/>
Password<input type="password" value="" name="password" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
And these are functions I have used
function has_presence($value){
return isset($value) && $value !=="";
}
function form_error($errors=array()){
$output = "";
if(!empty($errors)){
$output .="<div class=\"error\">";
$output .="Please fix the following errors";
$output .="<ul>";
foreach($errors as $key => $field){
$output .= "<li>{$field}</li>";
}
$output .="</ul>";
$output .="</div>";
}
return $output;
}
Can someone please guide me to validate the form using if array key exists
So that I get error message next to Or below the input field
or any other method which does the needful
<?php
$errors = array();
$message ="";
if(isset($_POST['submited'])){
$fields_required = array("username","password");
foreach($fields_required as $field)
if (!isset($_POST[$field]) || $_POST[$field]=="")
$errors[$field]= $field. " Cant Be blank";
}
function form_error($errors=array()){
$output = "";
if(!empty($errors)) {
$output .="<div class=\"error\">";
$output .="Please fix the following errors";
$output .="<ul>";
foreach($errors as $key => $field){
$output .= "<li>{$field}</li>";
}
$output .="</ul>";
$output .="</div>";
}
return $output;
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Single page Submission</title>
</head>
<body>
<?php echo $message;?>
<form action="form_with_validation.php" method="post">
<input type="hidden" name="submited" value="1" />
Username<input type="text" name="username" value=""/><br/>
Password<input type="password" value="" name="password" /><br/>
<input type="submit" name="submit" value="Submit" />
</form>
<?php echo form_error($errors)?>
</body>
</html>
#sandeep S K Can you please try once below code? :
Note: Please include your required DB connection or validation files on the top of the script.
<?php
function has_presence($value){
return isset($value) && $value !=="";
}
function form_error($errors=array()){
$output = "";
if(!empty($errors)){
$output .="<div class=\"error\">";
$output .="Please fix the following errors";
$output .="<ul>";
foreach($errors as $key => $field){
$output .= "<li>{$field}</li>";
}
$output .="</ul>";
$output .="</div>";
}
return $output;
}
$errors = array();
$message ="";
if(isset($_POST['submit'])){
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if (array_key_exists('submit', $_POST)){
}
$fields_required = array("username","password");
foreach($fields_required as $field){
$value = trim($_POST[$field]);
if(!has_presence($value)){
$errors[$field]= ucfirst($field). " Cant Be blank";
}
}
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Single page Submission</title>
</head>
<body>
<?php echo $message;?>
<?php if(!empty($errors)) { ?>
<div class="has_errors">
<ul>
<li>
There are errors in your input. Please fix them and try again.
</li>
</ul>
</div>
<?php
}
?>
<form action="index.php" method="post">
Username<input type="text" name="username" value=""/>
<?php
if (array_key_exists('username', $errors)) {
?>
<div class="has_errors">
<ul>
<li>
<?php print $errors['username'];?>
</li>
</ul>
</div>
<?php
}
?>
<br/>
Password<input type="password" value="" name="password" />
<?php
if (array_key_exists('password', $errors)) { ?>
<div class="has_errors">
<ul>
<li>
<?php print $errors['password'];?>
</li>
</ul>
</div>
<?php
}
?>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Related
I have the assignment below and I'm stuck at Step # 3 with the code file named inputforassignment2.php --basically, I am trying to append (add) rows to the existing data (songs.csv file) via that file with input fields. I tried to fix that code (which i obtained from a website, see sample source code far below, but it's returning errors or creates blank and numerical data in rows for each input.
Assignment: Create a simple PHP page that reads/writes to and from a .CSV file
The .CSV file should contain a list of your favorite items (for
example: songs, games, books, authors, etc,..). Each record in your
file should contain at least 3 attributes for your favorite item.
Also, it should have at least 7 records. --this part is done
The PHP should read the file and display the records in a TABLE with
the corresponding headers for each attribute of your favorite item.
--this part is done
Also, in the page should be a link (<< I did that part) that takes to another page where a new record can be added to the file. Then the list should display all previous records plus the new one. (<< where I am stuck)
-all my current source files:
song.csv
Song Title,Artist,Track Year
FLY,Sik-K,2017
Doverstreet,RIN,2017
Half Moon,Dean,2016
Blacklist,Loopy,2017
N/A,JooYoung,2018
Heyahe,ONE,2017
ADY,Sik-K,2017
assignment2.php how this php code displays song.csv
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Assignment 2</title>
</head>
<?php
echo "<table border=1> ";
$f = fopen("song.csv", "r"); //open a file in read mode
while (($line = fgetcsv($f)) !== false) { //read the each line of csv file
echo "<tr>"; //for printing in the table
foreach ($line as $cell) { //each data of line
echo "<td>" . htmlspecialchars($cell) . "</td>"; //print in the table
}
echo "</tr> ";
}
fclose($f); //close file
echo " </table>";
?>
Click here to add more songs!
<body>
</body>
</html>
inputformassignment2.php
<?php
//index.php
$error = '';
$name = '';
$email = '';
$subject = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
}
if(empty($_POST["subject"]))
{
$error .= '<p><label class="text-danger">Subject is required</label></p>';
}
else
{
$subject = clean_text($_POST["subject"]);
}
if($error == '')
{
$file_open = fopen("contact_data.csv", "a");
$no_rows = count(file("contact_data.csv"));
if($no_rows > 1)
{
$no_rows = ($no_rows - 1) + 1;
}
$form_data = array(
'sr_no' => $no_rows,
'name' => $name,
'email' => $email,
'subject' => $subject,
);
fputcsv($file_open, $form_data);
$error = '<label class="text-success">Thank you for contacting us</label>';
$name = '';
$email = '';
$subject = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add A New Song</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">Add A New Song</h2>
<br />
<div class="col-md-6" style="margin:0 auto; float:none;">
<form method="post">
<h3 align="center">Type below:</h3>
<br />
<?php echo $error; ?>
<div class="form-group">
<label>Song Title</label>
<input type="text" name="name" placeholder="Type your song title" class="form-control" value="<?php echo $name; ?>" />
</div>
<div class="form-group">
<label>Song Artist</label>
<input type="text" name="email" class="form-control" placeholder="Type the song artist here" value="<?php echo $email; ?>" />
</div>
<div class="form-group">
<label>Track Year</label>
<input type="text" name="subject" class="form-control" placeholder="Put the song's track year here" value="<?php echo $subject; ?>" />
</div>
<div class="form-group" align="center">
<input type="submit" name="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
sample source code obtained from website:
<?php
//index.php
$error = '';
$name = '';
$email = '';
$subject = '';
$message = '';
function clean_text($string)
{
$string = trim($string);
$string = stripslashes($string);
$string = htmlspecialchars($string);
return $string;
}
if(isset($_POST["submit"]))
{
if(empty($_POST["name"]))
{
$error .= '<p><label class="text-danger">Please Enter your Name</label></p>';
}
else
{
$name = clean_text($_POST["name"]);
if(!preg_match("/^[a-zA-Z ]*$/",$name))
{
$error .= '<p><label class="text-danger">Only letters and white space allowed</label></p>';
}
}
if(empty($_POST["email"]))
{
$error .= '<p><label class="text-danger">Please Enter your Email</label></p>';
}
else
{
$email = clean_text($_POST["email"]);
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error .= '<p><label class="text-danger">Invalid email format</label></p>';
}
}
if(empty($_POST["subject"]))
{
$error .= '<p><label class="text-danger">Subject is required</label></p>';
}
else
{
$subject = clean_text($_POST["subject"]);
}
if(empty($_POST["message"]))
{
$error .= '<p><label class="text-danger">Message is required</label></p>';
}
else
{
$message = clean_text($_POST["message"]);
}
if($error == '')
{
$file_open = fopen("contact_data.csv", "a");
$no_rows = count(file("contact_data.csv"));
if($no_rows > 1)
{
$no_rows = ($no_rows - 1) + 1;
}
$form_data = array(
'sr_no' => $no_rows,
'name' => $name,
'email' => $email,
'subject' => $subject,
'message' => $message
);
fputcsv($file_open, $form_data);
$error = '<label class="text-success">Thank you for contacting us</label>';
$name = '';
$email = '';
$subject = '';
$message = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>How to Store Form data in CSV File using PHP</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<br />
<div class="container">
<h2 align="center">How to Store Form data in CSV File using PHP</h2>
<br />
<div class="col-md-6" style="margin:0 auto; float:none;">
<form method="post">
<h3 align="center">Contact Form</h3>
<br />
<?php echo $error; ?>
<div class="form-group">
<label>Enter Name</label>
<input type="text" name="name" placeholder="Enter Name" class="form-control" value="<?php echo $name; ?>" />
</div>
<div class="form-group">
<label>Enter Email</label>
<input type="text" name="email" class="form-control" placeholder="Enter Email" value="<?php echo $email; ?>" />
</div>
<div class="form-group">
<label>Enter Subject</label>
<input type="text" name="subject" class="form-control" placeholder="Enter Subject" value="<?php echo $subject; ?>" />
</div>
<div class="form-group">
<label>Enter Message</label>
<textarea name="message" class="form-control" placeholder="Enter Message"><?php echo $message; ?></textarea>
</div>
<div class="form-group" align="center">
<input type="submit" name="submit" class="btn btn-info" value="Submit" />
</div>
</form>
</div>
</div>
</body>
</html>
Am trying to add custom validation messages to this form, but cannot figure it out for the life of me..
Any help/advice is appreciated...thank you.
I have tried a few tutorials, but nothing seems to be clicking for me, I am sure its something simple, but I just cant seem to figure it out.
Thanks
FORM FOR THE VALIDATION:
<?php
if (isset($_POST['insert'])) {
require_once('connection.php');
$OK = false;
$sql = 'INSERT INTO students (studentTitle, studentFirstName, studentLastName)
VALUES(:studentTitle, :studentFirstName, :studentLastName)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':studentTitle', $_POST['studentTitle'], PDO::PARAM_STR);
$stmt->bindParam(':studentFirstName', $_POST['studentFirstName'], PDO::PARAM_STR);
$stmt->bindParam(':studentLastName', $_POST['studentLastName'], PDO::PARAM_STR);
$stmt->execute();
$OK = $stmt->rowCount();
if ($OK) {
header('Location: http://localhost/mysqlquiz/student.php');
exit;
} else {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add Student Details</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 class="header">New student details</h1>
<p>Student Listing </p>
<?php
if (isset($error)) {
echo "<p class='warning'>Error: $error</p>";
}
?>
<?php
// define variables and set to empty values
$studentTitleErr = $studentFirstNameErr = $studentLastNameErr = "";
$studentTitle = $studentFirstName = $studentLastName = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["studentTitle"])) {
$studentTitleErr = "A title is required";
} else {
$studentTitle = test_input($_POST["studentTitle"]);
}
if (empty($_POST["studentFirstName"])) {
$studentFirstNameErr = "First name is required";
} else {
$studentFirstName = test_input($_POST["studentFirstName"]);
}
if (empty($_POST["studentLastName"])) {
$studentLastNameErr = "Last name is required";
} else {
$studentLastName = test_input($_POST["studentLastName"]);
}
}
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"]); ?>">
<p>
<label for="studentTitle">Title:</label>
<select name="studentTitle" id="studentTitle" ><span class="error">* <?php echo $studentTitleErr;?></span>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Miss">Miss.</option>
</select>
</p>
<p>
<label for="studentFirstName">First Name:</label>
<input type="text" name="studentFirstName" id="studentFirstName" ><span class="error">* <?php echo $studentFirstNameErr;?></span>
</p>
<p>
<label for="studentLastName">Last Name:</label>
<input type="text" name="studentLastName" id="studentLastName" ><span class="error">* <?php echo $studentLastNameErr;?></span>
</p>
<p>
<input type="submit" name="insert" value="Add Details" id="insert">
<input type="reset" name="clear" value="Clear" id="clear">
<input name="studentID" type="hidden" value="<?php echo $studentID; ?>">
</p>
</form>
</body>
</html>
I have updated your code. you should check the validation first then insert.
<?php
// define variables and set to empty values
$studentTitleErr = $studentFirstNameErr = $studentLastNameErr = "";
$studentTitle = $studentFirstName = $studentLastName = "";
if (isset($_POST['insert'])) {
require_once('connection.php');
$error = $OK = false;
if (empty($_POST["studentTitle"])) {
$studentTitleErr = "A title is required";
$error = true;
} else {
$studentTitle = test_input($_POST["studentTitle"]);
}
if (empty($_POST["studentFirstName"])) {
$studentFirstNameErr = "First name is required";
$error = true;
} else {
$studentFirstName = test_input($_POST["studentFirstName"]);
}
if (empty($_POST["studentLastName"])) {
$studentLastNameErr = "Last name is required";
$error = true;
} else {
$studentLastName = test_input($_POST["studentLastName"]);
}
if($error == false)
{
$sql = 'INSERT INTO students (studentTitle, studentFirstName, studentLastName)
VALUES(:studentTitle, :studentFirstName, :studentLastName)';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':studentTitle', $_POST['studentTitle'], PDO::PARAM_STR);
$stmt->bindParam(':studentFirstName', $_POST['studentFirstName'], PDO::PARAM_STR);
$stmt->bindParam(':studentLastName', $_POST['studentLastName'], PDO::PARAM_STR);
$stmt->execute();
$OK = $stmt->rowCount();
if ($OK) {
header('Location: http://localhost/mysqlquiz/student.php');
exit;
} else {
$error = $stmt->errorInfo();
if (isset($error[2])) {
$error = $error[2];
}
}
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Add Student Details</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1 class="header">New student details</h1>
<p>Student Listing </p>
<?php
if (isset($error)) {
echo "<p class='warning'>Error: $error</p>";
}
?>
<?php
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"]); ?>">
<p>
<label for="studentTitle">Title:</label>
<select name="studentTitle" id="studentTitle" ><span class="error">* <?php echo $studentTitleErr;?></span>
<option value="Mr">Mr.</option>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Miss">Miss.</option>
</select>
</p>
<p>
<label for="studentFirstName">First Name:</label>
<input type="text" name="studentFirstName" id="studentFirstName" ><span class="error">* <?php echo $studentFirstNameErr;?></span>
</p>
<p>
<label for="studentLastName">Last Name:</label>
<input type="text" name="studentLastName" id="studentLastName" ><span class="error">* <?php echo $studentLastNameErr;?></span>
</p>
<p>
<input type="submit" name="insert" value="Add Details" id="insert">
<input type="reset" name="clear" value="Clear" id="clear">
<input name="studentID" type="hidden" value="<?php echo $studentID; ?>">
</p>
</form>
</body>
</html>
Because your DB insert operation runs first and has no checks for required field data in order to run or not. So, it runs, and on success it redirects and exits:
header('Location: http://localhost/mysqlquiz/student.php');
exit;
Which clears POST and your required field logic never gets run.
How do I echo the form input without having to re-display the form after validation? I can only display the input after the form. Here is the code I have
<?php
$postalCode = $_POST['postalCode'];
$postalCodeErr = "";
$postalCodeValidation = '/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/';
$postalCodeIsValid = false;
?>
<html>
<body>
INT322 Lab 3-1
<br />
<br />
<form name="lab3form" action="index.php" method="post">
Postal Code:
<input type="text" name="postalCode" value="<?php if(isset($postalCode)) echo $postalCode; ?>" />
<?php
if(($postalCode != "") && preg_match($postalCodeValidation, $postalCode)) {
$postalCodeIsValid = true;
}
else {
$postalCodeErr = "Invalid Postal Code";
}
if(isset($postalCode)) echo " $postalCodeErr";
?>
<br />
<br />
<input type="submit" name="submit" />
</form>
</body>
</html>
<?php
if($_POST['submit'] && $postalCodeIsValid) {
echo "Postal Code: $postalCode";
}
?>
How about wrapping your form in the else of if($_POST['submit'] && $postalCodeIsValid) { ... } else { ... }
<?php
if($_POST['submit'] && $postalCodeIsValid) {
echo "Postal Code: $postalCode";
}
else {
<form name="lab3form" action="index.php" method="post">
Postal Code:
<input type="text" name="postalCode" value="<?php if(isset($postalCode)) echo $postalCode; ?>" />
<?php
if(($postalCode != "") && preg_match($postalCodeValidation, $postalCode)) {
$postalCodeIsValid = true;
}
else {
$postalCodeErr = "Invalid Postal Code";
}
if(isset($postalCode)) echo " $postalCodeErr";
?>
<br />
<br />
<input type="submit" name="submit" />
</form>
}
?>
UPDATED ANSWER with full code:
<html>
<body>
INT322 Lab 3-1
<br />
<br />
<?php
if(!empty($_POST['submit'])):
$postalCode = $_POST['postalCode'];
if(isValidPostalCode($postalCode)):
echo "Postal Code: $postalCode";
else:
form($postalCode, true);
endif;
else:
form();
endif;
?>
</body>
</html>
<?php
function form($postalCode = null, $hasError = false) { ?>
<form name="lab3form" action="postal.php" method="post">
Postal Code:
<input type="text" name="postalCode" value="<?php if(isset($postalCode)) echo $postalCode; ?>" />
<?php if ($hasError): ?>
<div class="error">Invalid Postal Code</div>
<?php endif; ?>
<br />
<br />
<input type="submit" name="submit" />
</form>
<?php }
function isValidPostalCode($postalCode) {
$postalCodeValidation = '/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/';
return !empty($postalCode) && preg_match($postalCodeValidation, $postalCode);
}
?>
Please note that your regular expression only works with postal codes such as A1B2C3 - I'm not sure if this is the behavior you want.
I have used this contact form before, I just changed out the information. For some reason now though it won't send. I gone over the html and php but I can't see the error. The only difference is that now I'm using godaddy, but other than that I'm not seeing what the error is. HELP!
HTML:
<div id="MainForm">
<form method="post" action="ContactForm-Handler.php" name="Contact Form" target="_parent" id="ContactForm" title="Contact Form">
<h1>Contact Form</h1>
<p>Please fill out information below.</p>
<p>
<label for="Name">Name:</label>
<input type="text" name="Name" id="Name" /><br /><br />
<label for="Email">Email: </label>
<input type="text" name="Email" id="Email" /><br /><br />
<label for="QuestionType">Regarding: </label>
<select name="QuestionType" id="QuestionType">
<option value="Products">Products</option>
<option value="Pricing">Pricing</option>
<option value="Terms">Terms</option>
<option value="Other Information">Other</option>
</select>
<br /><br />
<label for="MessageBox">Message: </label>
<Br /><br />
<textarea name="MessageBox" cols="50" rows="10">Enter Question/Message Here</textarea>
</p>
<p><input name="Send" type="submit" id="Send" onmouseup="ThankYou.html" value="Submit" />
</p>
</form>
</div>
PHP:
<?php
$errors = '';
$myemail = 'myemail#gmail.com';
$name = $_POST['Name'];
$email = $_POST['Email'];
$message = $_POST['MessageBox'];
$question = $_POST['QuestionType'];
if(!empty($_POST['Products'])) {
foreach($_POST['Products'] as $products) {
echo $check;
}
}
if(!empty($_POST['Pricing'])) {
foreach($_POST['Pricing'] as $pricing) {
echo $check;
}
}
if(!empty($_POST['Terms'])) {
foreach($_POST['Terms'] as $terms) {
echo $check;
}
}
if(!empty($_POST['Other Information'])) {
foreach($_POST['Other Information'] as $other) {
echo $check;
}
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact Form Submission";
$email_body = "$name needs some information regarding $question \n \n".
"$message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: ThankYou.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>qp Contact Form</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
if(!empty($_POST['Products'])) {
foreach($_POST['Products'] as $products) {
echo $check;
}
}
if(!empty($_POST['Pricing'])) {
foreach($_POST['Pricing'] as $pricing) {
echo $check;
}
}
if(!empty($_POST['Terms'])) {
foreach($_POST['Terms'] as $terms) {
echo $check;
}
}
if(!empty($_POST['Other Information'])) {
foreach($_POST['Other Information'] as $other) {
echo $check;
}
}
Remove this part of your code because here you're checking with the value of the <option> tag.
You should use name of your <select> box instead.
like this :
//we use name attributes. not value attributes. otherwise $_POST will have Undefined Index error.
if(!empty($_POST['QuestionType'])){
foreach($_POST['QuestionType'] as $QuestionType){
echo $check;
}
}
And yeah, use <?php error_reporting(E_ALL); ?> at the top of your php page to enable php to show you errors.
I made a login/register page. It works fine on chrome and firefox, but doesnt work on internet explorer (8, 9). After clicking any submit button ie redirect to index page. I have the same problem on another page based on this code.
Here is html code:
<?php
$log_in=false;
$token_sent=0;
$errors1 = array();
$missing1 = array();
$errors = array();
$missing = array();
if(isset($_POST['submit'])){
$expected = array('login', 'email', 'pwd');
$required = array('login', 'email', 'pwd');
require('./rejestracja.inc.php');
}
if ($token_sent) {
header('Location: (..) ');
exit;
}
if(isset($_POST['submit1'])) {
$expected=array('login1','pwd1');
$required=array('login1','pwd1');
require('./login.inc.php');
if($log_in) {
if (isset($_SESSION['name']) && !empty($_SESSION['name'])) {
unset($_SESSION['login1']);
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-86400, '/');
}
session_destroy();
}
if (isset($_POST['login1'])) {
session_start();
$_SESSION['name'] = $login1 ;
}
header('Location: http:// (...) ');
exit;
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<meta name="description" content="">
<meta name="copyright" content="Copyright (c) 2012 k">
<meta name="author" content="">
<link href="style.css" rel="stylesheet" type="text/css" media="screen">
<link href="login.css" rel="stylesheet" type="text/css" media="screen">
</head>
<body>
<div id="wrap">
<?php include('./login_menu.inc.php');?>
<div id="header">
<h1></h1>
</div>
<?php include('./fb.inc.php');?>
<?php include('./menu.inc.php');?>
<?php include('./w_budowie.inc.php');?>
<div id='left'>
<div id='info'>
<h1 class='title'>LOGIN</h1> <br/>
<form class='form' name="login1" action=" " method="post">
<?php if ($missing1) { ?>
<span class="warning">...</span><br/>
<?php } ?>
<?php if(isset($errors1['wrong'])){ ?>
<span class="warning">...</span><br/>
<?php } ?>
<?php if(isset($errors1['not_verified'])){ ?>
<span class="warning">...</span><br/>
<?php } ?>
<label for="login1">Login: </label>
<input type="text" name="login1"
<?php if ($missing1 || $errors1) {
echo 'value="' . htmlentities($login1, ENT_COMPAT, 'UTF-8') . '"';
} ?>
/>
<br/>
<label for="pwd1">Password:</label>
<input type="password" name="pwd1" /> <br/>
<br/>
<span class='submit'><input name="submit1" type="submit" value="LOGIN" /></span>
</form>
<br/>
</div>
</div>
<div id='right'>
<div id='info'>
<h1 class='title'>REGISTRATION</h1> <br/>
<form class='form' name="register" action=" " method="post">
<label for="login">Login: </label>
<input type="text" name="login"
<?php if ($missing || $errors) {
echo 'value="' . htmlentities($login, ENT_COMPAT, 'UTF-8') . '"';
} ?>
/>
<?php if ($missing && in_array('login', $missing)) { ?>
<span class="warning">...</span>
<?php
} elseif(isset($errors['login_mini'])){ ?>
<span class="warning">...</span>
<?php
} elseif(isset($errors['login_maxi'])){ ?>
<span class="warning">...</span>
<?php
} elseif(isset($errors['login'])){ ?>
<span class="warning"> A-Z, a-z, 0-9.</span>
<?php
} elseif(isset($errors['login_exist'])){ ?>
<span class="warning">...</span>
<?php } ?>
<br/>
<label for="email">Email: </label>
<input type="text" name="email"
<?php if ($missing || $errors) {
echo 'value="' . htmlentities($email, ENT_COMPAT, 'UTF-8') . '"';
} ?>
/>
<?php if ($missing && in_array('email', $missing)) { ?>
<span class="warning">...</span>
<?php } elseif(isset($errors['email'])){ ?>
<span class="warning">...</span>
<?php } elseif(isset($errors['email_exist'])){ ?>
<span class="warning">...</span>
<?php } ?>
<br/>
<label for="pwd">Password:</label>
<input type="password" name="pwd" />
<?php if ($missing && in_array('pwd', $missing)) { ?>
<span class="warning">...</span>
<?php
} elseif(isset($errors['pwd_mini'])){ ?>
<span class="warning">...</span>
<?php
} elseif(isset($errors['pwd_maxi'])){ ?>
<span class="warning">...</span>
<?php } ?>
<br/>
<span class='submit'><input type="submit" name="submit" value="REGISTER" /></span>
</form>
<br/>
</div>
</div>
<?php include('./footer.inc.php'); ?>
</div>
</body>
</html>
Here is 'login.inc.php' code:
<?php
$login1 = "";
$pwd1 = "";
$salt = "(...)";
/*---------------con------------------------*/
include('./sql_connect.inc.php');
/*---------------------------------------------*/
foreach ($_POST as $key => $value) {
$temp = is_array($value) ? $value : trim($value);
if (empty($temp) && in_array($key, $required)) {
$missing1[] = $key;
} elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}
if(!$missing1){
$login1 = mysql_real_escape_string($login1);
$hash = $salt . $pwd1;
for ( $i = 0; $i < 100; $i ++ ){
$hash = hash('sha256', $hash);
}
/*---------------COMPARE-----------------*/
$result = mysql_query("SELECT user_nick='$login1' FROM users WHERE user_password='$hash'") or die('Sorry, we could not count the number of results: ' . mysql_error());
if($result){
if(mysql_result($result, 0)){
/*-------IS ACTIVATED?------------------------------*/
$verified = mysql_query("SELECT user_verified='yes' FROM users WHERE user_nick='$login1'");
if($verified){
if(mysql_result($verified, 0)){
$log_in=true;
}else{
$errors1['not_verified'] = true;
}
}
}else{
$errors1['wrong'] = true;
}
}
}
mysql_close($con);
?>
Are you really using <form class='form' name="register" action=" " method="post"> ?
It could come from here, try with action="#"
There won't have any cross browser problem with PHP, it comes from HTML (generated or not)