assign form input to a session

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

assign form input to a session

Post by jonnyfortis »

i have a form that has a a few fields the are being sent to the database. one of the fields is an ID that is auto incremented i am trying to create a session for the ID to use on the next page once the form has been submitted but the session isn't showing on the next page

here is my code
page1.php

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO wildOrchidRes (ID, name, address, town, county, postCode, country, email, telephone, checkIn, checkOut, amount) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['ID'], "int"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['town'], "text"),
                       GetSQLValueString($_POST['county'], "text"),
                       GetSQLValueString($_POST['postCode'], "text"),
                       GetSQLValueString($_POST['country'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['telephone'], "text"),
                       GetSQLValueString($_POST['checkIn'], "text"),
                       GetSQLValueString($_POST['checkOut'], "text"),
                       GetSQLValueString($_POST['amount'], "double"));

  mysql_select_db($database_WO, $WO);
  $Result1 = mysql_query($insertSQL, $WO) or die(mysql_error());

  $insertGoTo = "confirm.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

session_start();
$_SESSION['orderID'] = $_POST['ID'];

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<input type="hidden" name="MM_insert" value="form1" />
</form>

confirm.php

Code: Select all

<?php 
session_start();
$_SESSION['orderID'];
?>
i did also try and use another post that has to be inputted

$_SESSION['orderID'] = $_POST['name'];

confirm.php

Code: Select all

<?php 
session_start();
$_SESSION['orderID'];
?>
but neither are showing on the confirm.php
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: assign form input to a session

Post by Christopher »

Maybe there is some missing code, but your form does not have an ID field. So $_POST['ID'] won't be defined:

Code: Select all

$_SESSION['orderID'] = $_POST['ID'];
// is the same as
$_SESSION['orderID'] = null;
Also you have a redirect in your page. A redirect is within the same request, but session vars are not written until the request is complete. You would need to force the vars to be written for them to be available on the second page.

Difficult to know what the problem is so I am just throwing ideas out there.
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: assign form input to a session

Post by jonnyfortis »

Christopher wrote:Maybe there is some missing code, but your form does not have an ID field. So $_POST['ID'] won't be defined:

Code: Select all

$_SESSION['orderID'] = $_POST['ID'];
// is the same as
$_SESSION['orderID'] = null;
Also you have a redirect in your page. A redirect is within the same request, but session vars are not written until the request is complete. You would need to force the vars to be written for them to be available on the second page.

Difficult to know what the problem is so I am just throwing ideas out there.

Hi sorry i didnt include that part of the form. there is an ID field. i have moved

session_start();
$_SESSION['orderID'] = $_POST['name'];

to before the redirect but still nothing.

regarding the ID feild this is auto incremented so doesnt need to have a value.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: assign form input to a session

Post by Christopher »

Did you try writing the session before redirecting?
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: assign form input to a session

Post by jonnyfortis »

Christopher wrote:Did you try writing the session before redirecting?
yes the full code looks like this

Code: Select all

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO wildOrchidRes (ID, orderID, name, address, town, county, postCode, country, email, telephone, checkIn, checkOut, amount) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['ID'], "int"),
                       GetSQLValueString($_POST['orderID'], "text"),
                       GetSQLValueString($_POST['name'], "text"),
                       GetSQLValueString($_POST['address'], "text"),
                       GetSQLValueString($_POST['town'], "text"),
                       GetSQLValueString($_POST['county'], "text"),
                       GetSQLValueString($_POST['postCode'], "text"),
                       GetSQLValueString($_POST['country'], "text"),
                       GetSQLValueString($_POST['email'], "text"),
                       GetSQLValueString($_POST['telephone'], "text"),
                       GetSQLValueString($_POST['checkIn'], "text"),
                       GetSQLValueString($_POST['checkOut'], "text"),
                       GetSQLValueString($_POST['amount'], "double"));
 
 
  mysql_select_db($database_WO, $WO);
  $Result1 = mysql_query($insertSQL, $WO) or die(mysql_error());
 
  session_start();//created order session
  $_SESSION['orderID'] = $_POST['name'] . $_POST['checkIn'] . $_POST['checkOut'] ;
 
  $insertGoTo = "confirm.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}
and the form is

Code: Select all

<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
<input type="hidden" name="ID" value="" size="32" />
<input type="text" name="name" value="" size="32" />
<input type="text" name="address" value="" size="32" />
<input type="text" name="town" value="" size="32" />
<input type="text" name="county" value="" size="32" />
<input type="text" name="postCode" value="" size="32" />
<input type="text" name="country" value="" size="32" />
<input type="text" name="email" value="" size="32" />
<input type="text" name="telephone" value="" size="32" />
<input type="text" name="checkIn" value="" id="CheckIn" size="32" />
<input type="text" name="checkOut" value="" id="CheckOut" size="32" />
<input type="text" name="amount" value="" size="32" />
<input type="submit" value="confirm order" />
<input type="hidden" name="orderID" value="<?php echo $_SESSION['orderID']; ?>" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: assign form input to a session

Post by Christopher »

Try this if you want the session data available on the redirected page:

Code: Select all

  session_start();//created order session
  $_SESSION['orderID'] = $_POST['name'] . $_POST['checkIn'] . $_POST['checkOut'] ;
  session_write_close();
(#10850)
jonnyfortis
Forum Contributor
Posts: 462
Joined: Tue Jan 10, 2012 6:05 am

Re: assign form input to a session

Post by jonnyfortis »

Christopher wrote:Try this if you want the session data available on the redirected page:

Code: Select all

  session_start();//created order session
  $_SESSION['orderID'] = $_POST['name'] . $_POST['checkIn'] . $_POST['checkOut'] ;
  session_write_close();
hI that's not this issue

the session is available on the next page, what is not happening is the session $_SESSION['orderID'] is not populating the <input type="hidden" name="orderID" value="<?php echo $_SESSION['orderID']; ?>" field so not being added to the database so when i run the query on the next page (confirm.php)

Code: Select all

session_start();

$colname_rsOrder = "-1";
if (isset($_SESSION['orderID'])) {
  $colname_rsOrder = $_SESSION['orderID'];
}
mysql_select_db($database_WO, $WO);
$query_rsOrder = sprintf("SELECT * FROM wildOrchidRes WHERE orderID = %s", GetSQLValueString($colname_rsOrder, "int"));
$rsOrder = mysql_query($query_rsOrder, $WO) or die(mysql_error());
$row_rsOrder = mysql_fetch_assoc($rsOrder);
$totalRows_rsOrder = mysql_num_rows($rsOrder);
the correct information is not being shown as the column orderID that should be storing the session $_SESSION['orderID'] is empty (currently for testing this is not null) if i set to null i am getting error "Column 'orderID' cannot be null"

does this make sense
Post Reply