IT-Guy Posted February 18, 2008 Share Posted February 18, 2008 What Iam trying to do is teach myself how to get php to check a field, and if the field is empty it returns you to the form saying, '$whatever-field-name Field was empty' So here is my script, the error Im getting is: Parse error: parse error, unexpected $end in D:\my\website\login\test\username.php on line 56 Here is my script, any suggestions on how I fix this? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Language" content="en-us" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Username</title> <style type="text/css"> input {color:#666;} </style> </head> <body> <table style="width: 100%"> <tr> <td>Username </td> </tr> <?php ##session_start(); $con = mysql_connect("****","***","***") or die('Could not connect: ' . mysql_error()); mysql_select_db("test", $con); if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"username.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"Submit\" name=\"submit\"></td></tr>\n"; echo "</form></table>\n"; }else { $error = array(); if(empty($_POST['username'])) { $error[] = "Your username was empty!"; } <tr> <td><input name="Username" size="10" type="text" /></td> </tr> </table> <p><input value="Submit Form" type="submit" /></p> </body> </html> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted February 18, 2008 Share Posted February 18, 2008 <?php $username=$_POST['username']; if ($username=="") {$error[] = "Your username was empty!";}?> Quote Link to comment Share on other sites More sharing options...
IT-Guy Posted February 18, 2008 Author Share Posted February 18, 2008 <?php if ($field=="") { echo "this field is empty!";}?> Do I have to put anything in between the field=="" quotations Quote Link to comment Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Share Posted February 18, 2008 <?php if (empty($field)) { echo "This field is empty!"; } ?> I would use empty because of the number of different checks it does. Check out the return values section. http://us2.php.net/empty Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted February 18, 2008 Share Posted February 18, 2008 its just another way of checking if it is empty. and no you dont. look at my code above now Quote Link to comment Share on other sites More sharing options...
IT-Guy Posted February 18, 2008 Author Share Posted February 18, 2008 I took a look, and fixed the code you suggested and now Im getting a parse error on line 42, but there isn't 41 lines of code Any suggestions? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Language" content="en-us" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Username</title> <style type="text/css"> input {color:#666;} </style> </head> <body> <table style="width: 100%"> <tr> <td>Username <br /> <input name="username" size="10" type="text" /><br /> <input value="submit" type="submit" /></td> </tr> <?php ##session_start(); $con = mysql_connect("****","****","****") or die('Could not connect: ' . mysql_error()); mysql_select_db("***", $con); if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"username.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n"; echo "</form></table>\n"; }else { $error = array(); if ($field=="") { echo "this field is empty!";} ?> Quote Link to comment Share on other sites More sharing options...
IT-Guy Posted February 18, 2008 Author Share Posted February 18, 2008 I got it all fixed! Thanks everyone for the help! Quote Link to comment Share on other sites More sharing options...
law Posted February 18, 2008 Share Posted February 18, 2008 just a thought but for the future it might be easier for you to use little qoutes inside of big quotes.. i guess its just all on what your comfortable with? example <?php echo "Now that i am using double quotes to display this text as html i can use 'single quotes' without a problem"; echo "<input type='reset' name='reset' value='Reset'>"; ?> Quote Link to comment Share on other sites More sharing options...
IT-Guy Posted February 18, 2008 Author Share Posted February 18, 2008 Ok, now that I got it fixed, when it actions, it actions to username.php (which is the name of the document Im using to test this out) Now if you leave the field blank, hit submit, it returns and says, 'This Field Is Empty' but it no longer shows the text box for the username field. - Why is that? Anyone know? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <?php ##session_start(); $con = mysql_connect("****","****","*****") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"username.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n"; echo "</form></table>\n"; }else { $error = array(); if ($field=="") { echo "this field is empty!";} } ?> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted February 18, 2008 Share Posted February 18, 2008 The way you have coded, you can't have both. They are in separate if else brackets. Quote Link to comment Share on other sites More sharing options...
IT-Guy Posted February 18, 2008 Author Share Posted February 18, 2008 So if I were to put it all in one bracket, it will display the field? Quote Link to comment Share on other sites More sharing options...
law Posted February 18, 2008 Share Posted February 18, 2008 i could be wrong but you really don't need the if(!$_POST['submit']){}else{} at all? i think the site will do what you intend for it to without that code ----Edited below--- also don't you need to define the $field because currently it is nothing right? $field = $_POST['username']; also there is a better way to right this to avoid someone using injection on you.. but i am not 100% sure what it is so im not going to tell you the wrong thing Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted February 18, 2008 Share Posted February 18, 2008 <?php ##session_start(); $con = mysql_connect("****","****","*****") or die('Could not connect: ' . mysql_error()); mysql_select_db("login", $con); $username=mysql_real_escape_string(trim(strip_tags($_POST['username']))); if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"username.php\">\n"; echo "<tr><td>Username</td><td><input type=\"text\" name=\"username\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" value=\"submit\" name=\"submit\"></td></tr>\n"; echo "</form></table>\n"; }else { $error = array();} if ($username=="") { echo "this field is empty!";} } ?> Quote Link to comment 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.