Jump to content

Recommended Posts

How can I get an output where it only shows the name of the drink and quantity where quantity > 0 ?

<?php
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
        "http://www.w3.org/TR/html4/strict.dtd">
<html lang="nl">


<html>
    <form method="post" action="<?php echo $PHP_SELF;?>">
        
<?php

$drinks = array("cola", "fanta", "bier", "koffie", "thee", "vodka");


$i = 0;

echo "<table>";
while ($drinks[$i]) {
    $listnaam = $drinks[$i] . "_aantal";
    $optionlist = "<select name= '$listnaam' ><option>0</option><option>1</option><option>2</option><option>3</option></select>";

    echo "<tr><td >" . $drinks[$i] . "</td>";
    echo "<td>" .$optionlist . "</td></tr>";
   
    $i++;
}

echo "</table>";



?>

   <input type="submit" value="Toon Output" name="submit"/>
    </form>
<?php
}
else {echo $drinks;}

?>    
    

</html>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/229995-form-problem/
Share on other sites

Don't double post.

 

Now that we've gotten that out of the way, try this and see if it's what you're looking to do:

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="nl">
<html>
<body>
<?php
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
echo '<form method="post" action="">';

$drinks = array("cola", "fanta", "bier", "koffie", "thee", "vodka");
echo "<table>";
foreach( $drinks as $v ) {
$optionlist = "<select name= \"drinks[$v]\">\n";
for( $i = 0; $i < 4; $i++ ) {
	$optionlist .= "<option value=\"$i\">$i</option>\n";
}
$optionlist .= "</select>\n";
echo "<tr><td >$v</td>";
echo "<td>$optionlist</td></tr>";
}
echo "</table>";
?>
<input type="submit" value="Toon Output" name="submit"/>
</form>
<?php
} else {
$selected = array();
if( is_array($_POST['drinks']) ) {
	foreach( $_POST['drinks'] as $k => $v ) {
		if( $v > 0 ) {
			$selected[] = "Drink: $k - Number: $v";
		}
	}
	echo implode( '<br>', $selected );
} else {
	echo "Form error: Non-array passed on line: " . __LINE__;
}
}
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/229995-form-problem/#findComment-1184692
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.