Jump to content

Recommended Posts

Can  anyone help me complete this script I can get it working with using an implode function, but for the purpose of knowledge I want to not use it.

 

I just want it to echo back the corresponding checkboxes, but I can't get the echoing correct, it echoes back array not the value?

 

<html>
<title>What do you want on your Tombstone?</title>

<body>

<?php

$Output_form = true;
if (isset($_POST['submit'])) 
{
if (empty($_POST['toppings'])) 
{ 

	echo '<b>You forgot the toppings for your Tombstone.</b>';
		echo '<br>';
		echo '<br>';

} else {

	echo '<p><b>This is what you are getting on your Tombstone: ';
(array)$_POST['toppings'];
	echo $toppings;
	echo '!';
	echo '</p></b>';
	$Output_form = false;
}
}

if ($Output_form)
{ 
?>

<form action="ProcessToppings.php" method="post">
<p><i><h2>What do you want on your Tombstone?</h2></i></p>
<p>
<input type="checkbox" name="toppings[]" value="pepperoni" /> Pepperoni<br />
<input type="checkbox" name="toppings[]" value="sausage" /> Sausage<br />
<input type="checkbox" name="toppings[]" value="mushrooms" /> Mushrooms<br />
<input type="checkbox" name="toppings[]" value="cheese" /> Cheese<br />
<input type="checkbox" name="toppings[]" value="olives" /> Olives</p>
<p>
<input type="submit" value="Make that Peet-Za!" name="submit" /></p>

</form>

<?php 
} 

?>

</body>
</html>

Use foreach() to iterate over each element in the array

 

    echo '<p><b>This is what you are getting on your Tombstone: ';
    foreach($_POST['toppings'] as $topping)
    {
        echo " - $topping<br>\n";
    }
    echo '</p></b>';

Since you're trying to access a $toppings variable that wasn't assigned to, or otherwise initialized, it looks like you have register_globals on, which is a very big security risk.  In fact, register_globals is turned off by default in PHP5+ and only exists for backwards compatibility.  I also believe that register_globals will be removed from PHP altogether relatively soon.

 

Also, you don't need to cast $_POST['toppings'] as an array.  PHP already turned it into an array because you named each check box toppings[] in your HTML form.

 

All that said, what you want to do is fairly trivial:

 

$implodedToppings = '';

foreach($_POST['toppings'] as $value)
{
   $implodedToppings .= '$value, ';
}

$implodedToppings = substr($implodedToppings, 0, -2); // get rid of the extra comma and space at the end

echo $implodedToppings;

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.