Is it possible to combine session variables multiple pages to a single POST in the final formhander?
Combining session variables - possible?
Of Course!!!
~Brett
lol... yeah, you can. But you just have to remember to put them into a string, and separate them. A possibility is to do "var=val&var=val&var=val". Then in PHP (when the form is submitted) just explode based upon your separarter ("&") and then list($var, $val) the explosion of the secondary separarter ("=").
Course, if you're gonna do that, why not just get the values from the session in the first place?
~Brett
another method we use is using a simple dual ~~ (tilde) signs. & signs work just as good, and it's really just about preference. However, why would you want to send session data through a POST ? It defeats the purpose of the session (whcih I think bpat is saying as well).
Im trying to create to combine session variables like so but i not sure if this is right- well it's not as I come up with Undefined index errors.
//part1.php
if (isset($_POST['submit'])) {
foreach ($_POST as $key=>$val) {
// build the column name list
$_SESSION['cols1'] .= $key . ',';
// and the values list
$_SESSION['vals1'] .= "'" . quote_smart($val) . "',";
//part2.php
if (isset($_POST['submit'])) {
foreach ($_POST as $key=>$val) {
// build the column name list
$_SESSION['cols2'] .= $key . ',';
// and the values list
$_SESSION['vals2'] .= "'" . quote_smart($val) . "',";
//part3.php
if (isset($_POST['submit'])) {
foreach ($_POST as $key=>$val) {
// build the column name list
$_SESSION['cols3] .= $key . ',';
// and the values list
$_SESSION['vals3'] .= "'" . quote_smart($val) . "',";
//formhandler.php
$strcols = rtrim($_SESSION['cols1, cols2, cols3'], ','); // remove final comma
$strvals = rtrim($_SESSION['vals1, vals2, vals3'], ',');
$sql = "INSERT INTO nottsvcs (" . $strcols . ") VALUES (" . $strvals . ")";
Like I said- the error message is Undefined index cols1 etc
$_SESSION['cols1, cols2, cols3']
What's this supposed to mean? It certainly doesn't mean $_SESSION['cols1'], $_SESSION['cols2'], etc., or any combination thereof.
Oh, I see; you wanted to take those three fields and join them together with commas.
$cols = array($_SESSION['cols1'], $_SESSION['cols2'], $_SESSION['cols3']);
$strcols = join(', ', $cols);
Undefined index errors are when you try to use an array element that doesn't exist. In
$_SESSION['cols1'] .= $key . ',';
for example, if $_SESSION['cols1'] doesn't exist yet, you'll get a notice (because PHP has to use the existing value of $_SESSION['cols1'] to work out what its new value should be).
Incidentally, don't be afraid to store arrays in arrays. It can make life a lot simpler.
// Part 1
if (isset($_POST['submit'])) {
if(!isset($_SESSION['fields']))
{
$_SESSION['fields'] = array();
}
$_SESSION['fields'] = array_merge($_SESSION['fields'], $_POST);
//Got all the fields; build the list for the query.
foreach($_SESSION['fields'] as $column=>$value)
{
$_SESSION['fields'][$column] = "'".quote_smart($value)."'";
}
$colstr = join(", ", array_keys($_SESSION['fields']));
$valstr = join(", ", array_values($_SESSION['fields']));
Consult with the manual to see what those array* functions do. Play with print_r() and see what ends up where in $SESSION['fields'].
I have a strong feeling that due to settings in the session variable part of my PHPinfo settings that my sessions are not being passed onto page to page.
Can anyone spot any irregular settings?
www.mindseyemidlands.co.uk/phpinfo.php
thx in advance
Well, the data is being sent from page to page because it's all there on the last.
Surely this means there cannot be problems with my coding- like you say- the data is being passed
But are you certain that what you're doing to put it in the database is correct?
Check out my code:
this is at the top of each of the forms:
<?php
session_start();
require_once('common_functions.php');
if (isset($_POST['submit'])) {
if(!isset($_SESSION['cols']))
$_SESSION['cols']='';
if(!isset($_SESSION['vals']))
$_SESSION['vals']='';
foreach ($_POST as $key=>$val) {
if($key <> 'submit') {
$_SESSION['cols'] .= $key . ',';
$_SESSION['vals'] .= "'" . quote_smart($val) . "',";
}
}
}
?>
and this is my formhandler than handles the db insert
<?php
include("connect.php");
session_start();
require_once('common_functions.php');
if (isset($_POST['submit'])) {
if(!isset($_SESSION['cols']))
$_SESSION['cols']='';
if(!isset($_SESSION['vals']))
$_SESSION['vals']='';
foreach ($_POST as $key=>$val) {
if($key <> 'submit') {
$_SESSION['cols'] .= $key . ',';
$_SESSION['vals'] .= "'" . quote_smart($val) . "',";
}
}
}
$strcols = rtrim($_SESSION['cols'], ','); // remove final comma
$strvals = rtrim($_SESSION['vals'], ',');
$sql = "INSERT INTO nottsvcs (" . $strcols . ") VALUES (" . $strvals . ")";
echo $sql; // to see the query string you've built
?>
I see nothing here that inserts anything into any database.