• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 494
  • Last Modified:

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.  

0
Large_Farva
Asked:
Large_Farva
  • 6
  • 5
  • 5
  • +3
2 Solutions
 
Sudaraka WijesingheWeb Application ProgrammerCommented:
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.
0
 
Large_FarvaAuthor Commented:
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.  
0
 
Marco GasiFreelancerCommented:
session_start should be called before any output. Can you show us your full page code?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Large_FarvaAuthor Commented:
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.  
0
 
Marco GasiFreelancerCommented:
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
0
 
Dave BaldwinFixer of ProblemsCommented:
"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.
0
 
Dave BaldwinFixer of ProblemsCommented:
Look under Browser support here: http://www.w3schools.com/tags/tag_button.asp
0
 
Marco GasiFreelancerCommented:
Oh, yest, it's true!

Try to use

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

ant it should work.

0
 
Large_FarvaAuthor Commented:
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?
0
 
Dave BaldwinFixer of ProblemsCommented:
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'])" .
0
 
Large_FarvaAuthor Commented:
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', '', '')
0
 
Marco GasiFreelancerCommented:
Show the query code, please (but this seems to be a totally different question, uh?)
0
 
Dave BaldwinFixer of ProblemsCommented:
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?
0
 
Large_FarvaAuthor Commented:
it was supposed to be "submit_button" but I changed it to try the suggestion and the result was db error.
0
 
Large_FarvaAuthor Commented:
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?
0
 
Marco GasiFreelancerCommented:
To submit the form you must use <inout type='submit' not button tag. If you want name it submit_button, you can do it but in this case you have to use if(isset($_POST['submit_button'])).

Anyway there is some malformed code if you receive an error using the correct code. If you wish get help you have to post full code: there is no need to split your form in two pages.

Now I have to go. If you post full code, when I'll come back I take a look.

I suggest you to read this book http://www.sitepoint.com/books/phpmysql4/: it is a good introduction to php and mysql. :-)

Cheers
0
 
Dave BaldwinFixer of ProblemsCommented:
I agree with @marqusG, you don't need to split up your code.  You do have to make your POST variables match the names in your forms AND the field names in the INSERT have to match the database.  I'll check in tomorrow too.
0
 
Ray PaseurCommented:
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

0
 
Ray PaseurCommented:
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

0
 
msdandageCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 6
  • 5
  • 5
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now