Welcome Guest, Not a member yet? Register   Sign In
Procedural PHP and $_SESSION issue
#1

[eluser]Bramme[/eluser]
Hey all, I've been asked to code a backend for a website. However, they need some things online as fast as possible and seeing as the whole page is in pure html for the moment, I decided to whip up some quick PHP pages while I work on the backend.

However, I've hit a snag: there's an order form on the website that I want to validate at least a little. But I'm having a problem with repopulating the fields...

Here's my code:

order page:
Code:
<?php
session_start();
print_r($_SESSION['bestteling']);
function echosession($key, $second = NULL) {
    if(isset($second)) {
        $value = !empty($_SESSION['bestelling'][$key][$second]) ? $_SESSION[$key][$second] : "";
    } else {
        $value = !empty($_SESSION['bestelling'][$key]) ? $_SESSION[$key] : "";
    }
    return $value;
}
$error = isset($_GET['error']) ? "<p style=\"color: #f00\">Bovenstaande velden zijn verplicht in te vullen!</p>" : "";
?&gt;

&lt;!-- some html, example of input --&gt;
&lt;input type="text" name="email" id="email" value="&lt;?php echo echosession('email'); ?&gt;" /&gt;

My orderconfirmation page looks like this:
Code:
&lt;?php
session_start();
if(isset($_POST)) {
    $bestelling['naam'] = !empty($_POST['naam']) ? $_POST['naam'] : "";
    $bestelling['email'] = !empty($_POST['email']) ? $_POST['email'] : "";
    $bestelling['telefoon'] = !empty($_POST['telefoon']) ? $_POST['telefoon'] : "";
    $bestelling['W1']['naam'] = "chiaccianoci 2006";
    $bestelling['W1']['prijs'] = 6.25;
    $bestelling['W1']['hoeveelheid'] = !empty($_POST['W1']) ? $_POST['W1'] : 0;
    $bestelling['W2']['naam'] = "Ile la Forge Merlot 2007";
    $bestelling['W2']['prijs'] = 8;
    $bestelling['W2']['hoeveelheid'] = !empty($_POST['W2']) ? $_POST['W2'] : 0;

    // .... add stuff to the "bestelling" array
    $_SESSION['bestelling'] = $bestelling;
    
    if(empty($bestelling['naam']) OR empty($bestelling['email']) OR empty($bestelling['telefoon'])) {
        header("Location: wijnbestellen.php?error=verplicht");
    }
}

I thought that this would repopulate the fields as $_SESSION should be accessible across both pages. However, the moment I use the header() function to redirect to the previous page, my session array is unset Sad
#2

[eluser]Bramme[/eluser]
Okay nevermind, just noticed a stupid mistake in my echosession function. It's fixed now and works perfectly. I had a typo in my print_r too, so that's why I thought the session was being unset. Silly me!




Theme © iAndrew 2016 - Forum software by © MyBB