Problem with "if"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Problem with "if"

Post by Nicole »

Hi, I have an "if" statement that says pop up these forms if such and such field is empty.

if ($_POST['text']==""){echo "<form method="post" action="<?php $_SERVER['PHP_SELF'] ;?>">
<input type='text' name='text' size=20>and other input fields with other names below this. Then the submit.
<input type='submit' name='submit' value='Submit Form' /></form>

Thing is, if the text fields is filled in but other fields aren't, none of it will send to my email. Is there some other If I can use so that no matter what fields are filled the email will still send?
I can't use
if ($_POST), I have it elsewhere on my page and it conflicts with the other.

I am not listing all my code because its too big and this is the only part causing the problem. I can tell its the problem because when I put one of the other input fields in here instead if ($_POST['anotherformfield']=="") and then fill in the field and send the form, it sends to my email. If I leave 'anotherformfield' empty and fill in any of the other input fields, it won't send to my email.

Anything I can do for a fix? Thank you very much.
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Code: Select all

<?php
if (empty($_POST['text'])){
   echo "<form method="post" action="$_SERVER[PHP_SELF]">
   <input type='text' name='text' size=20>and other input fields with other names below this. Then the submit.
   <input type='submit' name='submit' value='Submit Form' /></form>";
}
?> 
And it still could be another part of your file.. and i had some trouble since i don't know how it sends to your mail.
Nicole wrote:Thing is, if the text fields is filled in but other fields aren't, none of it will send to my email.
Are you checking all of them? or just the one..

example..

Code: Select all

<?php
if(!empty($_POST['text']) && !empty($_POST['text2'])){
  sendmail();
}
?>
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

I'm not quite sure what the problem is exactly. But what helps often in cases like this, is to write it down in pseudo code first, without the real code. So something like:

Code: Select all

if form is submitted process it
  check if field X is valid
    if not do A
    if yes do B
if form has not been submitted / else
  show form
If you get the logic right writing it down like this, implementing the real code is not difficult anymore.
Nicole
Forum Newbie
Posts: 22
Joined: Thu Oct 26, 2006 7:47 am

Post by Nicole »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Zox, I don't know what your first example means. Can you tell me what it is checking for? Its not working at all now. Is it saying if something is in the text field, then send to the email? 

Also, I'd really rather not put all the form fields into the "if" statement, is that the only other way? Here's more code to hopefully help. Thanks a bunch.

Row field taken from database and inserted into $row[email] all works fine. Just if any other form field is filled but 'text' form field is empty, email won't send. If 'text' form field is filled, email sends.

Code: Select all

while ($row = mysql_fetch_array($result)){
if ($_POST['text']==""){?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ;?>">
<input type='text' size=20 name='sendingto' value="<?php echo $row[email];?>" /> 
<input type='text' name='text' size=20><br /><br />
<input type='text' name='comments' size=20><br /><br />
<input type='submit' name='submit' value='Submit Form' /></form>
<br /><br />
<?php 
}
else{
$mailto = $_POST['sendingto'] ; 
$subject = 'Email Subject' ;
$text = $_POST['text'] ; 
$comments= $_POST['comments'] ;
$uself = 0;
$sep = (!isset( $uself ) || ($uself == 0)) ? "\r\n" : "\n" ;
$messageproper = "Email body.\n";
mail($mailto, $subject, $messageproper, "From: \"$text\" <$text>" . $sep . "Reply-To: \"$text\" <$text>" . $sep);
echo "email sent";
}
}
}
?>
I think there must be another way, please let me know, thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Nicole, I'm not going to ask nicely again: please start using the syntax highlighting tags provided by the forum.
Post Reply