Welcome to WebmasterWorld Guest from 54.145.246.183

Forum Moderators: coopster & jatar k

Only input from last checkbox

Array of checkboxes should display from form

   
1:58 pm on May 16, 2012 (gmt 0)



I'm a total NOOB at programming, and my client wants to be able to change the "Soup of the Day" on his website with a form. Sounded simple. Check off which soups are available with the form, and have PHP dynamically WRITE over the page each time.

Simple form (tweaked later)

<html>
<head>
</head>
<body>

<form action="checkbox-form.php" method="post">
Soup #1<input type="checkbox" name="soup[]" value="soup1" /><br />
Soup #2<input type="checkbox" name="soup[]" value="soup2" /><br />
Soup #3<input type="checkbox" name="soup[]" value="soup3" /><br />
Soup #4<input type="checkbox" name="soup[]" value="soup4" /><br />
<input type="submit" name="formSubmit" value="Submit" />
</form>




</body>
</html>


FORM HANDLER



<?php

$soupFile = "zoups.html";
$fh = fopen($soupFile, 'w') or die("can't open file");
$stringData = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
fwrite($fh, $stringData);
$stringData = "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n";
fwrite($fh, $stringData);
$stringData = "<head>\n";
fwrite($fh, $stringData);
$stringData = "<meta http-equiv=\"Content-Type\" content=\"text/html\; charset=utf-8\" />\n";
fwrite($fh, $stringData);
$stringData = "<title>Soups of the Day</title>\n";
fwrite($fh, $stringData);
$stringData = "<style type=\"text/css\">\n";
fwrite($fh, $stringData);
$stringData = "<!--\n";
fwrite($fh, $stringData);
$stringData = "body {\n";
fwrite($fh, $stringData);
$stringData = "background-color: #954A00;\n";
fwrite($fh, $stringData);
$stringData = "}\n";
fwrite($fh, $stringData);
$stringData = "body,td,th {\n";
fwrite($fh, $stringData);
$stringData = "color: #D5BB9F;\n";
fwrite($fh, $stringData);
$stringData = "}\n";
fwrite($fh, $stringData);
$stringData = "-->\n";
fwrite($fh, $stringData);
$stringData = "</style></head>\n";
fwrite($fh, $stringData);

$stringData = "<body>\n";
fwrite($fh, $stringData);

$stringData = "<h1>Featured soups of the day</h1>\n";
fwrite($fh, $stringData);

foreach ($_POST['soup'] as $key => $value) {
echo "Your soup choices are: $value<br>";
}
$stringData = "$value<br>";
fwrite($fh, $stringData);

$stringData = "</body>\n";
fwrite($fh, $stringData);
$stringData = "</html>\n";
fwrite($fh, $stringData);




?>



Submitting displays the choices, but the final web page only shows the last box selected. It has to be something simple that I'm missing, but what?

Thanks for any help.
3:24 pm on May 16, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Welcome aboard Dweebert, your actual write to the file is outside the foreach.

foreach ($_POST['soup'] as $key => $value) {
echo "Your soup choices are: $value<br>";
}
$stringData = "$value<br>"; // <-- only has last selected value
fwrite($fh, $stringData);

Second (and only partially related), this is far more verbose than it needs to be, and is in effect hammering your server's HD by executing multiple writes to disk when it only needs one. You're writing line by line and can do this:

<?php
$soupFile = "zoups.html";

$stringData = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html\; charset=utf-8\" />
<title>Soups of the Day</title>
<style type=\"text/css\">
body { background-color: #954A00; }
body,td,th { color: #D5BB9F; }
ul li { list-style:none; }
</style></head>
<body>
<h1>Featured soups of the day</h1>
<p>Your soup choices are:</p><ul>
" ; // <--- store everything to HERE in $stringData

foreach ($_POST['soup'] as $key => $value) {
$stringData .= "<li>$value</li>\n"; // Add to $stringData inside the loop
}

$stringData .= "</ul></body></html>"; // add last bit to $stringData

$fh = fopen($soupFile, 'w') or die("can't open file");
fwrite($fh, $stringData); // output to file ONCE, easier on the server/HD
fclose($fh); // always close file handles, freeing memory
?>

Note the concatenation operator, the dot. It allows you to add strings together:

$var = 'this';
$varble = 'that';

echo $var . ' and ' . $varble; // echoes this and that
$var .= ' and ' . $varble;
echo $var; // Same thing, but now we've changed the value of $var

So you compile the entire first part of the page as a string, then add to it as needed, then write to the file once.

A couple semantic changes to the output as well. :-)
8:10 pm on May 16, 2012 (gmt 0)



Thank You so much! That makes so much more sense now. I had read somewhere that each line had to be written one at a time, and stuck with that. Cheers!
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month