Jump to content

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.


Cyjm1120
Go to solution Solved by NotionCommotion,

Recommended Posts

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>

Link to comment
Share on other sites

  • Solution

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:

 

  1. Don't use Ajax, but submit each item to your server, and have it update your session value and send back the appropriate HTML.
  2. 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.