cant figure this one out

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

cant figure this one out

Post by revocause »

Hi ,
im new to sessions and cant figure this out:

I have javascript select menus (approximately 5 to 8 each page version)
I need their values to be passed to sessions so that i can use them on the next page OR
I would like to post them back to the same page showing what they had selected WITH the total input boxes filled with the
php math function.

so for example:
select menu 1 example

Code: Select all

<form action="page2.php" method="post">

<select name="quantity[]" id="b1">
  <option>Please Select </option>
  <option value="500">500</option>
  <option value="1000">1000</option>
  <option value="1500">1500</option>
 </select>

 <br>br>

<select name="price[]"  id="select2" onchange="this.form.submit()"> 
  <option>Please Select </option>
  <option value="1.20">light</option>
  <option value="1.39">medium</option>
  <option value="1.42">heavy</option>
</select>

i want the selected values to be passed into sessions , so that page 2 can output (echo perhaps) the $total, $quantity and (individual) $price.

I'm not sure how to get those user-defined values and have checked google front to back with only seeing 'mail form' sessions blah blah.
anyway, this is how i tried to set those valuesin sessions.

page1.php
-----------

Code: Select all

<?php
session_start();
$_SESSION['quantity'];
$_SESSION['sizeprice'];
?>
and the sessions on page2.php
--------------

Code: Select all

<?php
session_start();
?>
with page2.php having the echo like this:

Code: Select all

<input name="findval" value="<?php echo $_SESSION['quantity'] ?>" size="100">
if you can help me figure out how to pass those javascript values in a simple less-code way that would be super freakishly great!




The Ninja Space Goat | I have edited your post to use the proper [syntax] tags :)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I'm not sure what javascript has to do with anything here (other than submitting the form). Your values will be in the superglobal array $_POST (if you had used the "get" method, it would have been in $_GET). So, you'd do this...

Code: Select all

<?php
session_start();

if (isset($_POST['quantity'], $_POST['sizeprice'])) {

    $_SESSION['quantity'] = $_POST['quantity'];

    $_SESSION['sizeprice'] = $_POST['sizeprice'];

}

?>
also, never output user input without at least escaping it.

Code: Select all

<input name="findval" value="<?php echo htmlentities($_SESSION['quantity']); ?>" size="100">
Why? Because somebody could put in something like "><script>/* any horrendously terrible, cookie-stealing script I want */</script><

Always escape output - especially if it's coming in DIRECTLY from the user.
revocause
Forum Commoner
Posts: 94
Joined: Tue May 29, 2007 9:37 am

Thanks

Post by revocause »

Thanks a lot , I'll try that out.

and thanks for the tip on the escape.

you the man . lol
Post Reply