Cyjm1120 Posted November 14, 2014 Share Posted November 14, 2014 Hello everyone, I am working on a form that is similar to a shopping cart system and I am thinking of creating a button that submits the checked value and saves them to a $_SESSION variable. And also a link that links to a cart.html that takes the values of a $_SESSION variable. I am have trouble figuring what tag/attribute should I use in order to achieve that. Right now my code attached below submits the checked values to cart.html directly. However I want my submit button to save the checked box to a $_SESSION variable and STAY on the same page. And then I will implement a <a> to link to the cart.php. I researched a little bit about this subject and I know it's somewhat related to ajax/jquery. I just wanted to know more about it from you guys. I appreciate your attention for reading the post and Thanks! Below is the form that I currently have: <form name= "finalForm" method="POST" action="cart.php"> <input type="Submit" name="finalSelected"/> <?php foreach($FinalName as $key => $item) {?> <tr> <td><input type="checkbox" name="fSelected[]" value="<?php echo htmlspecialchars($FinalID[$key])?>" /> <?php echo "$FinalID[$key] & $item";?> </td> </tr> <?php } ;?> Below is the code for cart.php <?php require ('connect_db.php'); if(isset($_POST['finalSelected'])) { if(!empty($_POST['fSelected'])) { $chosen = $_POST['fSelected']; foreach ($chosen as $item) echo "aID selected: $item </br>"; $delimit = implode(", ", $chosen); print_r($delimit); } } if(isset($delimit)) { $cartSQL = "SELECT * from article where aID in ($delimit)"; $cartQuery = mysqli_query($dbc, $cartSQL) or die (mysqli_error($dbc)); while($row = mysqli_fetch_array($cartQuery, MYSQLI_BOTH)) { $aTitle[] = $row[ 'name' ]; } } ?> <table> <?php if(isset($delimit)) { $c=0; foreach($aTitle as $item) {?> <tr> <td> <?php echo $aTitle[$c]; $c++;?> </td> </tr> <?php }}?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/292470-how-to-create-a-form-with-a-submit-button-that-saves-the-checked-box-to-a-_session-and-create-a-link-to-a-different-php-that-takes-on-the-_session/ Share on other sites More sharing options...
Solution NotionCommotion Posted November 15, 2014 Solution Share Posted November 15, 2014 I know it is totally unrelated to your question, but I recommend not opening and closing PHP tags as often. Maybe there is some performance savings (or maybe the opposite), but any potential savings will greatly pale to the lost time in your life trouble shooting it (if others disagree, please comment). foreach($FinalName as $key => $item) { echo('<tr><td><input type="checkbox" name="fSelected[]" value="'.htmlspecialchars($FinalID[$key]).'" />'.($FinalID[$key] & $item).'</td></tr>'); } As for your specific question, I would probably do one of the following: Don't use Ajax, but submit each item to your server, and have it update your session value and send back the appropriate HTML. Don't use Ajax but just JavaScript (or jQuery which is JavaScript for noobs like me) to add hidden inputs on the existing page. If you do wish to use Ajax to update your session, you will need to send with it the session ID so that the server knows the session file to update. Quote Link to comment https://forums.phpfreaks.com/topic/292470-how-to-create-a-form-with-a-submit-button-that-saves-the-checked-box-to-a-_session-and-create-a-link-to-a-different-php-that-takes-on-the-_session/#findComment-1496624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.