Form validator endpoint not found - php

I'm trying to write a form for User Information to keep track of who downloads files from my website.
I downloaded a PHP form validator from this website and I tried to mimic the example 3 with client-side validation and here is what I came up with (please excuse the length, but I figured it was necessary for understanding everything):
<?php
require_once(ABSPATH. '/wp-includes/FormValidator/include/formvalidator.php');
$validation_errors='';
if(isset($_POST['submitButton']))
{// We need to validate only after the form is submitted
//Setup Server side Validations
//Please note that the element name is case sensitive
$validator = new FormValidator();
$validator->addValidation("firstname","req","Please fill in your First Name");
$validator->addValidation("firstname","alpha","Only letters a-z/A-Z are allowed for your First Name");
$validator->addValidation("lasttname","req","Please fill in your Last Name");
$validator->addValidation("lastname","alpha","Only letters a-z/A-Z are allowed for your Last Name");
//Then validate the form
if($validator->ValidateForm())
{
//If the validations succeeded, proceed with form processing
addUserDataToDB();
}
else
{
//Validations failed. Display Errors.
$error_hash = $validator->GetErrors();
foreach($error_hash as $inpname => $inp_err)
{
$validation_errors .= "<p>$inpname : $inp_err</p>\n";
}
}
}//if
$first_name = isset($_POST['firstname'])?$_POST['firstname']:'';
$last_name = isset($_POST['lastname'])?$_POST['lastname']:'';
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title>Whitepaper Download User Info</title>
<style type="text/css">
.error
{
font-family: Verdana, Arial, sans-serif;
font-size: 0.7em;
color: #900;
background-color : #ffff00;
}
</style>
<script type='text/javascript' src='<?php echo ABSPATH.'/wp-includes/FormValidator/scripts/gen_validatorv31.js' ?>'>
</script>
</head>
<body>
<form id="DownloadUserDataForm" style="width:100%;">
<fieldset>
<div style="width:100%;">
<p><b>Input Fields with (*) are Required.</b> <br/><p>
<div>
<span class='error'><?php echo $validation_errors; ?></span>
</div>
<div style="width:100%;">
<div style="display:inline-block; float:left; width:48%; margin-right:5px;">
<b>First Name *:</b> <input type="text" name="firstname" style="width:82%;" maxlength="50" <?php if (isset($errors)) { echo 'value="'.htmlentities($_POST['first_name']).'"'; }?>
/> <br />
<span id='firstname_errorloc' class='error'></span>
</div>
<div style="display:inline-block; float:left; width:48%;">
<b>Last Name *:</b> <input type="text" name="lastname" maxlength="50" style="display:inline-block; width:82%;" <?php if (isset($errors)) { echo 'value="'.htmlentities($_POST['last_name']).'"'; } ?> /><br/>
<span id='lastname_errorloc' class='error'></span>
</div>
</div>
<br />
<div class="clear:both;">
<p><input type="submit" name="submitButton" value="Submit" style="clear:both;" /></p>
</div>
</div>
</fieldset>
</form>
</body>
</html>
<?php
//Function for Processing Data
function addUserDataToDB()
{
global $wpdb;
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$rows_affected = $wpdb->insert( 'wp_downloadUserData', array( 'time' => current_time('mysql'), 'firstname' => $firstname,
'lastname' => $lastname ) );
//Redirect to download page
wp_redirect('Whitepaper.htm');
exit();
}
?>
<!-- client-side Form Validations:
Uses the form validation script from JavaScript-coder.com
See: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml
-->
<script type='text/javascript'>
console.log("javascript ran");
var frmvalidator = new Validator("DownloadUserDataForm");
frmvalidator.EnableOnPageErrorDisplay();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("firstname","req","Please fill in your First Name");
frmvalidator.addValidation("firstname","alpha","Only letters a-z/A-Z are allowed for your First Name");
frmvalidator.addValidation("lasttname","req","Please fill in your Last Name");
frmvalidator.addValidation("lastname","alpha","Only letters a-z/A-Z are allowed for your Last Name");
</script>
All that happens is the page refreshes, no errors or validations, and it doesn't seem to write to $wpdb because I have an admin page that queries the db table and it comes up empty (it also doesn't wp_redirect() like it should). Also for some reason it cannot find the javascript file even though the address looks correct.
GET
http:///home/public_html/wp-includes/FormValidator/scripts/gen_validatorv31.js
404 (Not Found)

For your javascript error try changing this line:
<script type='text/javascript' src='<?php echo ABSPATH.'/wp-includes/FormValidator/scripts/gen_validatorv31.js' ?>'>
to
<script type="text/javascript" src="<?php echo ABSPATH; ?>/wp-includes/FormValidator/scripts/gen_validatorv31.js"></script>

Related

PHP Checkbox Values Not Saved During Error Check or Posting

The "newvar_" values for "029, 032 and 033" are not being remembered during error-checking, nor are they being posted when a corrected form is submitted.
The "name" field works correctly both on error checking and posting.
The form default for the all checkboxes is not selected.
The user should be able to select only the forms they wish. Their selection should post to the txt file along with their name.
The validation library am using can be found at: http://www.benjaminkeen.com/software/php_validation
Thanks in advance for your assistance.
<?php
// Eliminate server error notices except for parse errors
error_reporting(E_PARSE);
// Receiving variables
$errors = array(); // set the errors array to empty, by default
$fields = array(); // stores the field values
if (isset($_POST['submit']))
{
// Import the validation library
require("../include/validation.php");
$rules = array(); // stores the validation rules
// Rules specific to this form
$rules[] = "required,name,Please enter Your Name.";
$rules[] = "length<71,name,Your Name is too long.";
// Check the user's entries for errors based upon above rules
$errors = validate_fields($_POST, $rules);
// If there were errors, re-populate the form fields with user entries
if (!empty($errors))
{
$fields = $_POST;
}
// If there were no errors
else
{
// Redirect to a "thank you" page URL shown below
header("Location: thank-you.php");
}
}
// Count the errors. If none process the form data
if (count($errors) == 0) {
// Location of flat file database - absolute path
$datafile_01 = '/home/domain/public_html/Forms.txt';
// Strip-out the HTML tags
$_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
// Write successfully submitted form data to flat file database
if ($_POST['submit']) {
$file = fopen($datafile_01, "a");
if (!$file) {
die("Can't write to database file");
}
// Set timezone to match delivery location
date_default_timezone_set('America/New_York');
$date = date('Y m-d g:i:s a');
// Select and code the fields being posted
$name = $_POST['name'];$name=($name);
//FORMS & SUPPLIES
// Standard Forms they requested
// Change the checkbox 'on' to something else (defined by $Newvar)
if($Form_F_029 == 'on') { $Newvar_029 = &$Form_F_029; $Newvar_029 = "X"; }
if($Form_F_032 == 'on') { $Newvar_032 = &$Form_F_032; $Newvar_032 = "X"; }
if($Form_F_033 == 'on') { $Newvar_033 = &$Form_F_033; $Newvar_033 = "X"; }
// Format the fields for the flat file
fwrite($file,
"Name:\t $name\n
REQUESTED FORMS AND SUPPLIES:\n
STANDARD FORMS ++++++++++++++++++++
Form F-029:\t $Newvar_029
Form F-032:\t $Newvar_032
Form F-033:\t $Newvar_033\n
=============================================================================== EOF =\n\n);"
fclose($file);
}
// Format and send email notice of successful submittal
if ($_POST['submit']) {
$sn_header = "From: Inquiries Database\n"
. "Reply-To: no_reply#domain.com\n";
$sn_subject = "Ordering Supplies Request";
$sn_email_to = "someone#domain.com";
$sn_message = "Please check the database for a recent submission.\n\n"
. "This is a post-only message.\n\n"
. "Do not reply.\n";
#mail($sn_email_to, $sn_subject ,$sn_message ,$sn_header ) ;
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Form Test</title>
<meta charset="utf-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1" />
<?php //This CSS needed only for forms to style Modal ?>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"
media="screen" />
</head>
<body>
<!-- Wrapper -->
<div id="wrapper" class="divided">
<section class="wrapper style1 align-left">
<div class="inner">
<h2 class="color-1">Measuring & Ordering Supplies</h2>
<form action="<?=$_SERVER['PHP_SELF']?>" name="inquiry" method="post">
<div class="field">
<label style="margin:1rem 0 .25rem 0"><i class=" color-1 fa fa-star"></i> Your Name:</label>
<input type="text" name="name" id="name" value="<?=$fields['name']?>" />
</div>
<div class="field" style="padding-top:1rem">
<label style="font-size:110%; margin-bottom:0"><b class="color-1">Standard Forms</b>
<span style="font-weight:300; font-size:80%">Check box to order | Click name to download PDF</span><br />
</label>
</div>
<div class="field third first" style="padding-top:10px">
<input type="checkbox" id="Form_F_029" name="Form_F_029" <?php if($Form_F_029 == "on"){echo "CHECKED";}?> />
<label for="F_029">Form F 029
</label>
</div>
<div class="field third" style="padding-top:10px">
<input type="checkbox" id="Form_F_032" name="Form_F_032" <?php if($Form_F_032 == "on"){echo "CHECKED";}?> />
<label for="Form_F_032">Form F 032
</label>
</div>
<div class="field third" style="padding-top:10px">
<input type="checkbox" id="Form_F_033" name="Form_F_033" <?php if($Form_F_033 == "on"){echo "CHECKED";}?> />
<label for="Form_F_033">Form F 033
</label>
</div>
<ul class="actions" style="padding-top:30px">
<li><input type="submit" class="button small" name="submit" id="submit" value="Submit Request" /></li>
</ul>
</form>
</div>
<div id="error" title="Form Errors:">
<?php
// if $errors is not empty display them in the modal
if (!empty($errors))
{
echo "<div style=\"padding:15px 15px 0 15px\">";
echo "<ul style=\"margin-bottom:20px\">";
foreach ($errors as $error)
echo "<li style=\"font-size:15px; padding:5px\">$error</li>";
echo "</ul></div>";
}
?>
</div>
</section>
<!-- Footer -->
</div>
<!-- Scripts -->
<?php include $_SERVER["DOCUMENT_ROOT"] . "/assets/includes/php/javascripts-01.php"; ?>
<?php // Supporting scripts for Error Modal ?>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
<?php if( isset( $_POST["submit"] ) ){ ?>
$('#error').dialog({
height: 380,
width: 260,
modal: true,
resizable: false,
dialogClass: 'no-close error-dialog'
});
<?php } ?>
</script>
</body>
</html>
The values of $Form_F_029, _032, _033 are never set.
You copied the POST variables into $fields:
$fields = $_POST;
So this worked as intended:
<input type="text" name="name" id="name" value="<?=$fields['name']?>" />
But uses of $Form_F_0xx are treated as null, undefined or false depending on context:
<input type="checkbox" id="Form_F_029" name="Form_F_029" <?php if($Form_F_029 == "on"){echo "CHECKED";}?> />
You could fix this by setting
$Form_F_029 = $fields['Form_F_029'];
or
$Form_F_029 = $_POST['Form_F_029'];
etc.
For debugging problems like this it can help to include the values of variables and arrays in your output, for example in a comment or pre tag:
echo "<!-- 29= $Form_F_029, 32 = $Form_F_032 -->"; // DEBUGGING
echo "<pre>- 29= $Form_F_029, 32 = $Form_F_032 </pre>"; // DEBUGGING
Just remember to delete them or comment them out later.

How do I check which input button has been pressed in PHP?

so I'm new to php and I have two buttons on this html page here (the id value is included in the url):
<!DOCTYPE html>
<head>
<title>StoryBlox is a Social Story Builder Tool</title>
<meta charset="utf-8">
<!-- these support the header/footer formatting -->
<link type="text/css" rel="stylesheet" href="css/main.css">
<link type="text/css" rel="stylesheet" href="css/header_footer.css">
<script src="js/header.js"></script>
<?php //include_once 'confirm_login.php'
include_once 'story_manager.php';
include_once 'open_connection.php';
//include_once 'functions.php';
//sec_session_start();
if(isset($_GET['id'])){
$str_id = $_GET['id'];
$draft_id = get_story_attribute($str_id, 'draft');
}else{
echo "Invalid story id.";
echo "<br>";
}
?>
</head>
<body>
<div id="wrapper_main">
<div id="wrapper_content">
<?php include_once 'header.php'; ?>
<h1>Welcome to StoryBlox Create Story!</h1>
</div>
<!-- menu -->
<!--<div id="inputs"> -->
<form id="create_form" action="save_story.php?id=<?php echo $str_id?>" method="POST">
<input type="text" name="storyTitle" id="title" placeholder="Enter title." autofocus/><br>
<textarea rows="4" cols="50" name="storyDesc" id="description" placeholder="Enter description here."></textarea>
<div id="footer">
<input type="button" name="draftBtn" onclick="this.form.submit()" value="Save as Draft"/>
<input type="button" name="finalBtn" onclick="this.form.submit()" value="Finished!"/>
</div>
</form>
</div>
</div>
<?php include_once 'footer.php'; ?>
</body>
When I click one of these two buttons, I'm brought to this php document here:
include_once 'open_connection.php';
include_once 'story_manager.php';
$mysqli = open_connection();
if($_SERVER['REQUEST_METHOD'] === 'POST'){
if(isset($_POST['draftBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_GET['id'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
header('Location: createStory.php');
}
elseif(isset($_POST['finalBtn'])){
$title = $_POST['storyTitle'];
$desc = $_POST['storyDesc'];
$str_id = $_POST['storyID'];
update_story_title($str_id, $title);
//update_story_description($str_id, $desc);
save_draft_as_completed($str_id);
header('Location: ../home.php');
}else{ echo "failed";}
}?>
And I always get "failed" printed out on my page. I've been Googling this for hours and I don't understand where I'm going wrong here. If anybody could help that would be appreciated. Also, if anyone could shed some light on what the equivalent to
<input type="textarea">
would be that would be great. Thanks!
Use
<input type="submit" name="draftBtn" value="Save as Draft"/>
instead of the button types with their onclick events.
try to use submit tag instead of button.
and if you want to use button tag then you can pass value through hidden field. set value of hidden field on click event.

PHP form validation, uses Javascript for error messages

OK so I have an email form on index.php. I am using mail_check.php to validate that both fields are filled in.
(there are more that is being validated, but not included as it is not the issue)
The main issue is that from the mail_check.php I want to be sent back to the index.php with a message in placed in the div id="mailrespond". I have chosen both PHP and Javascript to achieve this.
Index.php
<div id="mailrespond"></div>
<form method="post" action="mail_check.php">
<span>Name: <input type="text" name="name" maxlength="30" /></span><br />
<span>Email: <input type="text" name="email" maxlength="30" /></span><br />
<input type="submit" value="Sign Up" name="registration">
</form>
</div>
mail_check.php
if(isset($_POST['registration']))
$name = $_POST['name'];
$email = $_POST['email'];
if(empty($email) || empty($name)){
// if email and name are empty
//header ("location: index.php");
?>
<script language="javascript" type="text/javascript">
window.location.replace("index.php");
function msg() {
// creating elements are a safer method then innerHTML
dv = document.createElement('div'); // creates a Div element
dv.setAttribute('class', 'error_msg'); // adds error styling
txt = document.createTextNode('please enter your name and email '); // create the message
dv.appendChild(txt); // place the text node on the element
document.getElementById("mailrespond").appendChild(dv);
}
window.onload = msg;
</script>
<?php }
The code goes on, but My issue is that I am not getting the feed back messages. I am a little new to this all - if you can help that would be much appreciated! :)
<---- #downvoter please leave a reason! Thanks
Try this (and don't call a form field name, please since a form can have a name too)
Index.php
<!DOCTYPE html>
<html>
<head>
<title>Mail respond</title>
<script>
window.onload=function() {
document.getElementById("mailform").onsubmit=function(){
if (this.fullname.value=="" || this.email.value=="") {
alert("Please fill in name and email");
return false;
}
return true; // allow form submission
}
}
</script>
</head>
<body>
<div id="mailrespond"></div>
<form method="post" action="mail_check.php" id="mailform">
<span>Name: <input type="text" name="fullname" maxlength="30" /></span><br />
<span>Email: <input type="text" name="email" maxlength="30" /></span><br />
<input type="submit" value="Sign Up" name="registration">
</form>
</div>
</body>
</html>
mail_check.php
<?PHP
if(isset($_POST['registration']))
$name = $_POST['fullname'];
$email = $_POST['email'];
if(empty($email) || empty($name)){
// if email and name are empty - only seen if JavaScript is turned off
?>
<h3>You did not fill in name and email</h3>
<p>please wait to be redirected or click <a href='index.php'>here</a></p>;
<meta http-equiv="refresh" content="3; url=http://example.com/index.php" />
<script type="text/javascript">
window.onload=function() {
setTimeout(function() {
window.location.replace("index.php");
},3000);
}
</script>
<?php } ?>
You're redirecting to index.php in the msg function, so it does nothing else.
You should do it by pure PHP, set a component in $_SESSION, redirect by PHP to index.php and in the index.php check if the component exists.

PHP form not processing in process.php

I'm having difficulty figuring out why my PHP form is processing on process.php but not returning to the form page with the appropriate $messages. Am I missing a line of code? I wrote this all up myself and it's the first time.
Here is my html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Contact Form - jQuery</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h3>Contact Us</h3>
<?php echo $contact_message; ?>
<form id="myForm" method="post" action="process.php">
<input name="name" type="text" value="<?php echo $_POST[name]; ?>" placeholder="Name" required/>
<br>
<input name="email" type="email" value="<?php echo $_POST[email]; ?>" placeholder="you#yourmail.com" required/>
<br>
<textarea name="message" class="message" placeholder="We can answer your questions." required>
<?php echo $_POST[message]; ?>
</textarea>
<br>
<button type="submit" name="submit" class="btn send">
<img src="img/send.png">
</button>
<br>
<?php echo $contact_success_message; ?>
</form>
<!--close contact form-->
</body>
</html>
And here is my process.php
<?php
//checks for valid email
function is_valid_email($email) {
$result = true;
$pattern = '/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\#([a-z0-9])(([a-z0-9-])*([a-z0-9]))+(\.([a-z0-9])([-a-z0-9_-])?([a-z0-9])+)+$/i';
if(!preg_match($pattern, $email)) {
$result = false;
}
return $result;
}
//when send is pressed, validate fields
if(isset($_POST['submit'])) {
$valid = true;
$contact_message = '';
if ( $_POST['name'] == "" ) {
$contact_message .= "You forgot to tell us your name. ";
$valid = false;
}
if ( !is_valid_email($_POST['email']) ) {
$contact_message .= "A valid email is required, don't worry we don't share it with anyone. ";
$valid = false;
}
if ( $_POST['message'] == "" ) {
$contact_message .= "What did you want to ask us? ";
$valid = false;
}
//if everything checks out, send the message!
if ( $valid == true ) {
$safe_email = str_replace("\r\n","",$_POST[email]);
$mail = "From: $_POST[name]\n";
$mail .= "Email: $_POST[email]\n";
$mail .= "$_POST[message]\n";
mail('ME#MYEMAIL.COM','New Contact from RN+',$mail,"From: $safe_email\r\n");
$contact_success_message = 'Brilliant I say! We will be in contact with you shortly.';
//clear form when submission is successful
unset($_POST);
}
}
?>
I could have sworn that I've used this before but this time it's not returning to the contact page.
It looks like you meant to use the code like this:
form.php
<?php include 'process.php'; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test Contact Form - jQuery</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</head>
<body>
<h3>Contact Us</h3>
<?php echo $contact_message; ?>
<form id="myForm" method="post" action="process.php">
<input name="name" type="text" value="<?php echo $_POST[name]; ?>" placeholder="Name" required/><br>
<input name="email" type="email" value="<?php echo $_POST[email]; ?>" placeholder="you#yourmail.com" required/><br>
<textarea name="message" class="message" placeholder="We can answer your questions." required><?php echo $_POST[message]; ?></textarea><br>
<button type="submit" name="submit" class="btn send"><img src="img/se
__
formnd.png"></button><br>
<?php echo $contact_success_message; ?>
</form><!--close contact form-->
</body>
</html>
The form processor will set the appropriate variables you are outputting in your HTML code. Since process.php checks if the method is POST, you don't have to do that in the form page.
If you want process.php to redirect back to your form, you need to add a PHP header code like: header('Location: http://www.example.com/form.php');
If you want to carry through any data back to the original page, include it in the URL as a GET variable: header('Location: http://www.example.com/form.php?message='.$messagetext); You can then retrieve this on your form page through use of GET: echo $contact_success_message = $_GET['message'];
Do not forget to exit(); or die(); after your redirect!
If you don't have any reason for excluding a single PHP page, you could merge the two (form and process) into one php page.
<?php
//checks for valid email function
...
if(isset($_POST['submit'])) {
...
}
?>
...your HTML goes here...
This will display just the form if no data has been submitted, and if the form has been submitted will "reload" the page, perform the action, and display the appropriate message. Change the form action to action="" so the file will post to itself.
I would recommend using jQuery validation. It is easier and will help with any issues you might have in returning the message.
http://docs.jquery.com/Plugins/Validation
Something like this...
$("#myForm").validate({
rules: {
name: { required: true; }
},
messages: {
name: "You forgot to tell us your name.",
}
});
You can do this for email fields and for your whole form. You can find plenty of examples online.
Just include your other fields. If you do it this way the validation is client side and the form will process and then you forward to a thank you for contacting us page.

Form Validation Error Message not showing

Was hoping for some fresh eyes on this. I've got a page which will add a new event to a database. All 3 fields are required, so if they are empty when the submit button is pushed the error message should appear. Anyone know why it isn't showing the error message? Thanks in advance!
Here's the code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>ADMIN - Repetative Strain Injury UK</title>
<meta http-equiv="content-type" content="application/xhtml; charset=utf-8" />
<link href="/iis_project/view/css/mycss.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<!-- wrapper div for positioning -->
<div class="grid10 first" id="header">
<!-- Header Section -->
<img src="/iis_project/view/img/banner.jpg" alt="RSI UK Banner" width="1090" height="75"/>
</div>
<div class="grid11 first" id="date">
<!-- Date Section -->
<?php
include ("date.php");
?>
</div>
<div class="grid2 first" id="col1">
<!-- Left column Section -->
<h1>Navigation</h1>
<p>
<strong>Home</strong>
</p>
<p>
<strong>About Us</strong>
</p>
<p>
<strong>Register</strong>
</p>
<p>
<strong>Log In</strong>
</p>
<p>
<strong>Events</strong>
</p>
<p>
<strong>Acknowledgements</strong>
</p>
</div>
<div class="grid6" id="col2">
<!-- Middle column Section -->
<h1>New Event</h1>
<p>
As an admin you have the right to create a new event. Simply fill out the form below and hit submit.
</p>
<?php
// creates the new record form
// since this form is used multiple times in this file, I have made it a function that is easily reusable
function renderForm($name, $date, $location, $error22)
{
?>
<?php
// if there are any error22s, display them
if ($error22 != '') {
echo '<div style="padding:4px; border:1px solid red; color:red;">' . $error22 . '</div>';
}
?>
<form action="" method="post">
<div>
Name:
<input type="text" name="name" value="<?php echo $name;?>" />
<br/>
Date:
<input type="text" name="date" value="<?php echo $date;?>" />
<br/>
Location:
<input type="text" name="location" value="<?php echo $location;?>" />
<br/>
<input type="submit" name="submit_event" value="Submit">
</div>
</form>
<?php
}
// connect to the database
include ("home_connection.php");
// check if the form has been submitted. If it has, start to process the form and save it to the database
if (isset($_POST['submit']))
{
// get form data, making sure it is valid
$name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
$location = mysql_real_escape_string(htmlspecialchars($_POST['location']));
// check to make sure all fields are entered
if ($name == '' || $date == '' || $location == '')
{
// generate error22 message
$error22 = 'error22: Please fill in all required fields!';
// if either field is blank, display the form again
renderForm($name, $date, $location, $error22);
}
else
{
// save the data to the database
mysql_query("INSERT events SET name='$name', date='$date', location='$location'")
or die(mysql_error22());
// once saved, redirect back to the view page
header("Location: login_success_admin.php");
}
}
else
// if the form hasn't been submitted, display the form
{
renderForm('','','','');
}
?>
</div>
<div class="grid2" id="col3">
<!-- Right column Section -->
<h1>Newsletter</h1>
<h2>Sign up to our newsletter:</h2>
<?php
include ("newsletter.php");
?>
</div>
<div class="grid10 first" id="footer">
<!-- Footer Section -->
<p>
Repetative Strain Injury UK © West Street, Jubille Way, Leeds, LS6 3QW. Email: info#rsiuk.org
Tel: 0113 658102. Reg charity no: 1032941
</p>
</div>
</div> <!-- end container -->
</body>
</html>
You have the submit name as "submit_event"
So you must use
if (isset($_POST['submit_event'])) {
.
.
.
}
Hope it helps
Try adding
<input type="hidden" name="submit" value="1" />
to your form. The $_POST array does not have a 'submit' value in the code you have provided, so it's just going to display the form again.

Categories