
ctapp
New Members-
Posts
3 -
Joined
-
Last visited
ctapp's Achievements

Newbie (1/5)
0
Reputation
-
Sticky form help; PHP code is showing in HTML form
ctapp replied to ctapp's topic in PHP Coding Help
Eck! Sorry the picture is so big, I've been working on this and my jQuery class for almost 6 hours...starting to get loopy! time to get help from the pros! -
Sticky form help; PHP code is showing in HTML form
ctapp replied to ctapp's topic in PHP Coding Help
Thank you so much Jacques1! I was able to clean things up (even though i'm not too clear on how not mix a little PHP with HTML when the data for the variables come from the user's input) and got everything to be 'sticky'. Unfortunately, I have a new problem with the confirmation function. I am using an if statement in the function. When I load it to the server, I get an error that there is an undefined variable in the if statement. I declared the variables in the previous function. I thought that if a variable is declared and given a value, it holds that value until changed. Am I wrong in this? Or can't I pass multiple conditionals as parameters? I'm not sure what I am doing wrong! I have attached my updated code along with a screen shot, <?php ini_set('display_errors',1); error_reporting(E_ALL); include_once 'header.html'; // Create function for a sticky radio element function fluffy($value, $name = 'seen') { echo '<input type="radio" name="'. $name .'" value="'. $value . '"'; // creates radio element if (isset($_POST[$name]) && ($_POST[$name] == $value)) { echo ' checked="checked"'; } //makes radio button sticky echo '>' . $value .' '; // closes radio element } // Create variables and create functions to validate first name, last name, email and if they saw fluffy function validation () { if($_SERVER['REQUEST_METHOD'] == 'SELF') { $firstname = $_POST['fname']; $lastname = $_POST['lname']; $when = $_POST['when']; $length = $_POST['length']; $seen = $_POST['seen']; $look= $_POST['look']; $do= $_POST['do']; $seen= $_POST['seen']; $comments= $_POST['comments']; $email = $_POST['email']; if (!empty($_POST['fname'])) { $firstname = $_POST['fname']; } else { $firstname = NULL; echo '<p>Sorry, you must give your First Name!</p>';} if (!empty($_POST['lname'])) { $lastname = $_POST['lname']; } else { $lastname = NULL; echo '<p>Sorry, you must give your Last Name!</p>'; } if (!empty($_POST['email'])) { $email = $_POST['email']; } else { $email = NULL; echo '<p>Sorry, you must provide an email address!</p>'; } if (isset($_POST['seen'])) { $seen = $_POST['seen']; } elseif ($seen = "no") { echo '<p>Darn, we really miss him!</p>'; } elseif ($seen = NULL) { echo '<p>Sorry you have not told us if you saw Fluffy or not!</p>' ; } else { $seen == NULL ; echo '<p>Sorry, you did not tell us if you saw Fluffy or not!</p>'; } }; } function confirmation() { if ($firstname && $lastname && $email && $seen == true) { echo "<p>Thanks for submitting the form <emp><b>$firstname $lastname</b></emp>!</p> <p>This is what we got: <br> You were abducted: <b> $when</b> for <b>$length</b></p> <p>You saw <b>$see</b> aliens that appeared <b>$look</b></p> <p>You answered: <b>$seen</b> in regards to seeing Fluffy.</p> <p> Here are your additional comments: <br> $comments</p> <hr> \n"; } } echo confirmation(); ?> <form method="POST" action="" name="abductreport"> <label for="fname">First Name:<b>*</b></label> <input type="text" name="fname" id="fname" placeholder="First Name" value="<?php if(isset($_POST['fname'])) echo $_POST['fname']; ?>"><br> <label for="lname">Last Name:<b>*</b></label> <input type="text" name="lname" id="lname" placeholder="Last Name" value="<?php if(isset($_POST['lname'])) echo $_POST['lname']; ?>"><br> <label for="email">What is your Email address?<b>*</b></label> <input type="email" name="email" id="email" placeholder="Email" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>"><br> <label for="when">When did it happen?</label> <input type="date" name="when" id="when" value="<?php if(isset($_POST['when'])) echo $_POST['when']; ?>"><br> <label for="length">How long were you gone?</label> <input type="text" name="length" id="length" placeholder="days, months, years?" value="<?php if(isset($_POST['length'])) echo $_POST['length']; ?>"><br> <label for="see">How many did you see?</label> <input type="number" name="seen" id="seen" placeholder="Enter a Number" value="<?php if(isset($_POST['seen'])) echo $_POST['seen']; ?>"><br> <label for="look">Describe them:</label> <input type="text" name="look" id="look" placeholder="What was their appearance?" value="<?php if(isset($_POST['look'])) echo $_POST['look']; ?>"><br> <label for="do">What did they do you to?</label> <input type="text" name="do" id="do" placeholder="Describe what they did" value="<?php if(isset($_POST['do'])) echo $_POST['do']; ?>"><br> Have you seen my dog fluffy?<b>*</b> <?= fluffy('Yes'); fluffy('No'); ?> <br><img src="fluffy.jpg" alt="Have you seen Fluffy?"><br> <label for="comments">Anything else you want to Add?</label> <textarea rows="3" cols="50" name="comments" id="comments" placeholder="Anything else you would like to add"> <?php if(isset($_POST['comments'])) echo $_POST['comments']; ?> </textarea><br><br> <input type="submit" value="Report Abduction"> </form> -
Hello all! I am currently taking a PHP/SQL class and I am stumped on one of my assignments. We are building a form and I have a list of requirements for this step of the project. They are: 1. Submits the form to itself. 2. Displays the Alien Abduction Form. Remember, your PHP script should be setup to display this page - do not use a separate HTML form. You should be able to copy and paste your code from the html form lab into a function. 3. Make all the form fields sticky. If there is an error in filling out the form no input data should be lost. It should redisplay the information entered (including radio buttons)along with any appropriate error messages. 4. Display error messages on the html form (don't take the user to a separate error page). 5. Display a confirmation page once all information has been filled in and submitted. At this point the form should not redisplay. I am at the point where I need to make the form fields sticky. I completed an exercise with a MPG calculator to make the fields sticky and that exercise will run correctly from my server. So my logic is the code that worked for one field would work in another. But, when I place the code in my assignment, the php code shows in the browser. I have looked at php.net for help with what would cause this and a few other forums but I can't seem to find this happening to anyone else. I have attached a screenshot of what I am getting along with the code. Thanks in advanced for any help and advice. <?php ini_set('display_errors',1); error_reporting(E_ALL); $page_title = 'Alien Abduction Form'; include ('header.html'); // Create post variables if($_SERVER['REQUEST_METHOD'] == 'POST') { $firstname = $_POST['fname']; $lastname = $_POST['lname']; $when = $_POST['when']; $length = $_POST['length']; $see = $_POST['see']; $look= $_POST['look']; $do= $_POST['do']; $seen= $_POST['seen']; $comments= $_POST['comments']; $email = $_POST['email']; validation(); }; // Create functions to validate first name, last name, email and if they saw fluffy function validation () { if (!empty($_POST['fname'])) { $firstname = $_POST['fname']; $firstnameerror = NULL; echo '<p>Sorry, you must give your First Name!</p>';} if (!empty($_POST['lname'])) { $lastname = $_POST['lname']; } else { $lastname = NULL; echo '<p>Sorry, you must give your Last Name!</p>'; } if (!empty($_POST['email'])) { $email = $_POST['email']; } else { $email = NULL; echo '<p>Sorry, you must provide an email address!</p>'; } if (isset($_POST['seen'])) { $seen = $_POST['seen']; } elseif ($seen == "no") { echo '<p>Darn, we really miss him!</p>'; } elseif ($seen = NULL) { echo '<p>Sorry you have not told us if you saw Fluffy or not!</p>' ; } else { $seen = NULL ; echo '<p>Sorry, you did not tell us if you saw Fluffy or not!</p>'; }} // create confirmation function function confirmation () { if ($lastname && $firstname && $email && $seen == true) { echo "<p>Thanks for submitting the form <emp><b>$firstname $lastname</b></emp>!</p> <p>This is what we got: <br> You were abducted: <b> $when</b> for <b>$length</b></p> <p>You saw <b>$see</b> aliens that appeared <b>$look</b></p> <p>You answered: <b>$seen</b> in regards to seeing Fluffy.</p> <p> Here are your additional comments: <br> $comments</p> \n"; } else { validation() ;} } echo '<p>Share your story of alien abduction:</p> <p>Please note, anything with an asertisk (<b>*</b>) is required.</p> <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="abductreport"> <!-- First Name Input --!> <label for="fname">First Name:<b>*</b></label> <input type="text" name="fname" id="fname" placeholder="First Name" value="<?php echo $fname ?>"><br> <label for="lname">Last Name:<b>*</b></label> <input type="text" name="lname" id="lname" placeholder="Last Name"><br> <label for="email">What is your Email address?<b>*</b></label> <input type="email" name="email" id="email" placeholder="Email"><br> <label for="when">When did it happen?</label> <input type="date" name="when" id="when"><br> <label for="length">How long were you gone?</label> <input type="text" name="length" id="length" placeholder="days, months, years?"><br> <label for="see">How many did you see?</label> <input type="number" name="see" id="see" placeholder="Enter a Number"><br> <label for="look">Describe them:</label> <input type="text" name="look" id="look" placeholder="What was their appearance?"><br> <label for="do">What did they do you to?</label> <input type="text" name="do" id="do" placeholder="Describe what they did"><br> Have you seen my dog fluffy?<b>*</b> <input type="radio" name="seen" id="yes" value="yes">Yes <input type="radio" name="seen" id="no" value="no">No<br> <img src="fluffy.jpg" alt="Have you seen Fluffy?"><br> <label for="comments">Anything else you want to Add?</label> <textarea rows="3" cols="50" name="comments" id="comments" placeholder="Your comments..."></textarea><br><br> <input type="submit" value="Report Abduction"> </form>' ?>'