asked on
$value1 = $_POST['select_time1'];
$value2 = $_POST['select_time2'];
$value3 = $_POST['select_time3'];
$value4 = $_POST['select_time4'];
$value5 = $_POST['select_time5'];
$value6 = $_POST['select_time6'];
$protien_id = $_POST['protiens'];
$monday = $_POST['mon'];
$tuesday= $_POST['tues'];
$wednesday= $_POST['wed'];
$thursday = $_POST['thur'];
$friday = $_POST['fri'];
<?php
session_start();
$default = array(
'select_time1' => '',
'select_time6' => '',
'protiens' > '',
'mon' => '',
'fri' => ''
);
$data = loadData($default);
print_r($data);
/*
This function gets the data from the post - if there is none
it gets the data from the Session - if there is none it
defaults to an empty array.
It then loops through supplied default array and merges the
values from the default with those it already has from POST
or Session. It returns an array that is guaranteed to have values
for all the required values - obtained either from POST or Session
in that order
*/
function loadData($default)
{
$data = !empty($_GET['data']) ? $_GET['data'] : false;
if ($data === false) {
$data= !empty($_SESSION['data']) ? $_SESSION['data'] : array();
}
foreach($default as $key => $value) {
if (!isset($data[$key])) {
$data[$key] = $value;
}
}
$_SESSION['data'] = $data;
return $data;
}
<?php // demo/form_highlight_errors.php
/**
* Demonstrate how to highlight errors in form input, and remember valid inputs
* Client is asked to put in a value
* If the value fails our test we show an error message
* We put a marker next to the input control on the form
* We turn the form border red
* SEE http://www.w3schools.com/CSS/pr_class_visibility.asp
*/
error_reporting(E_ALL);
// THESE STYLE ELEMENTS ARE SET FOR THE SCRIPT INITIALIZATION
$error_abc = $error_xyz = $error_any = 'hidden';
$boxer_abc = $boxer_xyz = 'black';
// CAPTURE AND NORMALIZE THE POST VARIABLES - ADD YOUR OWN SANITY CHECKS HERE
$abc = isset($_POST["abc"]) ? trim(strtoupper($_POST["abc"])) : NULL;
$xyz = isset($_POST["xyz"]) ? trim(strtoupper($_POST["xyz"])) : NULL;
// IF ANYTHING WAS POSTED, VALIDATE IT
if (!empty($_POST))
{
// VALIDATE THE 'abc' FIELD
if ($abc != 'ABC')
{
// VALIDATION FAILED
$error_abc = $error_any = 'visible';
$boxer_abc = 'red';
// BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
$abc = NULL;
}
// VALIDATE THE 'xyz' FIELD
if ($xyz != 'XYZ')
{
// VALIDATION FAILED
$error_xyz = $error_any = 'visible';
$boxer_xyz = 'red';
// BECAUSE THIS FAILED VALIDATION, REMOVE IT FROM THE FORM
$xyz = NULL;
}
// DO WE HAVE INPUT FREE FROM ANY ERRORS?
if ($error_any != 'visible')
{
echo "CONGRATULATIONS - ALL VALIDATION PASSED";
die();
}
// OTHERWISE... OOPS - WE HAVE ERRORS AND MUST SHOW THE FORM AGAIN
}
// IF NOTHING WAS POSTED, OR IF THERE ARE ERRORS, WE NEED NEW CLIENT INPUT
$form = <<<ENDFORM
<style type="text/css" media="all">
.error_any { visibility:$error_any; }
.error_abc { visibility:$error_abc; }
.error_xyz { visibility:$error_xyz; }
</style>
<pre>
<form method="post">
<span class="error_any">PLEASE CORRECT THE FOLLOWING ERRORS</span>
<span class="error_abc">YOU MUST ENTER 'abc' IN THIS FIELD</span>
PLEASE ENTER "ABC" HERE: <input style="border-color:$boxer_abc;" name="abc" value="$abc" />
<br>
<span class="error_xyz">YOU MUST ENTER 'xyz' IN THIS FIELD</span>
PLEASE ENTER "XYZ" HERE: <input style="border-color:$boxer_xyz;" name="xyz" value="$xyz" />
<br>
<input type="submit" />
</form>
ENDFORM;
// WRITE THE FORM WITH THE APPROPRIATE CSS STYLES ON THE ERROR MESSAGE FIELDS
echo $form;
it's $_POST that contains the request variables.Yes but you have to test and access each one at a time. If you use a form array you can get them in one go.
<?php
session_start();
$_SESSION['POST'] = $_POST;
ASKER
ASKER
therefore the session is gone because I'm back to the first pageIf this is really happening to you, there is something else going wrong in your application. Please read the article about how sessions work. It is required knowledge for PHP developers. The session, once established, should remain in place as long as you're still on the same web site. If it's losing data when you "go back" you almost certainly have a design flaw or logic error in your app.
Sessions allow you to store complex types (Arrays and Objects)Sometimes, but not always. Please see the cautionary note here:
JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.
TRUSTED BY
Here's an article about the PHP session. HTH, ~Ray
https://www.experts-exchange.com/articles/11909/PHP-Sessions-Simpler-Than-You-May-Think.html