Jump to content

Recommended Posts

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 by stutego

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 by mrMarcus

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 by stutego

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

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.

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

 

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";
   }
 }
}

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)

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.

@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.

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.