mindapolis Posted November 8, 2011 Share Posted November 8, 2011 Hi, I wrote a simple code to check to see if the user filled in the name field, but when I uploaded the file, it just displays the code. here 's the code. <!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-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?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['name'] == " "){ $errors['name']="dipshit, put your name"; } ?> <form action="" method="post" name="test"> Name: <input name="name" type="text" size="10" maxlength="15" value="<?php echo $_POST[name]; ?>"/><br /> <?php echo $errors['name']; ?> <input name="submit" type="button" value="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/ Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 Is the file extension .php? Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286208 Share on other sites More sharing options...
mindapolis Posted November 8, 2011 Author Share Posted November 8, 2011 yes Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286212 Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 And what happens when you run the code locally? It should generate a parse error; unexpected $end Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286215 Share on other sites More sharing options...
mindapolis Posted November 8, 2011 Author Share Posted November 8, 2011 No, there 's no error. It just displays the script on the webpage. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286223 Share on other sites More sharing options...
Pikachu2000 Posted November 8, 2011 Share Posted November 8, 2011 Perhaps you could elaborate on that a bit? Maybe paste the result of View ---> Source? Is this script being include()d by another script? Details are important. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286225 Share on other sites More sharing options...
mindapolis Posted November 8, 2011 Author Share Posted November 8, 2011 when I click view source I get this code. here 's the link to the page. http://auntievics.com/error.phtml <!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-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?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['name'] == " "){ $errors['name']="dipshit, put your name"; } ?> <form action="" method="post" name="test"> Name: <input name="name" type="text" size="10" maxlength="15" value="<?php echo $_POST[name]; ?>"/><br /> <?php echo $errors['name']; ?> <input name="submit" type="button" value="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286232 Share on other sites More sharing options...
RaythMistwalker Posted November 8, 2011 Share Posted November 8, 2011 .phtml isn't .php Save as error.php Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286233 Share on other sites More sharing options...
mindapolis Posted November 8, 2011 Author Share Posted November 8, 2011 I am so sorry. In my PHP class my professor said php and phtml were the same thing. Anyway, I fixed that error but if I don't enter a name and click submit no error message comes up. I wonder if it has something to do with the form action? ? Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286347 Share on other sites More sharing options...
NewcastleFan Posted November 8, 2011 Share Posted November 8, 2011 Try: if(!$_POST['name']) Instead of: if($_POST['name'] == " ") Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286349 Share on other sites More sharing options...
Drummin Posted November 8, 2011 Share Posted November 8, 2011 $_POST['name'] is going to be there so if(!$_POST['name']) is not what you need. If (empty($POST['name'])) will let you know if the value of $POST['name'] has a value. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286358 Share on other sites More sharing options...
redarrow Posted November 8, 2011 Share Posted November 8, 2011 In my PHP class my professor said php and phtml were the same thing your professor must of fort, when you load php from scratch, the extension settings are set as phtml and not .php when you download a copy of php, sometimes the extension settings are set to all sorts of settings. maybe the professor, should teach the internal settings of php and Apache. also looking at your code, i think if the teacher used upper case, for xhtml then others can identify html from php quicker. also where the comments to show what the code doing. If your going to learn php, html, mysql, and ajax and JavaScript plus css, might as well do it Wright. what the course price dont tell me £3.ooo plus only 10 sittings/lessons Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286378 Share on other sites More sharing options...
KevinM1 Posted November 8, 2011 Share Posted November 8, 2011 Maybe the professor should teach to not use the 'global' keyword, and how to properly structure a PHP script as well. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286380 Share on other sites More sharing options...
xyph Posted November 8, 2011 Share Posted November 8, 2011 Your professor seems like 75% of the 'teachers' I had going through my degree. The only thing harder than putting up with their classes was trying to get my money back To solve you issue, no PHTML is NOT the same extension as PHP. Just like <? is not the same as <?php. Some PHP installs are set up to only parse files with the PHP extension, some will even parse HTML extensions. There are ways around this, but as a general rule, name your files using the 'php' extension to ensure maximum compatibility. Let us know what happens after you change the file's extension. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286403 Share on other sites More sharing options...
mindapolis Posted November 9, 2011 Author Share Posted November 9, 2011 I changed the file extension & adjusted the following code but still no go. <?php $errors=array(); if(isset($_POST['submit'])) { validate_input(); if(count($errors) !=0) { display_form(); } else { display_form(); } } function validate_input() { global $errors; if(empty($_POST['name']) == " "){ $errors['name']="Hey dipshit, put your name"; } } ?> <form action="checkOut.php" method="post" name="test"> Name: <input name="name" type="text" size="10" maxlength="15" value="<?php echo $_POST[name]; ?>"/><br /> <?php echo $errors['name']; ?> <input name="submit" type="button" value="submit" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286478 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 No go what? The PHP still isn't being parsed? Are you trying to open these files on your local computer? If so, it isn't getting parsed because you don't have a parser on your computer. Download and install WAMP (Windows) or MAMP (OSX) to solves that issue. When it's installed and running, put your PHP files in your web directory and access through http://localhost/file.php Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286480 Share on other sites More sharing options...
mindapolis Posted November 9, 2011 Author Share Posted November 9, 2011 I'm sorry, I meant if I leave the name field blank and click submit no error comes up saying put your name Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286482 Share on other sites More sharing options...
xyph Posted November 9, 2011 Share Posted November 9, 2011 Of course it doesn't. Look at what your script checking. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286483 Share on other sites More sharing options...
mindapolis Posted November 9, 2011 Author Share Posted November 9, 2011 What? ? sorry, it 's late Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286484 Share on other sites More sharing options...
Drummin Posted November 9, 2011 Share Posted November 9, 2011 I won't even mess with your function attempt but here's a working form based on what you have started. You should read up on html and forms. Don't use type="button"... <!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-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php $errors=array(); if(isset($_POST['submit'])) { if(empty($_POST['name'])){ $errors['name']="dipshit, put your name"; } } ?> <form action="" method="post" name="test"> <?php if(isset($errors['name'])){ echo $errors['name']."<br />"; } ?> Name: <input name="name" type="text" size="10" maxlength="15" value="<?php if(isset($_POST['name'])){ echo "$_POST[name]"; } ?>" /><br /> <input name="submit" type="submit" value="Submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286504 Share on other sites More sharing options...
mindapolis Posted November 9, 2011 Author Share Posted November 9, 2011 Oh, I am so sorry. I just realized what I did. It was really late when I made my last reply. I am so sorry. Quote Link to comment https://forums.phpfreaks.com/topic/250698-error-checking-code/#findComment-1286611 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.