Link to home
Start Free TrialLog in
Avatar of Large_Farva
Large_Farva

asked on

PHP Form with sessions trouble

Hello Experts,

Having trouble getting this to work.  I want to save form input in session and echo a few pages later.  The submit button on the form processes the data (creates a user) and redirects to a "dashboard".  This is within an ecommerce cms in php.  

I placed this in my header template file...before anything:

<?php 
session_start();

if(isset($_POST['submit'])){
  $_SESSION['username'] = $_POST['username'];
  $_SESSION['primary_email'] = $_POST['primary_email'];
  $_SESSION['password'] = $_POST['password'];
   
?>

Open in new window


and then my form:
<form action="http://www.mydomain.com/registration" method="post" name="myform" id="myform" >
              
<input type="text" name="username" value="" id="username" size="50"  />
<input type="password" name="password" value="" id="password" size="50" />
 <input type="text" name="primary_email" value="" id="primary_email" size="50"  />         
  <button name="product_button"  type="submit" id="submit_button">continue >> </button>
                

Open in new window


when I try to echo using this, nothing show up:
<? echo $_SESSION['primary_email'];?>

Open in new window


New to php and sessions so thanks for looking.  

Avatar of Sudaraka Wijesinghe
Sudaraka Wijesinghe
Flag of Sri Lanka image

Since you include the first code snippet on the header template, I assume session_start() is called on every page before you access any session variables? If not that should be so.

Also, place error_reporting(E_ALL); before session_start(); and see if it shows any errors.
Avatar of Large_Farva
Large_Farva

ASKER

Yes to session start and I tried the error reporting and nothing shows up.  I did have an error in my first snippet, I left off a closing bracket before php close...but still not working.  
Avatar of Marco Gasi
session_start should be called before any output. Can you show us your full page code?
start is before doctype in header file:
<?php 

session_start();

if(isset($_POST['submit'])){
  $_SESSION['username'] = $_POST['username'];
  $_SESSION['primary_email'] = $_POST['primary_email'];
  $_SESSION['password'] = $_POST['password'];
}
?>
   


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<!-- load html charset -->
<meta http-equiv="Content-Type" content="text/html; charset={charset}" />

<!-- load page title -->
<title>{sts_store_name} - {page_title}</title>

<!-- load meta info -->
<meta name="description" content="{meta_description}" />
<meta name="keywords" content="{meta_keywords}" />
<meta name="author" content="{sts_store_name}" />
<meta name="robots" content="all" />

<base href="{base_url}" />

<!-- load stylesheets -->

{lang_header_data}


</head>

<!--- container for the whole page -->
<div id="jroxContainer" class="jroxContainer">
	
 
    <!-- top menu -->
    
        {top_menu}
        
    <!-- content of page -->
    
    

Open in new window


The form in my original post is using the same template and so is the action element.  
Oooops. I saw it just now! Change the id of your submit button to 'submit' or alternatively, chenge your if statement this way:

if (isset($_POST['submit_button']))...

Actually your chack fails because of this little mistake.

cheers
"isset($_POST['submit'])" looks for a POST variable with the Name of 'submit'.  IE might use the 'id' as the name but I don't think Firefox and the others will.
Oh, yest, it's true!

Try to use

<input type='submit' name='submit', id='submit' value='continue' />

ant it should work.

I changed my php to:
<?php 

session_start();

if(isset($_POST['submit_button'])){
  $_SESSION['username'] = $_POST['username'];
  $_SESSION['primary_email'] = $_POST['primary_email'];
  $_SESSION['password'] = $_POST['password'];
}
?>

Open in new window


and still no joy.  Could there be a conflict with the cms?
No, "<button name="product_button"  type="submit" id="submit_button">continue >> </button>" doesn't work for this.

<input type='submit' name='submit' id='submit' value='continue' /> would be better.  Then go back to "isset($_POST['submit'])" .
Database error:
A Database Error Occurred
Error Number: 1054

Unknown Column 'Submit' In 'Field List'

INSERT INTO `Jem_members` (`Fname`, `Username`, `Password`, `Primary_email`, `Submit`, `Status`, `Login_status`, `Enable_affiliate_marketing`, `Updated_by`, `Updated_on`, `Signup_date`, `Confirm_id`, `Sponsor_id`, `Lname`, `Payment_name`) VALUES ('Sdfadadsf', 'Fasdfdd3333', '56ea2f4b8a17efcde124617f1056ab11', 'Dfadsd@Xalt.Me', 'Continue', '1', '0', '1', 0, 1301707677, 1301707677, 'B0EOC', '2', '', '')
Show the query code, please (but this seems to be a totally different question, uh?)
Ok, that's a different error unrelated to your question.  Except that maybe you did a bulk replace and changed something else to `Submit`.  Do you know what that field name is supposed to be?
it was supposed to be "submit_button" but I changed it to try the suggestion and the result was db error.
I changed my button settings back to the beginning and db error is gone, but still no session.  

I may try this:  Break my form into 2 pages, with page 1 containing the stuff i need in session later (just 3 items), post to page 2 session and echo in hidden field on this form with weird button settings.  Does that sound more solid?
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have not read all the other posts here, but I want to let you know about data visualization techniques that you can use any time you are not sure what is in a given data element.  Try putting this at the top of your action script, so you can see what came through from the posted form.
<?php // TEMPORARY DEBUGGING CODE
echo "<pre>";
var_dump($_POST);
die('THERE IT IS!');

Open in new window

You can test this script on my server, here:
http://www.laprbass.com/RAY_post_to_session.php

Also, let me second the recommendation of the SitePoint book.  It will not make you a pro, but it is very readable and has great examples.  Now in its fourth printing, it has been a part of my professional library since Version One.

Best regards, ~Ray
<?php // RAY_post_to_session.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO CARRY POSTED DATA FROM PAGE TO PAGE IN THE SESSION ARRAY


// ALWAYS START THE SESSION ON THE TOP OF EVERY PAGE
session_start();

// ESTABLISH A DEFAULT EMPTY STRING VALUE IF VARIABLE IS NOT SET
if (!isset($_SESSION["my_value"])) $_SESSION["my_value"] = NULL;

// SEE IF THERE IS ANYTHING IN THE POST ARRAY
if (!empty($_POST))
{
    // TIDY UP THE INPUT VALUE AND COPY IT INTO THE SESSION
    $_SESSION["my_value"] = $_POST["my_value"];

    // ECHO WHAT WE HAVE GOT ONTO THE BROWSER
    echo "<br/>HERE IS THE POST ARRAY<pre>";
    var_dump($_POST);
    echo "</pre>\n";

    // ECHO WHAT WE HAVE GOT ONTO THE BROWSER
    echo "<br/>HERE IS THE SESSION ARRAY<pre>";
    var_dump($_SESSION);
    echo "</pre>\n";

}
// END OF PHP - DROP INTO HTML TO PUT UP THE FORM
?>

ENTER SOMETHING HERE AND I WILL PUT IT INTO THE SESSION:
<form method="post">
<input name="my_value" value="<?php echo $_SESSION["my_value"]; ?>" />
<input type="submit" value="Go" />
</form>

Open in new window

Hey Dave,

Point A )

Your are using the E-commerce CMS framework and at the same time trying to use session_start method of php. It will  not work due to some couple of reasons behind this -

1- Each framework having it's own methods for to store and retrieve the session variables from and into.
2- Please don't use the any core method of php in any framework directly instead of this try to find out the alternate method of that particular framework.

Please let me know which you are currently using ?

B) Regarding Database Error -

1- Please once confirm the column listing and insert values are both equally same or not ?

2- Which type of database and database engine you are using in your application ? I don't seem any thing wrong with your sql statement.

Thanks