SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_SESSION help please

    Hi,

    Wondering if anyone could help. I post some values from a story form through to another page and output them in a for loop like so:

    PHP Code:
    if ($_POST['Submit']) {

    for (
    $i 0$i count($_POST['story_id']); $i++) {
      echo 
    "{$_POST['headline'][$i]}<br />";
      echo 
    "{$_POST['story_id'][$i]}<br />";
      echo 
    "{$_POST['live'][$i]}<br />";
    }


    But instead of echoing them is there any way I can store them in a $_SESSION, so I can access them at any time and they are held in memory?

    Thanks

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sure:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['formData'] = $_POST;
    When you next want to access the form posted data, you'd use $_SESSION['formData']['itemName'];

    e.g:
    PHP Code:
    $formData $_SESSION['formData'];
    if (
    Array_Key_Exists('Submit'$formData)) {
        foreach(
    $formData['headline'] as $Key => $Value){
            echo 
    "{$formData['headline'][$Key]}<br />";
            echo 
    "{$formData['story_id'][$Key]}<br />";
            echo 
    "{$formData['live'][$Key]}<br />";
        }

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    What i'm not sure of though is accessing these values in the session. As you will see in the below code I want to send an email ONLY when another button is pressed on this page - and access these values and put them on an email, but the email does not contain these values when it's sent?

    First Page

    PHP Code:
    <form action="test.php" method="post"> 

    <?
    // Set how many fields
    $number 5;
    for(
    $i 1$i <= $number$i++)
    {
    echo 
    "<input type=\"hidden\" name=\"headline[]\" value=\"Headline\">";
    echo 
    "<input type=\"hidden\" name=\"story_id[]\" value=\"12\">";
    echo 
    "<input type=\"hidden\" name=\"live[]\" value=\"1\">";                               
    }
    ?>
        
    <input type="submit" name="Submit" value="Submit" />
    test.php

    PHP Code:
    <?php

    session_start
    ();
    $_SESSION['formData'] = $_POST

    $formData $_SESSION['formData'];

    //These items are echoed to the page with no problems
    if (Array_Key_Exists('Submit'$formData)) {
    foreach(
    $formData['headline'] as $Key => $Value){
    echo 
    "{$formData['headline'][$Key]}<br />";
    echo 
    "{$formData['story_id'][$Key]}<br />";
    echo 
    "{$formData['live'][$Key]}<br />";
    }
    }

    //*****************************************
    //THEN If THE submit button on the bottom of this code is hit send an email
    //*****************************************

    if ($_POST['Submit_two']) {
    $your_email 'me@mywebaddressgoeshereitdoes.co.uk';

    $message "Hello";

    //This bit is the problem bit how do I access these values to send in an email when Submit_two is pressed
    if (Array_Key_Exists('Submit'$formData)) {
    foreach(
    $formData['headline'] as $Key => $Value){
    $message .= "{$_SESSION['formData']['headline'][$Key]}";
    $message .= "{$_SESSION['formData']['story_id'][$Key]}";
    $message .= "{$_SESSION['formData']['live'][$Key]}";
    }
    }

    $message .= "Many Thanks";

    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers  "From: My Name<shhssdad@dhsdhhsd.com>\r\n";

    mail($your_email"This is the headline"$message$headers); 
      
      echo 
    "Sent";
     
    }

    ?>

    <form action="" method="post" name="form" id="form">
    <button  name="Submit_two" type="submit" value="Submit_two">Submit_two</button>
    </form>

  4. #4
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two things:
    First, when you set your email headers, you left out the '.' before the last header, emptying everything you set to it before.

    PHP Code:
    $headers  'MIME-Version: 1.0' "\r\n";
    $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
    $headers .= "From: My Name<shhssdad@dhsdhhsd.com>\r\n"//<-You need .= , not just = 

    Secondly, I believe the problem you're having is because you are posting back to test.php, so at the top of your script the formData variable of your $_SESSION array is being set back to the posted data.

    Try using this:

    PHP Code:
    if(isset($_POST['Submit']))
    {
        
    $_SESSION['formData'] = $_POST

    That will make it so you only set the formdata if you post from the first form, because the submit button in the second one is named differently.

    Also, you're going to need to walk through the $_SESSION['formData'] variable when printing to the email.

    PHP Code:
    if (Array_Key_Exists('Submit'$_SESSION['formData'])) {
    foreach(
    $_SESSION['formData']['headline'] as $Key => $Value){
    $message .= "{$_SESSION['formData']['headline'][$Key]}";
    $message .= "{$_SESSION['formData']['story_id'][$Key]}";
    $message .= "{$_SESSION['formData']['live'][$Key]}";
    }


  5. #5
    Non-Member
    Join Date
    Feb 2005
    Posts
    737
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all that seems to have worked a treat. :-)

  6. #6
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad to hear it. Good luck with the rest of your coding!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •