14862875 Posted April 26, 2009 Share Posted April 26, 2009 hey guys i'm having trouble with this sticky form i coded...my form don't seem to show when i run the script. here's my code: <?php if(isset($_POST['submitted'])) { if(!empty($_POST['fname'])) { print"<b>First name:</b>{$_POST['fname']}"; } else { print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>"; include('form.php'); } if(!empty($_POST['lname'])) { print"<b>Last name:</b>{$_POST['lname']}"; } else { print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>"; include('form.php'); } if(!empty($_POST['email'])) { print"<b>Email:</b>{$_POST['email']}"; } else { print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>"; include('form.php'); } if(!empty($_POST['ctn'])) { print"<b>Contact number:</b>{$_POST['ctn']}"; } else { print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>"; include('form.php'); } } ?> thats my index.php here's my form.php <form name="basic" action="index.php" method="post"> <table cellpadding="8" style="text-align:left;color:black;font-family:tahoma;font-size:12;padding:20px;"> <tr><td>First name:</td><td><input type='text' name='fname' value='<?php if(isset($_POST['fname'])) print $_POST['fname'];?>'</input></td></tr> <tr><td>Last name:</td><td><input type='text' name='lname' value='<?php if(isset($_POST['lname'])) print $_POST['lname'];?>'</input></td></tr> <tr><td>Email:</td><td><input type='text' name='email' value='<?php if(isset($_POST['email'])) print $_POST['email'];?>'</input></td></tr> <tr><td>Contact number:</td><td><input type='text' name='ctn' value='<?php if(isset($_POST['ctn'])) print $_POST['ctn'];?>'</input></td></tr> <input type="hidden" name="submitted" value="TRUE"/> <tr><td><input type="submit" value="Submit"></td></tr> </form> please help i've coded it differently <?php if(isset($_POST['submitted'])) { $problem=FALSE; if(empty($_POST['fname'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>"; $problem=TRUE; } if(empty($_POST['lname'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>"; $problem=TRUE; } if(empty($_POST['email'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>"; $problem=TRUE; } if(empty($_POST['ctn'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>"; $problem=TRUE; } else { print"please try again"; } } ?> it gets all tangled up, don't know how to fix this.. please help.. Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/ Share on other sites More sharing options...
mikesta707 Posted April 26, 2009 Share Posted April 26, 2009 try if(isset($_POST['submitted'])) { $problem=FALSE; else if(empty($_POST['fname'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>"; $problem=TRUE; } else if(empty($_POST['lname'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>"; $problem=TRUE; } else if(empty($_POST['email'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>"; $problem=TRUE; } else if(empty($_POST['ctn'])) { print "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>"; $problem=TRUE; } else { print"please try again"; } } ?> [code=php:0] the last else you wrote is only connected to the last if, and not the entire chain of if statements. I'm still not sure what you are trying to do with this form, or what the problem is. Can you explain further? Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819689 Share on other sites More sharing options...
14862875 Posted April 26, 2009 Author Share Posted April 26, 2009 Hey, thanx buddy, but its still not working...The first index.php page that i posted above is the more preferable one or one that might work if it gets chop and changed a bit.. I want the user to enter his first name, last name, email adres and contact number.. if one field is left out a warning message should appear and the form should be displayed again already containing the values of the fields he/she did complete – thus a ‘sticky’ form. my form does not display, the include function seems to not be working.. i'm not really good at php tho, so i might be messing up somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819702 Share on other sites More sharing options...
mrMarcus Posted April 26, 2009 Share Posted April 26, 2009 well, double-check that your form.php is in the correct directory (included properly) first. Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819718 Share on other sites More sharing options...
14862875 Posted April 26, 2009 Author Share Posted April 26, 2009 it is, double checked Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819768 Share on other sites More sharing options...
mrMarcus Posted April 26, 2009 Share Posted April 26, 2009 you're missing an end table tag, btw. try this : <?php error_reporting(E_ALL); if(isset($_POST['Submit'])) //lost the hidden input .. just use the submit button instead; { if(!isset($_POST['fname'])) { $errors = "<span style='color:red;font-weight:bold;'>Please fill out your first name field</span><p/>"; } if(!isset($_POST['lname'])) { $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your last name field</span><p/>"; } if(!isset($_POST['email'])) { $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your email adres</span><p/>"; } if(!isset($_POST['ctn'])) { $errors[] = "<span style='color:red;font-weight:bold;'>Please fill out your contact number</span><p/>"; } #check if there are any errors; if (is_array ($errors)) { $do = "errors"; } else { $do = "submit"; } /* switch(); * toggle errors, valid submission; * default: show the form; */ switch ($do) { case errors: $i = 1; foreach ($errors as $v) { echo $i++.'. '.$v.'<br />'; } include ('form.php'); break; case submit: //do any required actions here, ie. insert/update database, send email, etc. break; default: include ('form.php'); break; } } ?> keep your form as is .. looks like it should remain sticky. post your results afterwards. Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819834 Share on other sites More sharing options...
mrMarcus Posted April 26, 2009 Share Posted April 26, 2009 EDIT... add an } else { include ('form.php'); } to include the form upon no form submission .. left it out last time. Quote Link to comment https://forums.phpfreaks.com/topic/155728-helpsticky-form-problem/#findComment-819877 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.