Jump to content

Recommended Posts

Hi guys, I have stuck with this issue for over 3 hours now. My remove all button just wouldn't work.

 

Can anyone tell me what's the issue? I have 2 pages, one parent and another pop-up page that takes the values (cartSubmit) from parent. And I wrote out the code for both pages but I just cannot get it to work. 

I apologize in advanced if my code looks messy or doesn't make any sense.

 

Please take a look at the code and let me know if you could spot anything wrong with it. Any help is greatly appreciated.

 

Here is the code related to the remove all button in the parent page:

if(isset($_POST['finalSelected']))
{	echo "<br/>inside";
	if(!empty($_POST['fSelected']))
	{	echo "<br/>if NOT empty";
		$chosen = $_POST['fSelected'];
		foreach ($chosen as $item)
			echo "</br>aID selected: $item </br>";
		//implode function to transform an array to a string
		//chosen(array) to $delimit(String)
		$delimitStr = implode(", ", $chosen);
		//Save the selected String to SESSION
		if(!isset($_SESSION["final"]))
		{	
			$_SESSION["final"] .= "$delimitStr";
		}
		else
		{
			$_SESSION["final"] .= ", $delimitStr";
		}
		//Explode function to transform the SESSION variable back to an array for further trimming.
		$arraySession = explode(', ', $_SESSION["final"]);
		//Array unique to filter out duplicates.
		$uniqueArray = array_unique($arraySession);
		$cartSubmit = implode(", ",$uniqueArray);
		$_SESSION['cartSubmit'] = $cartSubmit;
	}
	else
	{
		echo "<br/>else empty";
		$noSubmit = $_SESSION["final"];
		//Explode function to transform the SESSION variable back to an array for further trimming.
		$arraySession = explode(', ', $_SESSION["final"]);
		//Array unique to filter out duplicates.
		$uniqueArray = array_unique($arraySession);
		$cartSubmit = implode(", ",$uniqueArray);
		$_SESSION['cartSubmit'] = $cartSubmit;
		
	}
}


<form name= "finalForm" method="POST" action="test6-1.php" target = "_self">
<input type="Submit" name="finalSelected"/>
<a href="javascript:popup('test6-3.php?aID=
<?php if(empty($_SESSION['final']) && empty($_SESSION['cartSubmit'])) echo "0";
else if(empty($cartSubmit)) echo "0"; else echo $_SESSION['cartSubmit'];?>')">Cart</a>


<?php if(isset($FinalName)) 
{ 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 }
}?>
	</form>

And here is the code in the pop-up page.

<script> window.onunload = refreshParent;
function refreshParent() 
{
window.opener.location.reload();
}
</script>
if(isset($_POST['removeAll'])) 
{ $aID = "0"; 
unset($_SESSION['cartSubmit']); 
unset($_SESSION['final']); 
header("Location : test6-3.php?aID=0"); 
}
Edited by Cyjm1120
Link to comment
https://forums.phpfreaks.com/topic/292547-remove-all-button-wont-work-properly/
Share on other sites

 

 

Here is the code related to the remove all button in the parent page:

What do you mean by remove all?  What is being removed? Where and when does it need to be removed?

 

Also the code you posted is just weird. You're converting the data submitted by your form from an array into a comma delimited string. Then you explode it back to an array and apply array_unique then you implode it back into a comma delimited string and saving it to the session each time. Why? It will be a lot better to keep it as an array and just apply array unique and then save it the session. Sessions can store any data type not just strings.

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.