|
-
Mar 30th, 2004, 04:32 PM
#1
Thread Starter
Frenzied Member
If (!$a) - undefined index [resolved]
I have the following code:
PHP Code:
<?
$HTML1 = "You haven't filled in all the fields. I'll redirect you to my contact form in a couple more seconds.<script type='text/javascript'>function a() { location.href = 'contact.php' } window.setTimeout('a()',6000)</script>";
$HTML2 = "The mail was sent successfully. I'll redirect you to my index page now.<script type='text/javascript'>function a() { location.href = 'index.php' } window.setTimeout('a()',6000)</script>";
$HTML3 = "There has been an error posting the email, please try again.<script type='text/javascript'>function a() { location.href = 'contact.php' } window.setTimeout('a()',6000)</script>";
$a = $_POST['name'];
$b = $_POST['subject'];
$c = $_POST['body'];
if ((!$a) || (!$b) || (!$c))
{
//They have not filled in all fields Send them back
print $HTML1;
}
else
{
if (mail("[email protected]",$b,$c,"From " . $a))
{
print $HTML2;
}
else
{
print $HTML3;
}
}
?>
The page which sends the data has nothing wrong with it. Even if it did, this page is supposed to realise that.
When I run this, I always get:
Undefined index: name
Undefined index: subject
Undefined index: body
on lines 5,6 and 7. Which is where I make a variable equal them. What does error mean, what should I do to fix this?
edit:
after doing this, it prints $HTML1, even though I have filled in all the fields on the previous page.
Last edited by Acidic; Apr 2nd, 2004 at 10:31 AM.
Have I helped you? Please Rate my posts. 
-
Mar 30th, 2004, 05:53 PM
#2
I'm thinking that you have a setting enabled in your PHP ini to show warning of undefined indexes. More "proper" code would be to do:
PHP Code:
if(isset($var)){
/* OR */
if(!empty($var)){
Since your POST variables do not exist because the form was not submitted, you're assigning variables to unexistant globals. When your form IS submitted, it will work fine.
An easy way around this without using lots of if statements is:
PHP Code:
$a = (isset($_POST['var'])) ? $_POST['var'] : "";
Which will assign $a to $_POST['var'] if it is set, and to a blank string if it doesn't.
Remember, isset() checks if the variable EXISTS, not if it has a value. empty() will check whether or not it is a null string.
Hope that helps you out.
-
Mar 31st, 2004, 06:38 AM
#3
Thread Starter
Frenzied Member
thanks, I'll test this when I get home.
But I had submitted the frm, and I still got those results.
method="post" (jn the form) right?
Have I helped you? Please Rate my posts. 
-
Mar 31st, 2004, 11:42 AM
#4
Thread Starter
Frenzied Member
I've tried your code and I still can't get it working. I tried print $a; right after setting it equal to $_POST['name'];. I don't get anything even thuogh I should do. This made me think my HTML form was incorrect, but I really can't see where, here's it's code:
Code:
<form action="send_mail.php" method="post" enctype="text/plain">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">If you are expecting me to reply, then don't forget to include your e-mail address in the body.</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
In the send_mail.php file I have changed the IF statement to this:
PHP Code:
if (empty($a) || empty($b) || empty($c))
Still no success. It does exactly what it did before.
Have I helped you? Please Rate my posts. 
-
Mar 31st, 2004, 02:19 PM
#5
ok, READ over my post. I said you can't assign a variable to a non-existant variable. Your post variables don't exist, so you can't assign them to $a, $b, or $c. You have to check whether they exist BEFORE you assign $_POST['whatever'] to $a, along with your other variables that rely on POST information.
Your HTML looks fine.
-
Mar 31st, 2004, 02:42 PM
#6
Thread Starter
Frenzied Member
OK, thanks, now I don't get any errors. But it seems to always fill the criteria for the IF statement (not the else), even when I have filled in all the fields on the previous page. Here's my IF statement:
PHP Code:
if (!isset($_POST['name']) || !isset($_POST['body']) || !isset($_POST['subject']))
{
//They have not filled in all fields Send them back
print $HTML1;
}
else
{
...
Have I helped you? Please Rate my posts. 
-
Mar 31st, 2004, 07:29 PM
#7
does this script work correctly for you? try running it on your server..
PHP Code:
<?
if(!isset($_POST['a']) || !isset($_POST['b'])){
echo "<form method=post>\n";
echo "<input type=hidden name=a value=aaaa>\n";
echo "<input type=hidden name=b value=bbbb>\n";
echo "<input type=submit value=go>\n";
echo "</form>\n";
}else{
echo "<pre>\n";
print_r($_POST);
echo "</pre>\n";
}
?>
if when you run it it displays a "go" button, click it, and afterwards, it should print this:
Code:
Array
(
[a] => aaaa
[b] => bbbb
)
it should work, seeing as how it works fine on my PC.
if it works and your script doesn't work, post your entire source.. along with whatever you're using to submit stuff.
Last edited by kows; Mar 31st, 2004 at 07:33 PM.
-
Mar 31st, 2004, 07:41 PM
#8
nevermind about the source stuff.. i realized you'd already posted everything i need.. i put it your original script in a file with the html you posted, and then i added the isset() stuff to it that you posted recently.. try this out, changed some of your php code because it was ugly, though. it worked fine for me.
PHP Code:
<?
$HTML1 = <<< EOF
<!--<form action="send_mail.php" method="post" enctype="text/plain">-->
<!--removed your original to preserve it and not have to change your action-->
<form method="post">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">dont forget your email - replaced your original message because it kept screwing up my IDE when using EOFs</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
EOF;
$HTML2 = "The mail was sent successfully. Ill redirect you to my index page now.";
$HTML3 = "There has been an error posting the email, please try again.";
if(!isset($_POST['name']) || !isset($_POST['body']) || !isset($_POST['subject'])){
echo $HTML1;
}else{
echo "done!";
/*
if(mail("[email protected]",$b,$c,"From " . $a)){
print $HTML2;
}else{
print $HTML3;
}
*/
}
?>
-
Apr 1st, 2004, 05:33 AM
#9
<?="Moderator"?>
Hey can u tell more about this code as cant find any info in the PHP Manual about it?
PHP Code:
$HTML1 = <<< EOF
....
EOF;
-
Apr 1st, 2004, 06:01 AM
#10
-
Apr 1st, 2004, 11:52 AM
#11
Thread Starter
Frenzied Member
OK I've tried this now:
PHP Code:
<html>
<form action="contact.php" method="post" enctype="text/plain">
<?
$HTML1 = <<< EOF
<!--<form action="send_mail.php" method="post" enctype="text/plain">-->
<!--removed your original to preserve it and not have to change your action-->
<form method="post">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">dont forget your email - replaced your original message because it kept screwing up my IDE when using EOFs</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
EOF;
$HTML2 = "The mail was sent successfully. Ill redirect you to my index page now.";
$HTML3 = "There has been an error posting the email, please try again.";
if(!isset($_POST['name']) || !isset($_POST['body']) || !isset($_POST['subject'])){
echo $HTML1;
}else{
echo "done!";
/*
if(mail("[email protected]",$b,$c,"From " . $a)){
print $HTML2;
}else{
print $HTML3;
}
*/
}
?>
</form>
</html>
The file name is "contact.php", so it is directed towards itself. This should then display done if done, but it always displays the form. Which I suppose means the form can't be read.
About the earlier code I should try, could you please explain how I should try it. is that all the source for the page? Does the form automatically target itself?
Have I helped you? Please Rate my posts. 
-
Apr 1st, 2004, 12:13 PM
#12
You need to bracket out each logical statement. ! takes precedance over ||:
PHP Code:
if((!isset($_POST['name'])) || (!isset($_POST['body'])) || (!isset($_POST['subject']))) {
echo $HTML1;
}else{
echo "done!";
}
-
Apr 1st, 2004, 12:19 PM
#13
You've also got an error in your HTML. You need to give your form an action attribute.
I'm not sure why you have two. However the first one is not needed:
Code:
<html>
<form action="contact.php" method="post" enctype="text/plain">
<?
$HTML1 = <<< EOF
<!--<form action="send_mail.php" method="post" enctype="text/plain">-->
<!--removed your original to preserve it and not have to change your action-->
<form method="post" action="contact.php">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">dont forget your email - replaced your original message because it kept screwing up my IDE when using EOFs</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
EOF;
$HTML2 = "The mail was sent successfully. Ill redirect you to my index page now.";
$HTML3 = "There has been an error posting the email, please try again.";
if(!isset($_POST['name']) || !isset($_POST['body']) || !isset($_POST['subject'])){
echo $HTML1;
}else{
echo "done!";
/*
if(mail("[email protected]",$b,$c,"From " . $a)){
print $HTML2;
}else{
print $HTML3;
}
*/
}
?>
</form>
</html>
Add the bit in blue and remove the bits in red and it should work fine. If you don't put a action attribute in the form then it won't submit anywhere.
-
Apr 1st, 2004, 12:25 PM
#14
Thread Starter
Frenzied Member
OK, I got rid of what you had in red, added what was in blue and changed the IF as you said, I still get "done!"
ALWAYS.
Have I helped you? Please Rate my posts. 
-
Apr 1st, 2004, 12:37 PM
#15
Originally posted by Acidic
OK, I got rid of what you had in red, added what was in blue and changed the IF as you said, I still get "done!"
ALWAYS.
Well that should work. It works on my system.
Maybe your browser is caching the post variables. Try closing it and re-opening and going to the page again.
-
Apr 1st, 2004, 01:11 PM
#16
Thread Starter
Frenzied Member
I tried closing and re-opring the browser. I tried a different browser (IE). Still same thing.
I'll try uploading it (as opposed to localhost)
Have I helped you? Please Rate my posts. 
-
Apr 1st, 2004, 01:26 PM
#17
Thread Starter
Frenzied Member
it still always says done
Have I helped you? Please Rate my posts. 
-
Apr 1st, 2004, 05:11 PM
#18
I don't understand why. That exact code works for me.
I would suggest putting some debugging statements in the code and double check for any errors.
-
Apr 1st, 2004, 05:20 PM
#19
Thread Starter
Frenzied Member
I'm totally stumped too (read comments on why). Here's the whole code:
PHP Code:
<html>
<?
$HTML1 = <<< EOF
<form method="post" action="contact.php">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">Remember to include your email address if you are expecting a reply.</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
EOF;
$HTML2 = "The mail was sent successfully. Ill redirect you to my index page now.";
$HTML3 = "There has been an error posting the email, please try again.";
if ((!isset($_POST['name'])) || (!isset($_POST['body'])) || (!isset($_POST['subject']))) {
echo $HTML1;
}else{
/*
echo $_POST['name'];
echo $_POST['body'];
echo $_POST['subject'];
When I include the above three lines, and fil in the form (all the fields), it gives no errors
the variables are displayed, then done is displayed too.*/
echo "done!";
}
/*
if(mail("[email protected]",$b,$c,"From " . $a)){
print $HTML2;
}else{
print $HTML3;
}
*/
?>
</html>
They seem to be set fine, but the IF doesn't pick it up.
Have I helped you? Please Rate my posts. 
-
Apr 1st, 2004, 07:03 PM
#20
try running just the script i gave you.. nothing else. don't add anything, take anything away, or even modify it.. just run it. it works fine for me, and that's the only reason i gave you it.
you could also change your if statement to just do this to get around any operators having a higher priority over another:
PHP Code:
if(!isset($_POST['name'], $_POST['body'], $_POST['subject'])){
echo $HTML1;
}else{
echo "done!";
}
not sure if that will help you though, since the script i gave you works completely fine for me. it's mostly just a space saver..
also: if you don't have an action in your form, it defaults to submitting to itself, that's why i don't have one.
-
Apr 2nd, 2004, 08:06 AM
#21
Thread Starter
Frenzied Member
OK I had my previous form, directed it to send_mail.php
then I made send_mail.php:
PHP Code:
<?
if(!isset($_POST['name'], $_POST['body'], $_POST['subject'])){
echo $HTML1;
}else{
echo "done!";
}
?>
EXACTLY as you said. I filled no forms and it said done.
Have I helped you? Please Rate my posts. 
-
Apr 2nd, 2004, 10:00 AM
#22
<?="Moderator"?>
hey have you tried using
PHP Code:
<?
if(!isset($_REQUEST['name'], $_REQUEST['body'], $_REQUEST['subject'])){
echo $HTML1;
}else{
echo "done!";
}
?>
-
Apr 2nd, 2004, 10:08 AM
#23
Thread Starter
Frenzied Member
I just tried that, no change. I also changed _REQUEST to _POST, still nothing.
Have I helped you? Please Rate my posts. 
-
Apr 2nd, 2004, 10:13 AM
#24
Is your page live or just on a localhost. Is it possible to post a link to the form so we can see exaclty whats going on?
-
Apr 2nd, 2004, 10:20 AM
#25
Thread Starter
Frenzied Member
I've just shoved them online (friends domain)
here
So you know the PHP, here it is:
PHP Code:
<html>
<?
$HTML1 = <<< EOF
<form method="post">
<table summary="the contact form">
<tr>
<td>
<p>Your name:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="name" />
</td>
</tr>
<tr>
<td>
<p>The subject:</p>
</td>
<td>
<input type="text" style="width: 200px;" name="subject" />
</td>
</tr>
</table>
<br />
The Body:<br />
<textarea name="body" cols="50" rows="10"></textarea><br />
<font size="-2">Remember to include your email address if you are expecting a reply.</font><br />
<input type="submit" value="Send E-mail (Alt-s)" accesskey="s">
</form>
EOF;
$HTML2 = "The mail was sent successfully. Ill redirect you to my index page now.";
$HTML3 = "There has been an error posting the email, please try again.";
if ((isset($_POST['name'])) && (isset($_POST['body'])) && (isset($_POST['subject'])))
{
/*
echo $_POST['name'];
echo $_POST['body'];
echo $_POST['subject'];
When I include the above three lines, and fil in the form (all the fields), it gives no errors
the variables are displayed, then done is displayed too.*/
echo "done!";
}
else
{
echo $HTML1;
}
/*
if(mail("[email protected]",$b,$c,"From " . $a)){
print $HTML2;
}else{
print $HTML3;
}
*/
?>
</html>
I've changed it back to one page, easier to read.
Last edited by Acidic; Apr 2nd, 2004 at 10:24 AM.
Have I helped you? Please Rate my posts. 
-
Apr 2nd, 2004, 10:25 AM
#26
<?="Moderator"?>
how about checking the length of the values
PHP Code:
<?
if((strlen($_POST['name'])>0) && (strlen($_POST['body'])>0) && (strlen($_POST['subject'])>0)){
echo $HTML1;
}else{
echo "done!";
}
?>
-
Apr 2nd, 2004, 10:31 AM
#27
Thread Starter
Frenzied Member
That works, thank you soo much. I had to change > to ==, so that it does what I want it to do, but thank you soo much.
Thanks to all of you for the time and effort involved.
hehe, I'll most probably be back when I can't get the email working, but that's a whole new thread.
edit: Actualyl the e-mail worked too, not on localhost, but from the server it did work.
Last edited by Acidic; Apr 2nd, 2004 at 10:42 AM.
Have I helped you? Please Rate my posts. 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|