stutego Posted November 22, 2012 Share Posted November 22, 2012 (edited) please friends i have a problem trying to create a form for my web site please this is the code <?php $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (!$_post[$field]) { $warnings[$field] = "Required"; } } if ($_POST["email"] && !ereg("^[^@]+@([a-z\-]+\.)+[a-z]{2,4}$", $_POST["email"])) $warnings["email"] = "Invalid email"; if ($_POST["telephone"] && !ereg("^\([[:digit:]]{3}\)[[:digit:]]{3}-[[:digit:]]{4}$", $_POST["telephone"])) $warnings["telephone"] = "Must be (555)555-5555"; if (count($warnings) > 0) { ?> <form action = "register.php" method=post> <table border=0> <tr> <td>Firstname:</td> <td><input type=text size=30 name="fname" value="<?php echo $_post ["fname"];?>" > </td> <td><?php echo $warnings["fname"];?> </td> </tr><br/> <tr> <td>Lastname:</td> <td><input type=text size=30 name="lname" value="<?php echo $_post ["lname"];?>"> </td> <td><?php echo $warnings["lname"];?> </td> </tr> <tr> <td>Email address:</td> <td><input type=text size=30 name="email" value="<?php echo $_post["email"];?>"> </td> <td><?php echo $warnings["email"];?> </td> </tr> <tr> <td>Password:</td> <td><input type=text size=30 name="password" value="<?php echo $_post["password"];?>"> </td> <td><?php echo $warnings["password"];?> </td> </tr> <tr> <td>Telephone:</td> <td><input type=text size=30 name="telephone" value="<?php echo $_post["telephone"]?>"> </td> <td><?php echo $warnings["telephone"];?> </td> </tr> <tr> <td>Sex</td> <td><script src="sex.php"> </script></td> </tr> <tr> </TABLE> <INPUT TYPE=SUBMIT VALUE="Register"> </form> <?php } else { echo "Thank you for registering"; } ?> and this are the error messages, am a newbie to php please help and this is the error message Notice: Undefined variable: _post in C:\xampp\htdocs\form.php on line 8 Notice: Undefined variable: _post in C:\xampp\htdocs\form.php on line 8 Notice: Undefined variable: _post in C:\xampp\htdocs\form.php on line 8 Notice: Undefined variable: _post in C:\xampp\htdocs\form.php on line 8 Notice: Undefined index: email in C:\xampp\htdocs\form.php on line 13 Notice: Undefined index: telephone in C:\xampp\htdocs\form.php on line 16 please help me out Edited November 22, 2012 by stutego Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/ Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 You can write regular expressions but you can't understand those errors? Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394386 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 (edited) if (!$_post['field']) $_post should be in CAPS across all instances where it's in lowercase (within your form, as well): if (!$_POST['field']) And email/telephone have not been set, yet you're echo'ing them out within the form. You need to first determine whether they're set variables before attempting to access their value. <td><input type=text size=30 name="email" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : ''); ?>"> </td> Edited November 22, 2012 by mrMarcus Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394389 Share on other sites More sharing options...
stutego Posted November 22, 2012 Author Share Posted November 22, 2012 (edited) You can write regular expressions but you can't understand those errors? i've tried my best to clear the error and on like i said before am new to php, and i've been trying my best to learn php on my own please help out Edited November 22, 2012 by stutego Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394390 Share on other sites More sharing options...
stutego Posted November 22, 2012 Author Share Posted November 22, 2012 if (!$_post['field']) $_post should be in CAPS across all instances where it's in lowercase (within your form, as well): if (!$_POST['field']) And email/telephone have not been set, yet you're echo'ing them out within the form. You need to first determine whether they're set variables before attempting to access their value. <td><input type=text size=30 name="email" value="<?php echo (isset($_POST['email']) ? $_POST['email'] : ''); ?>"> </td> i've done that and it clear part of the error but not all Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394396 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 Did you make sure to set the variables? Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394397 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 Apply that same logic throughout the rest of your code. An undefined index means that index does not (yet) exist. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394399 Share on other sites More sharing options...
thara Posted November 22, 2012 Share Posted November 22, 2012 (edited) Try something like this... if ($_POST["email"] && !preg_match ('/^[\w.-]+@[\w.-]+\.[AZa-z]{2,6}$/', $_POST['email'])) Edited November 22, 2012 by thara Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394400 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 Try something like this... if ($_POST["email"] && !preg_match ('/^[\w.-]+@[\w.-]+\.[AZa-z]{2,6}$/', $_POST['email'])) I sincerely hope you were being sarcastic. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394403 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 OP, just look at the line numbers for the errors. Line 13: if ($_POST['email'] && ... You must check if index 'email' has been set within the $_POST array: if (isset($_POST['email']) && ... Follow that logic for line 16, and any other instances that might arise. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394407 Share on other sites More sharing options...
stutego Posted November 22, 2012 Author Share Posted November 22, 2012 OP, just look at the line numbers for the errors. Line 13: if ($_POST['email'] && ... You must check if index 'email' has been set within the $_POST array: if (isset($_POST['email']) && ... Follow that logic for line 16, and any other instances that might arise. yes tanx very much u've helped me figure some, remaining this ones Notice: Undefined index: fname in C:\xampp\htdocs\form.php on line 8 Notice: Undefined index: lname in C:\xampp\htdocs\form.php on line 8 Notice: Undefined index: email in C:\xampp\htdocs\form.php on line 8 Notice: Undefined index: password in C:\xampp\htdocs\form.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394410 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 He just explained to you how to get rid of those errors. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394411 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 He just explained to you how to get rid of those errors. Yup. Please re-read my first reply for the answer. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394413 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 He just explained to you how to get rid of those errors. I know that you know, but just to be clear - there are no errors here Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394414 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 I know that you know, but just to be clear - there are no errors here Mr. Technical lol. Yes, you're right. They're Notices and not Errors. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394415 Share on other sites More sharing options...
stutego Posted November 22, 2012 Author Share Posted November 22, 2012 so please friends how am i going to clear this errors or notices like i've said before am new to php and i dont really understand this error messages that was my reason for posting it here Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394419 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 This is one of those situations where I want to reach out through someone's computer screen and slap some sense into them. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394421 Share on other sites More sharing options...
DavidAM Posted November 22, 2012 Share Posted November 22, 2012 I know that you know, but just to be clear - there are no errors here Notices are logic errors. They may not be syntax errors; the script may continue to run; but the fact is that it is not able to do exactly what you told it to do. You can choose to ignore them but then, you can choose to not look both ways before crossing the street, too. At some point, there will likely be a problem. OP: Look at the error messages and look at your code. The message says all those errors are on line 8. My guess is this code at the beginning of your script: $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (!$_post[$field]) { $warnings[$field] = "Required"; } } that IF statement is checking each of those elements in the POST array. You need to check to see if they are set. They will not be set when the page is first requested (before it is submitted). Try if (isset($_POST[$field])). But, TEXT fields will always be set when submitted even if the user left them blank. So you can check for empty, except if the user just types spaces. I usually wrap all of the validation code in an IF that checks for the SUBMIT element, but you have not given your SUBMIT element a name. You could wrap it in a check of the request method: if ($_SERVER['REQUEST_METOD'] == 'POST') { $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (isset($_POST[$field])) { $check = trim($_POST[$field]); if (empty($check) $warnings[$field] = "Required"; } } } Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394430 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 @David, depend of the notice. It's not possible the language to know that you made a logical error, right? Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394434 Share on other sites More sharing options...
stutego Posted November 22, 2012 Author Share Posted November 22, 2012 Notices are logic errors. They may not be syntax errors; the script may continue to run; but the fact is that it is not able to do exactly what you told it to do. You can choose to ignore them but then, you can choose to not look both ways before crossing the street, too. At some point, there will likely be a problem. OP: Look at the error messages and look at your code. The message says all those errors are on line 8. My guess is this code at the beginning of your script: $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (!$_post[$field]) { $warnings[$field] = "Required"; } } that IF statement is checking each of those elements in the POST array. You need to check to see if they are set. They will not be set when the page is first requested (before it is submitted). Try if (isset($_POST[$field])). But, TEXT fields will always be set when submitted even if the user left them blank. So you can check for empty, except if the user just types spaces. I usually wrap all of the validation code in an IF that checks for the SUBMIT element, but you have not given your SUBMIT element a name. You could wrap it in a check of the request method: if ($_SERVER['REQUEST_METOD'] == 'POST') { $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (isset($_POST[$field])) { $check = trim($_POST[$field]); if (empty($check) $warnings[$field] = "Required"; } } } i've tried this $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (isset($_POST[$field])) { $warnings[$field] = "Required"; } } and got another error message as this Notice: Undefined variable: warnings in C:\xampp\htdocs\form.php on line 21 and this is what that line 21 is saying if (count($warnings) > 0) Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394440 Share on other sites More sharing options...
mrMarcus Posted November 22, 2012 Share Posted November 22, 2012 $warnings has not yet been defined. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394444 Share on other sites More sharing options...
Beeeeney Posted November 22, 2012 Share Posted November 22, 2012 i've tried this $required=array("fname" => "Firstname", "lname" => "Lastname", "email" => "Email address", "password" => "Password"); foreach ($required as $field => $label) { if (isset($_POST[$field])) { $warnings[$field] = "Required"; } } and got another error message as this Notice: Undefined variable: warnings in C:\xampp\htdocs\form.php on line 21 and this is what that line 21 is saying if (count($warnings) > 0) That says "If there is something in the $warnings variable.." then you would tell it to do something. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394445 Share on other sites More sharing options...
jazzman1 Posted November 22, 2012 Share Posted November 22, 2012 You have erros too in your script! Why are you using a $_post global varible instead of $_POST? Ereg() and eregi() are deprecated from php 5.2 (I think), do you know that? Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394451 Share on other sites More sharing options...
DavidAM Posted November 22, 2012 Share Posted November 22, 2012 @David, depend of the notice. It's not possible the language to know that you made a logical error, right? It is true that the language cannot know that $margin = $cost / $sales; is the wrong formula (a logic error). However, PHP issues a NOTICE or WARNING when I do some things wrong: $margn = $cst / $sales;. Oops! I spelled two of those variable names wrong. PHP will execute the statement, and issue a NOTICE, and set the value of $margn to null. Later on when I print $margin; I will get another NOTICE and PHP will print an empty value. If I ignore all these notices (or hide them), I can say my script "runs" or even "works", but it is NOT producing the correct results. By the way, in any compiled language, these notices (in my example) would be fatal compile-time errors, and you would not get an executable. The undefined index errors (from this thread) would not be, but would likely cause the program to crash when it runs. Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394458 Share on other sites More sharing options...
DavidAM Posted November 22, 2012 Share Posted November 22, 2012 and got another error message as this Notice: Undefined variable: warnings in C:\xampp\htdocs\form.php on line 21 and this is what that line 21 is saying if (count($warnings) > 0) Either define $warnings at the beginning of the script: $warnings = array() or use empty instead of count Quote Link to comment https://forums.phpfreaks.com/topic/271034-please-friends-i-have-problem-that-have-been-bordering/#findComment-1394459 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.