mattrd Posted April 10, 2008 Share Posted April 10, 2008 I have a form, that when filled out, generates a mad lib followed by a custom paragraph based on how you filled out the form. I want the form to have preset values. This is all works fine, but if I change the preset value and then generate the madlib the page reloads with the preset values there again. How can I have preset values, yet make the form retain the custom answers I plug in after I generate the madlib? you can see the page here: http://matthewrdaniels.com/php/form_validation2.php I'm still very new to php. Any help would be greatly appreciated. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/ Share on other sites More sharing options...
Psycho Posted April 10, 2008 Share Posted April 10, 2008 Well, it would have been helpful if you had provided code. Since you didn't I will only offer some mock code to show one method. There may be a more efficient method based upon your existing code and functionality. <?php $field1value = (!empty(trim($_POST['field1']))) ? trim($_POST['field1']) : "Default for field 1"; ?> <input type="text" name="field1" value="<?php echo $field1value; ?>"> Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/#findComment-513834 Share on other sites More sharing options...
obsidian Posted April 10, 2008 Share Posted April 10, 2008 Just an FYI, you don't need to do a trim within the empty() call, since empty interprets empty space as well. Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/#findComment-513838 Share on other sites More sharing options...
mattrd Posted April 10, 2008 Author Share Posted April 10, 2008 sorry about not posting code, here is the php code: <?php if (isset($_POST['submitted'])) { #define variables for the mad lib and write out an error message if any fields in the form were left blank #disable magic quotes using the stripslashes function #remove left and right spaces if they are entered in the form using the trim function. if (!empty($_POST['1'])) { $one = trim(stripslashes($_POST['1'])); } else { $one = NULL; echo "<span style=\"color:#ff0000;\">You didn't enter a plural noun!</span><br />"; } if (!empty($_POST['2'])) { $two = trim(stripslashes($_POST['2'])); } else { $two = NULL; echo "<span style=\"color:#ff0000;\">You didn't enter an adjective!</span><br />"; } if (!empty($_POST['3'])) { $three = trim(stripslashes($_POST['3'])); } else { $three = NULL; echo "<span style=\"color:#ff0000;\">You didn't enter the first noun!</span><br />"; } if (!empty($_POST['4'])) { $four = trim(stripslashes($_POST['4'])); } else { $four = NULL; echo "<span style=\"color:#ff0000;\">You didn't enter the second noun!</span><br />"; } if (!empty($_POST['5'])) { $five = trim(stripslashes($_POST['5'])); } else { $five = NULL; echo "<span style=\"color:#ff0000;\">You didn't enter the third noun!</span><br />"; } #place the mad lib inside of a variable #include and concatenate the previous defined variables $madlib = "Cinderella had two step-" . $one .", who were very " . $two . " to her. So her fairy god" . $three . " turned her pumpkin into a big " . $four . ", and dragged her off to the " . $five . "."; #write an if statement to display the madlib if filled out correctly, but display an error message if any fields in the form were left blank. if (($one == NULL) || ($two == NULL) || ($three == NULL) || ($four == NULL) || ($five == NULL)) { echo "<p style=\"color:#ff0000;\">Please fill out the form completely.</p>"; } else { echo $madlib; } #print out a custom paragraph after the mad lib but do not print anything if one or more of the fields in the form were left blank. if (($one == NULL) || ($two == NULL) || ($three == NULL) || ($four == NULL) || ($five == NULL)) { echo NULL; } else { if (($one == "watermelons") && ($two == "smelly") && ($three == "toilet") && ($four == "fanny") && ($five == "poop")) { echo "<p><em>\"Hey! You copied off me!! And now, I shall kill you!!\"</em></p>"; } elseif (($one == "watermelons") || ($two == "smelly") || ($three == "toilet") || ($four == "fanny") || ($five == "poop")) { echo "<p><em>\"Still using some of my words... get your own!!\"</em></p>"; } elseif (($one == "sisters") && (($two == "mean") || ($two == "evil") || ($two == "nasty")) && ($three == "mother") && ($four == "carriage") && (($five == "ball") || ($five == "castle"))) { echo "<p><em>\"Oh, you did it the way it's supposed to go... that's not very funny now is it?\"</em></p>"; } else { echo "<p><em>\"Haha! Oh, how ruthlessly absurd.\"</em></p>"; } } } else { echo "<p>Stewie's Mad Lib</p> <p>Do the same Cinderella Mad Lib that Stewie did in Family Guy! The text boxes are already filled in with the words he used on the show, but you can change them to your own words if you like. Have fun!</p> <form action=\"form_validation3.php\" method = \"post\"> <table border=\"0\"> <tr><td>Enter a plural noun</td><td><input name=\"1\" type=\"text\" value=\"watermelons\" /></td></tr> <tr><td>Enter an adjective</td><td><input name=\"2\" type=\"text\" value=\"smelly\" /></td></tr> <tr><td>Enter a noun</td><td><input name=\"3\" type=\"text\" value=\"toilet\" /></td></tr> <tr><td>Enter another noun</td><td><input name=\"4\" type=\"text\" value=\"fanny\" /></td></tr> <tr><td>Enter another noun</td><td><input name=\"5\" type=\"text\" value=\"poop\" /></td></tr> <tr colspan=\"2\"><td><input type=\"submit\" value=\"Let's see my Mad Lib!\" /></td></tr> <input type=\"hidden\" name=\"submitted\" value=\"TRUE\" /> </table> </form>"; } if (isset($_POST['submitted'])) { echo "<a href=\"form_validation3.php\">go back</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/#findComment-513846 Share on other sites More sharing options...
mattrd Posted April 10, 2008 Author Share Posted April 10, 2008 tried using mjdamato's code and it spit out an error Fatal error: Can't use function return value in write context in /home/mattrd/public_html/php/form_validation2.php on line 11 can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/#findComment-513860 Share on other sites More sharing options...
Psycho Posted April 10, 2008 Share Posted April 10, 2008 <html> <head></head> <body> <?php #set default values $one = 'watermellons'; $two = 'smelly'; $three = 'toilet'; $four = 'fanny'; $five = 'poop'; if (isset($_POST['submitted'])) { #define variables for the mad lib #disable magic quotes using the stripslashes function #remove left and right spaces if they are entered in the form using the trim function. $one = trim(stripslashes($_POST['1'])); $two = trim(stripslashes($_POST['2'])); $three = trim(stripslashes($_POST['3'])); $four = trim(stripslashes($_POST['4'])); $five = trim(stripslashes($_POST['5'])); #set error message if any fields in the form were left blank $errors = array(); if (empty($_POST['1'])) { $errors[] = "You didn't enter a plural noun!"; } if (empty($_POST['2'])) { $errors[] = "You didn't enter an adjective!"; } if (empty($_POST['3'])) { $errors[] = "You didn't enter the first noun!"; } if (empty($_POST['4'])) { $errors[] = "You didn't enter the second noun!"; } if (empty($_POST['5'])) { $errors[] = "You didn't enter the third noun!"; } #write an if statement to display the madlib if filled out correctly, but display an error message if any fields in the form were left blank. if (count($errors)) { foreach ($errors as $thisError) { echo "<span style=\"color:#ff0000;\">$thisError</span><br />";; } echo "<p style=\"color:#ff0000;\">Please fill out the form completely.</p>"; } else { #place the mad lib inside of a variable #include and concatenate the previous defined variables $madlib = "Cinderella had two step-" . $one .", who were very " . $two . " to her. So her fairy god" . $three . " turned her pumpkin into a big " . $four . ", and dragged her off to the " . $five . "."; echo $madlib; #print out a custom paragraph after the mad lib but do not print anything if one or more of the fields in the form were left blank. if (($one == "watermelons") && ($two == "smelly") && ($three == "toilet") && ($four == "fanny") && ($five == "poop")) { echo "<p><em>\"Hey! You copied off me!! And now, I shall kill you!!\"</em></p>"; } elseif (($one == "watermelons") || ($two == "smelly") || ($three == "toilet") || ($four == "fanny") || ($five == "poop")) { echo "<p><em>\"Still using some of my words... get your own!!\"</em></p>"; } elseif (($one == "sisters") && (($two == "mean") || ($two == "evil") || ($two == "nasty")) && ($three == "mother") && ($four == "carriage") && (($five == "ball") || ($five == "castle"))) { echo "<p><em>\"Oh, you did it the way it's supposed to go... that's not very funny now is it?\"</em></p>"; } else { echo "<p><em>\"Haha! Oh, how ruthlessly absurd.\"</em></p>"; } } } echo "<p>Stewie's Mad Lib</p> <p>Do the same Cinderella Mad Lib that Stewie did in Family Guy! The text boxes are already filled in with the words he used on the show, but you can change them to your own words if you like. Have fun!</p> <form action=\"$_SERVER[php_SELF]\" method = \"post\"> <table border=\"0\"> <tr><td>Enter a plural noun</td><td><input name=\"1\" type=\"text\" value=\"$one\" /></td></tr> <tr><td>Enter an adjective</td><td><input name=\"2\" type=\"text\" value=\"$two\" /></td></tr> <tr><td>Enter a noun</td><td><input name=\"3\" type=\"text\" value=\"$three\" /></td></tr> <tr><td>Enter another noun</td><td><input name=\"4\" type=\"text\" value=\"$four\" /></td></tr> <tr><td>Enter another noun</td><td><input name=\"5\" type=\"text\" value=\"$five\" /></td></tr> <tr colspan=\"2\"><td><input type=\"submit\" value=\"Let's see my Mad Lib!\" /></td></tr> <input type=\"hidden\" name=\"submitted\" value=\"TRUE\" /> </table> </form>"; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/100480-newbie-needs-help-with-sticky-forms/#findComment-513920 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.