blacksheep Posted October 25, 2006 Share Posted October 25, 2006 I'd consider this my first real php experience as I've only used include() before now, lol, but I'm having a hard time with some form validation. It's easy enough to check for problems and error it out, but I want to have the user still have all their inputted information displayed in the form (even the errored stuff). I've been looking around online for a couple days on this and figured most of it out but I can't get the damn drop-down menu or the textarea to "save".Pardon the ghetto code :-[ The error stuff wasn't being recognized and I finally messed around until it worked and so I don't want to clean it up and break it. ;D[code]<?php /* define blanks */ $to = "my email"; $subject = "Facilities Request $type"; $email = $_POST['email']; $name = $_POST['name']; $phone = $_POST['phone']; $building = $_POST['building']; $room = $_POST['room']; $request = $_POST['request']; $type = $_POST['type']; $message = ""; /* Form Validation */ if ($email == "" || $name == "" || $phone == "" || $building == "" || $room == "" || $request == "" || $type == "Please Select Type of Request") { $errormsg = "Error: One or more field was left blank."; $error = true; error_out(); } elseif (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) { $errormsg = "Error: Invalid email address."; $error = true; error_out(); } else { $error2 = 1; } /* define message content */ $message .= "Request Type: $type <br><br>"; $message .= "From: $name <br>"; $message .= "Email Address: $email <br>"; $message .= "Phone Number: $phone <br><br>"; $message .= "Building: $building <br>"; $message .= "Room: $room <br><br>"; $message .= "Message:\n $request\n"; /* clearly define headers */ $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $headers .= "To: name <my email>\n"; $headers .= "From: name <do-not-reply email>\n"; if($error2 == 1) { $sent = mail($to, $subject, $message, $headers); print "<p align=center><br>Thank you. Your request was sent successfully and will be processed soon.</p>"; } else { print "<font color=red> $errormsg </font> <br><br>"; } function error_out() { global $errormsg; global $email; global $name; global $phone; global $building; global $room; global $request; global $message; global $type; global $error2; global $error; $error = false; ?> <!-- contact_m.php --> <div align="center"> <p class="MAEtitleText"> <font color="#000066" size=+2><strong>Maintenance Request Form</strong></font><br> All Fields Required </p> <form method="post" action="sendform_m.php"> <table width=300 border=0> <tr> <td width="40%"> Name: </td> <td> <input name="name" type="text" size="32" value="<?php echo $name ?>"> </td> </tr> <tr> <td> Email: </td> <td> <input name="email" type="text" size="32" value="<?php echo $email ?>"> </td> </tr> <tr> <td> Phone Number: </td> <td> <input name="phone" type="text" size="32" value="<?php echo $phone ?>"> </td> </tr> <tr> <td> </td> <td align=right> Building: <input name="building" type="text" size="7" value="<?php echo $building ?>"> Rm: <input name="room" type="text" size="5" value="<?php echo $room ?>"> </td> </tr> <tr> <td><br></td> </tr> <tr> <td colspan=2> <select name="type"> <option> Please Select Type of Request <option> - Maintenance <option> - Telecommunications <option> - Work Order <option> - Other </select> </td> </tr> <tr> <td> Request: </td> <td> </td> </tr> <tr> <td colspan=2> <textarea name="request" rows="10" cols="43" value="<?php echo $_POST['request'] ?>"></textarea> </td> </tr> <tr> <td> <input type="submit" value="Submit"> </td> <td align=right> <input type="reset" value="Reset"> </td> </tr> </form> </div> <?php }?>[/code]I truly appreciate any help. :) Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/ Share on other sites More sharing options...
gmwebs Posted October 25, 2006 Share Posted October 25, 2006 The select part needs to have the <option> set as selected. Read up on the HTML element sites about that. Also, you should include the closing element tag </option> after each one.The textarea element is a different beast. The value option is not used. You need to do the following...[code]<textarea name="request" rows="10" cols="43"><?php echo $_POST['request'] ?></textarea>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114291 Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 First of all. Large parts of this code flat out don't make sense to me.Lemme see if I can fix this up a little...[code]<?php /* define blanks */ $to = "my email"; // $subject = "Facilities Request $type"; This line should be after $type is defined $email = $_POST['email']; $name = $_POST['name']; $phone = $_POST['phone']; $building = $_POST['building']; $room = $_POST['room']; $request = $_POST['request']; $type = $_POST['type']; $message = ""; $subject = "Facilities Request $type"; /* Form Validation */ if ($email == "" || $name == "" || $phone == "" || $building == "" || $room == "" || $request == "" || $type == "Please Select Type of Request") { $errormsg = "<font color=red>Error: One or more field was left blank.</font><br><br>"; //Change this to html to make things easier later $error = TRUE; error_out(); } elseif (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) { $errormsg = "<font color=red> Error: Invalid email address.</font><br><br>"; //This too $error = TRUE; error_out(); } else { $error = FALSE; //I don't see any good reason to use a different variable here } /* define message content */ $message .= "Request Type: $type <br><br>"; $message .= "From: $name <br>"; $message .= "Email Address: $email <br>"; $message .= "Phone Number: $phone <br><br>"; $message .= "Building: $building <br>"; $message .= "Room: $room <br><br>"; $message .= "Message:\n $request\n"; /* clearly define headers */ $headers .= "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $headers .= "To: name <my email>\n"; $headers .= "From: name <do-not-reply email>\n"; if($error === FALSE) { //Change this line to conform to change in variables above... mail($to, $subject, $message, $headers); //I'm not sure that mail() returns anything at all, and if it does, you don't use it anyways. print "<p align=center><br>Thank you. Your request was sent successfully and will be processed soon.</p>"; } //Don't print $errormsg here. Print it IN the html /* else { print "<font color=red> $errormsg </font> <br><br>"; }*/ function error_out() { global $errormsg; global $email; global $name; global $phone; global $building; global $room; global $request; global $message; global $type; //You already know what $error and $error2 are gonna equal, why globalize them here? And why change $error to false? //OK, I'm pretty sure you can't do this, because I think you only want the page to show this stuff if is executes error_out, and if you type it outside of the <?php, it'll show this one way or the other. This is what you meant to do: echo <<<END <!-- contact_m.php --> <div align="center"> <p class="MAEtitleText"> <font color="#000066" size=+2><strong>Maintenance Request Form</strong></font><br> All Fields Required </p> <form method="post" action="sendform_m.php"> $errormsg <table width=300 border=0> <tr> <td width="40%"> Name: </td> <td> <input name="name" type="text" size="32" value="$name"> </td> </tr> <tr> <td> Email: </td> <td> <input name="email" type="text" size="32" value="$email"> </td> </tr> <tr> <td> Phone Number: </td> <td> <input name="phone" type="text" size="32" value="$phone"> </td> </tr> <tr> <td> </td> <td align=right> Building: <input name="building" type="text" size="7" value="$building"> Rm: <input name="room" type="text" size="5" value="$room"> </td> </tr> <tr> <td><br></td> </tr> <tr> <td colspan=2> <select name="type"> <option>$type</option <option> Please Select Type of Request</option> <option> - Maintenance</option> <option> - Telecommunications</option> <option> - Work Order</option> <option> - Other</option> </select> </td> </tr> <tr> <td> Request: </td> <td> </td> </tr> <tr> <td colspan=2> <textarea name="request" rows="10" cols="43">$request</textarea> </td> </tr> <tr> <td> <input type="submit" value="Submit"> </td> <td align=right> <input type="reset" value="Reset"> </td> </tr> </form> </div>END;?>[/code]I haven't tried this yet, there may still be bugs, but was this what you were trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114302 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 Try replacing just this bit of code:[code]<select name="type"> <option> Please Select Type of Request <option> - Maintenance <option> - Telecommunications <option> - Work Order <option> - Other</select>[/code]With this:[code]<select name="type"><?php$dropdown = array("Please Select Type of Request", "Maintenance", "Telecommunications", "Work Order", "Other");foreach ($dropdown as $option){ if ($option == $_POST['type']){ echo "<option value=\"$option\" selected=\"selected\"> - $option</option>\n"; } else { echo "<option value=\"$option\"> - $option</option>\n"; }}?></select>[/code]You should be able to just swap it straight out.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114342 Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 yeah...I like HuggieBear's idea, that's probably the way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114384 Share on other sites More sharing options...
blacksheep Posted October 25, 2006 Author Share Posted October 25, 2006 [quote author=ksteuber link=topic=112686.msg457489#msg457489 date=1161796290]First of all. Large parts of this code flat out don't make sense to me.Lemme see if I can fix this up a little...[code]// $subject = "Facilities Request $type"; This line should be after $type is defined// ** Actually, it never had a problem with it, but I see your logic//Change this to html to make things easier later//This too//I don't see any good reason to use a different variable here// ** Surprisingly, all that error nonsense worked perfectly. I didn't see a reason for a second variable either, but it never recognized it as false. It refused to enter the final else statement and always took error as true. As soon as I made it a second variable and had it written that way, it recognized it. haha. There's probably something messed up elsewhere, but this works :D//Change this line to conform to change in variables above...//I'm not sure that mail() returns anything at all, and if it does, you don't use it anyways.// ** What do you mean mail() doesn't do anything? I thought it was the function that sent the form response to my email? //Don't print $errormsg here. Print it IN the html// ** That would clean it up a bit, but now I'm afraid it'll change how it displays it and I won't be able to fix it, haha//You already know what $error and $error2 are gonna equal, why globalize them here? And why change $error to false?// ** I was changing error to false incase it decided to remember it as true when it went through the next time. I think it was like that mostly because I was trying to find the bug with it not going through the else statement. Ideally, it should reset to false or stay true in the if statement anyways - so I see your point.//OK, I'm pretty sure you can't do this, because I think you only want the page to show this stuff if is executes error_out, and if you type it outside of the </?php, it'll show this one way or the other. This is // ** Actually, the function only appears in the if, elseif, so it'll only display the form again when you error. The first form display is another php file that posts to this one. lol. It's mostly because the error checking was a complete afterthought :D[/code]I haven't tried this yet, there may still be bugs, but was this what you were trying to do?[/quote]Thanks a bunch for the tips on the poor code, but somehow, someway, it all worked as it was perfectly well. ??? haha.[quote author=gmwebs link=topic=112686.msg457478#msg457478 date=1161795343][code]<textarea name="request" rows="10" cols="43"><?php echo $_POST['request'] ?></textarea>[/code][/quote]Wow, that was surprisingly easy! Damn. lol. Thanks![quote author=HuggieBear link=topic=112686.msg457530#msg457530 date=1161799835]Try replacing just this bit of code:[code]<select name="type"> <option> Please Select Type of Request <option> - Maintenance <option> - Telecommunications <option> - Work Order <option> - Other</select>[/code]With this:[code]<select name="type"><?php$dropdown = array("Please Select Type of Request", "Maintenance", "Telecommunications", "Work Order", "Other");foreach ($dropdown as $option){ if ($option == $_POST['type']){ echo "<option value=\"$option\" selected=\"selected\"> - $option</option>\n"; } else { echo "<option value=\"$option\"> - $option</option>\n"; }}?></select>[/code]You should be able to just swap it straight out.RegardsHuggie[/quote]Thank you so much! That code works great - I just wish I knew how it worked completely. If you have the time, could you explain it a little (mostly the if statement)? :)Thanks again to everyone! This was a really quick and knowledgable response! Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114409 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 When you submit the form, it posts the variables to the page, so lets sat for instance I select 'Work Order' from the drop down list and have filled all but one field in on the rest of the form.The value 'Work Order' gets assigned to the $_POST predefined variable, but it gets assigned to the 'type' part of it, because that's what our select list is called. It's the same as typing this [code=php:0]$_POST['type'] = "Work Order";[/code] with me so far?Now, when the form doesn't validate (because I didn't fill out one of the fields) it tries to display the form again, but this time we know what the user selected, as they've seen the form once, they selected Work Order, because that's what's in our $_POST['type'] variable.So all that foreach loop is doing, is saying, go through every one of the values in the $dropdown array and if it matches what's in our $_POST['type'] variable, then mark it as selected.I hope this helps.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25070-forms-remembering-inputs-help/#findComment-114494 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.