Jump to content

form problem


andrej13

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.