Jump to content

Newbie needs help with sticky forms


mattrd

Recommended Posts

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!

Link to comment
Share on other sites

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; ?>">

Link to comment
Share on other sites

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>";
}
?>

Link to comment
Share on other sites

<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>

Link to comment
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.