Jump to content

Some problems with a SESSION


Sunamena
 Share

Recommended Posts

Greetings all,

What i want to achieve is the following:
- Visitors are browsing products. Each product has an unique ID.
- When a visitor wants more info on a product, he or she clicks on it and sees more information about the product. The ID of the product must be saved in the SESSION.

I did this as shown below. I used part of the URL (since the ID is a number, i cut all the non number characters). This worked perfectly fine, untill i use a form (this one:

<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">

)

on that page.

Idealy i would love to have an onclick:<?php $_SESSION['zoekertjesid'] = $zoekertjesid ?>, which i would then implement in my foreach code.
I tried this, but all it did was making my SESSION ID and the content i received that of the last ID generated (because the on click is done AFTER the foreach is completely finished.

I would apreciate it very much if i can get some advise here.


Below you can see the relevant code (i left out the parts of the pages that i deemed irrelevant).

 

 

Page "zoeken"

 

foreach ($result as $row) {
// Declareren van variabelen, dit gaat mijn werk wat vergemakkelijken
$zoekertjesid = $row['zoekertjesid'];
$gebruikersid = $row['gebruikersid'];
$titel = $row['titel'];
$omschrijving = $row['omschrijving'];
$rubriek = $row['rubriek'];
$prijs = $row['prijs'];
$aantalAfbeeldingen = $row['aantalAfbeeldingen'];
?>
<div class="productdetails">
<h2 class="titel"><?php echo $titel ?></h2>
<div>
<img src="images/<?php echo $zoekertjesid; ?>/thumbs/thumb_<?php echo $zoekertjesid; ?>_1.jpg">
<h5>Omschrijving</h5>
<p><?php echo $omschrijving ?></p>
<h5>Vraagprijs</h5>
<p><?php echo $prijs ?> EUR</p>
<p>
<a href="meerInfo.php?zoekertjesid=<?php echo $zoekertjesid; ?>">Meer info</a>
</p>
</div>
</div>
<?php
}



Page "meerinfo"
<?php
session_start();
include "dbconnectie.php";
if(!empty($_SESSION["gebruikersid"])){
// TEST echo "Welkom " . $_SESSION["gebruikersnaam"] . "<br>";
// TEST echo "<a href='afmelden.php'>Afmelden</a>";
}
else {
header("Location: error.php");
}
// TEST print_r($_SERVER);
// TEST echo "<br><br><br>" . $_SERVER['QUERY_STRING'];
$maakZoekertjesID = $_SERVER['QUERY_STRING'];
$_SESSION['zoekertjesid'] = preg_replace("/[^0-9,.]/", "", $maakZoekertjesID);
//TEST echo "<br><br><br>". $_SESSION['zoekertjesid'];
?>

 

Link to comment
Share on other sites

I can't find the relation between the code you've shown and the question you're asking. There isn't a form tag or form processing code.

 

A session variable shouldn't be necessary if you have one form for each item. Just add the ID as a hidden input. <input type="hidden" name="zoekertjesid" value="<?php echo $zoekertjesid; ?>">

 

You should leave the form action attribute empty rather than using PHP_SELF, then it will preserve query string values and you can use $_GET to find out which ID is being used.

Link to comment
Share on other sites

I can't find the relation between the code you've shown and the question you're asking. There isn't a form tag or form processing code.

 

A session variable shouldn't be necessary if you have one form for each item. Just add the ID as a hidden input. <input type="hidden" name="zoekertjesid" value="<?php echo $zoekertjesid; ?>">

 

You should leave the form action attribute empty rather than using PHP_SELF, then it will preserve query string values and you can use $_GET to find out which ID is being used.

 

 

There are no form elements involved.

 

It is a hyperlink ( <a href="meerInfo.php?zoekertjesid=<?php echo $zoekertjesid; ?>">Meer info</a> ). When someone clicks it, the variable $zoekertjesid should be added to the $_SESSION['zoekertjesid'].

 

 

 

The page where you are directed to, gives product specifications.

 

The Session starts like this:

 

<?php
session_start();
include "dbconnectie.php";
if(!empty($_SESSION["gebruikersid"])){
// TEST echo "Welkom " . $_SESSION["gebruikersnaam"] . "<br>";
// TEST echo "<a href='afmelden.php'>Afmelden</a>";
}
else {
header("Location: error.php");
}
$maakZoekertjesID = $_SERVER['QUERY_STRING'];
$_SESSION['zoekertjesid'] = preg_replace("/[^0-9,.]/", "", $maakZoekertjesID);
?>
The green text is how i currently put the "zoekertjesid" into the $_SESSION.
This works, but when completing this form:
<h5>Doe een bod</h5>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="bod">Uw bod: </label>
<input type="number" name="bod">
</p>
<p>
<input type="submit" value="Verzenden" name="submit"> </p>
</form>
<?php
/*
if(isset($_POST["submit"])) {
$sql = "INSERT INTO gastenboek (zoekertjesid, gebruikersid, bod, datum) VALUES (:zoekertjesid, :gebruikersid, :bod, :datum)";
$stmt = $db -> prepare($sql);
$stmt -> bindParam(':zoekertjesid', $zoekertjesidbod, PDO::PARAM_STR);
$stmt -> bindParam(':gebruikersid', $gebruikersidbod, PDO::PARAM_STR);
$stmt -> bindParam(':bod', $bod, PDO::PARAM_STR);
$stmt -> bindParam(':datum', $datum, PDO::PARAM_STR);
$zoekertjesidbod = $_SESSION['zoekertjesid'];
$gebruikersidbod = $_SESSION['gebruikersid'];
$bod = htmlentities($_POST['bod']);
$datum = date("Y-m-d");
$stmt -> execute();
}
*/
It gives errors (since it updates the URL, and the zoekertjesid, thus not finding your product)
I hope my question is now a little more clear =D

 

And thankyou so much for always supporting and helping me and other people on this forum! =D

I think you would be a great teacher.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...