mindapolis Posted October 21, 2015 Share Posted October 21, 2015 Hi, I haven't done PHP in years. Could someone help me figure out why this code won't work please? there seems to be a problem between lines 24-28. <?php require_once('functions.php'); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <style type="text/css"> td { margin-bottom:110px; } </style> <title>Untitled Document</title> </head> <body> <table id="contactForm"> <?Php $errors=array(); if(isset($_POST['submit'])) { validate_input(); if(count($errors) !=0) { display_form(); } else(display_form(); function validate_input() { global $errors; if($_POST["fname"] == * *) { $errors['fname']="<font color ='red'> Please enter your first name </font>"; } ?> <form method="post"> <tr> <td class="question" >First Name:<br /> <input type="text" name="fname" value =<?php echo $_POST[fname];?> size="15"> <?php echo $errors['fname']; ?></td> <td class="question">Last Name<br /> <input type="text" name="lname" required size="20"> </td> <td class="question">Organization's Name:<br /> <input type="text" name="orgName" size="15" maxlength="50"> </td> </tr> <tr> <td class="question">Street Address: <br /> <input type="text" name="address" size="15" maxlength="50"></td> <td class="question">City: <br /> <input type="text" name="city" size="10" maxlength="25"> </td> <td class="question">State: <br /> <select name = "state" value="<?=$state?>"> <option value ="Please choose a state"> Please choose a state</option> <?php states($state) ?> </select> </td> <td class="question">Zipcode:<br /> <input type="number" name="zipcode" size="5" maxlength="5"> </td> </tr> <tr> <td>Phone Number: <br />(including area code) <br /> <input type="text" name="phone" size="10" maxlength="10"></td> <td>Fax Number: <br />(including area code) <br /> <input type="text" name="fax" size="10" maxlength="10"></td> </tr> <tr> <td>Email:<br /> <input type="text" name="email" required ></td> <td>Confirm Email:<br /> <input type="text" name="ConfirmEmail" required ></td> </tr> <tr> <td>What would you like help with? </td> <td><table id="projectOptions"> <tr> <td><input type="checkbox" name="SocialMedia">Social Media </td> <td><input type="checkbox" name="WebContentManagement">Web Content Management </td> </tr> <tr> <td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation </td> <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) </td> </tr> <tr> <td><input type="checkbox" name="VideoEditing"> Video Editing </td> <td><input type="checkbox" name="WebDesign">Web Design </td> </tr> </table></td> <tr> <td>Overview about the project: </td> <td><textarea></textarea></td> </tr> <tr> <td>If you are not a robot, what year is it? </td> <td><input type="text" name="year" size="4" maxlength="4"> </tr> <tr> <td><input type="submit" name="Contact Me!"></td> <td><input type="reset"></td> </tr> </form> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/ Share on other sites More sharing options...
maxxd Posted October 21, 2015 Share Posted October 21, 2015 Define "won't work". The most immediate thing that I see is that it doesn't look like you've matched brackets on your if/else statements. It's difficult to tell, as the indentation is completely out of whack, but that could be the board doing that. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523939 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 When I run the page it saysParse error: syntax error, unexpected ';' in /web/html/mediaservicesunlimited.com/contactUs.php on line 25 Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523946 Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 Unwanted parenthesis else(display_form(); Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523949 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 that fixed one error. Now it saysParse error: syntax error, unexpected '*' in /web/html/mediaservicesunlimited.com/contactUs.php on line 28 I'm having problems seeing what 's in between the quotes Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523952 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 (edited) I figured that out but now it saysParse error: syntax error, unexpected $end in /web/html/mediaservicesunlimited.com/contactUs.php on line 99 Edited October 21, 2015 by mindapolis Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523954 Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 (edited) The most common cause of that error is an opening "{" without a corresponding closing "}" [edit] ... or, in your case, two Edited October 21, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523959 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 I think I found it but now it saysFatal error: Call to undefined function display_form() in /web/html/mediaservicesunlimited.com/contactUs.php on line 25 sorry for all the questions. like I said it 's been awhile since I have worked with PHP Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523962 Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 Do you think that may be because you haven't defined a function called display_form()? And what is purpose of the if() statement here when you do exactly the same regardless of the outcome? if(count($errors) !=0) { display_form(); } else display_form(); Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523964 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 To be honest, I followed an example from a PHP textbook I had several years ago and that is the structure they used. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523965 Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 The structure is ok, it's the logic using it that is questionable. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523966 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 What 's the best way to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523967 Share on other sites More sharing options...
Barand Posted October 21, 2015 Share Posted October 21, 2015 Strictly speaking, it ain't broken but a simple display_form(); would do exactly the same as if(count($errors) !=0) { display_form(); } else display_form(); The question is whether or not you want to call the function at all, as you don't appear to have a definition of that function. Should there be a display_form() function? Only you know that - we do not know what you intend to do. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523969 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 Got it! Thanks for all the help! Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523970 Share on other sites More sharing options...
mindapolis Posted October 21, 2015 Author Share Posted October 21, 2015 I thought it was fixed, but if I dont put anything in the fname field and click submit no error message appears. <?php require_once('functions.php'); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <style type="text/css"> td { margin-bottom:110px; } </style> <title>Untitled Document</title> </head> <body> <table id="contactForm"> <?Php $errors=array(); if(isset($_POST['submit'])) { validate_input(); display_form(); function validate_input() { global $errors; if($_POST["fname"] == " ") { $errors['fname']="<font color ='red'> Please enter your first name </font>"; } } } ?> <form method="post"> <tr> <td class="question" >First Name:<br /> <input type="text" name="fname" size="15" value =<?php echo $_POST[fname];?> > <?php echo $errors['fname']; ?></td> <td class="question">Last Name<br /> <input type="text" name="lname" size="20"> </td> <td class="question">Organization's Name:<br /> <input type="text" name="orgName" size="15" maxlength="50"> </td> </tr> <tr> <td class="question">Street Address: <br /> <input type="text" name="address" size="15" maxlength="50"></td> <td class="question">City: <br /> <input type="text" name="city" size="10" maxlength="25"> </td> <td class="question">State: <br /> <select name = "state" value="<?=$state?>"> <option value ="Please choose a state"> Please choose a state</option> <?php states($state); ?> </select> </td> <td class="question">Zipcode:<br /> <input type="number" name="zipcode" size="5" maxlength="5"> </td> </tr> <tr> <td>Phone Number: <br />(including area code) <br /> <input type="text" name="phone" size="10" maxlength="10"></td> <td>Fax Number: <br />(including area code) <br /> <input type="text" name="fax" size="10" maxlength="10"></td> </tr> <tr> <td>Email:<br /> <input type="text" name="email" /></td> <td>Confirm Email:<br /> <input type="text" name="ConfirmEmail" /></td> </tr> <tr> <td>What would you like help with? </td> <td><table id="projectOptions"> <tr> <td><input type="checkbox" name="SocialMedia">Social Media </td> <td><input type="checkbox" name="WebContentManagement">Web Content Management </td> </tr> <tr> <td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation </td> <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) </td> </tr> <tr> <td><input type="checkbox" name="VideoEditing"> Video Editing </td> <td><input type="checkbox" name="WebDesign">Web Design </td> </tr> </table></td> <tr> <td>Overview about the project: </td> <td><textarea></textarea></td> </tr> <tr> <td>If you are not a robot, what year is it? </td> <td><input type="text" name="year" size="4" maxlength="4"> </tr> <tr> <td><input type="submit" value="Contact Me!"></td> <td><input type="reset"></td> </tr> </form> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523977 Share on other sites More sharing options...
Barand Posted October 22, 2015 Share Posted October 22, 2015 You have coded it to give an error only when the name contains a single space if($_POST["fname"] == " ") { $errors['fname']="<font color ='red'> Please enter your first name </font>"; } Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1523997 Share on other sites More sharing options...
mindapolis Posted October 22, 2015 Author Share Posted October 22, 2015 i deleted the space between the quotes but that didn't solve the problem. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524022 Share on other sites More sharing options...
maxxd Posted October 22, 2015 Share Posted October 22, 2015 Use empty() if(empty($_POST['fname'])){ $errors['fname'] = "<span class='error'>Please enter your first name.</span>"; } Please note that I updated your code to HTML5 and assumed you would use stylesheets, instead of deprecated markup and inline styling as is currently the case. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524029 Share on other sites More sharing options...
mindapolis Posted October 22, 2015 Author Share Posted October 22, 2015 (edited) so put if empty()($_post['fname']) { Edited October 22, 2015 by mindapolis Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524032 Share on other sites More sharing options...
maxxd Posted October 22, 2015 Share Posted October 22, 2015 so put if empty()($_post['fname']) { No. Use the code I gave you. The if() statement is pretty clear in that post. Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524033 Share on other sites More sharing options...
mindapolis Posted October 22, 2015 Author Share Posted October 22, 2015 I'm so sorry. I put what you wrote but it 's still not working if the fname field is left blank. <?Php $errors=array(); if(isset($_POST['submit'])) { validate_input(); display_form(); function validate_input() { global $errors; if(empty($_POST['fname'])){ $errors['fname'] = "<span class='error'>Please enter your first name.</span>"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524043 Share on other sites More sharing options...
Barand Posted October 22, 2015 Share Posted October 22, 2015 You have only defined the function to validate input, you haven't called it Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524048 Share on other sites More sharing options...
maxxd Posted October 22, 2015 Share Posted October 22, 2015 Try running this code - comment out everything else on your page, paste this at the top of the page, and let us know what it says in your browser when you submit the form to this page. <?php error_reporting(-1); ini_set('display_errors',1); if(isset($_POST['submit'])) { $errors = validate_input(); display_form($errors); }else{ print("<p>POST is not set</p>"); } function validate_input() { if(empty($_POST['fname'])){ $errors['fname'] = "<span class='error'>Please enter your first name.</span>"; } return $errors; } function display_form(array $errors){ if(!empty($errors)){ print("<pre>".print_r($errors,true)."</pre>"); }else{ print("<p>No errors!</p>"); } } Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524051 Share on other sites More sharing options...
mindapolis Posted October 23, 2015 Author Share Posted October 23, 2015 At the top of the page it says POST is not set and under the first name textbox it says Notice: Undefined variable: errors in /web/html/mediaservicesunlimited.com/contactUs.php on line 58 <?php error_reporting(-1); ini_set('display_errors',1); if(isset($_POST['submit'])) { $errors = validate_input(); display_form($errors); }else{ print("<p>POST is not set</p>"); } function validate_input() { if(empty($_POST['fname'])){ $errors['fname'] = "<span class='error'>Please enter your first name.</span>"; } return $errors; } function display_form(array $errors){ if(!empty($errors)){ print("<pre>".print_r($errors,true)."</pre>"); }else{ print("<p>No errors!</p>"); } } //<?php require_once('functions.php'); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <style type="text/css"> </style> <title>Untitled Document</title> </head> <body> <table id="contactForm" border cellspacing="20"> <?php /* $errors=array(); if(isset($_POST['submit'])) { validate_input(); display_form(); function validate_input() { global $errors; if(empty($_POST['fname'])){ $errors['fname'] = "<span class='error'>Please enter your first name.</span>"; } } } ?> <?php validate_input();*/ ?> <form method="post"> <tr> <td class="question" >First Name:<br /> <input type="text" name="fname" size="15" value =<?php //aecho $_POST[fname];?> > <?php echo $errors['fname']; ?></td> <td class="question">Last Name<br /> <input type="text" name="lname" size="20"> </td> <td class="question">Organization's Name:<br /> <input type="text" name="orgName" size="15" maxlength="50"> </td> </tr> <tr> <td class="question">Street Address: <br /> <input type="text" name="address" size="15" maxlength="50"></td> <td class="question">City: <br /> <input type="text" name="city" size="10" maxlength="25"> </td> <td class="question">State: <br /> <select name = "state" value="<?=$state?>"> <option value ="Please choose a state"> Please choose a state</option> <?php states($state); ?> </select> </td> <td class="question">Zipcode:<br /> <input type="number" name="zipcode" size="5" maxlength="5"> </td> </tr> <tr> <td>Phone Number: <br />(including area code) <br /> <input type="text" name="phone" size="10" maxlength="10"></td> <td>Fax Number: <br />(including area code) <br /> <input type="text" name="fax" size="10" maxlength="10"></td> </tr> <tr> <td>Email:<br /> <input type="text" name="email" /></td> <td>Confirm Email:<br /> <input type="text" name="ConfirmEmail" /></td> </tr> <tr> <td>What would you like help with? </td> <td><table id="projectOptions"> <tr span=2> <td><input type="checkbox" name="SocialMedia">Social Media </td> <td><input type="checkbox" name="WebContentManagement">Web Content Management </td> </tr> <tr> <td><input type="checkbox" name="MarketingMaterials">Marketing Material Creation </td> <td><input type="checkbox" name="SEO">SEO (Search Engine Optimization) </td> </tr> <tr> <td><input type="checkbox" name="VideoEditing"> Video Editing </td> <td><input type="checkbox" name="WebDesign">Web Design </td> </tr> </table></td> <tr> <td>Overview about the project: </td> <td><textarea></textarea></td> </tr> <tr> <td>If you are not a robot, what year is it? </td> <td><input type="text" name="year" size="4" maxlength="4"> </tr> <tr> <td><input type="submit" value="Contact Me!"></td> <td><input type="reset"></td> </tr> </form> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524098 Share on other sites More sharing options...
benanamen Posted October 23, 2015 Share Posted October 23, 2015 (edited) Your problem is that submit is missing the name attribute Your code: <input type="submit" name="submit" value="Contact Me!"> Correct: <input type="submit" name="submit" value="Contact Me!"> You still have other issues. I may rewrite it for you. You are also missing a couple closing tags and you dont use tables for page layout. Use CSS. Edited October 23, 2015 by benanamen Quote Link to comment https://forums.phpfreaks.com/topic/298756-sticky-form/#findComment-1524101 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.