if statement not returning correctly?

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
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

if statement not returning correctly?

Post by hurdy gurdy »

I dont even know how to search for posts relating to this issue... This is boggling me.

All I want to do is check whether or not strNewsTitle and strNewsCopy is empty. Seems easy 'nuff, right? However, the if statement below should run if the fields are empty, but it doesnt. I'll illustrate below.

Its a pretty simple setup, first a form (an abbreviated example) :

Code: Select all

<form action="add_proc.php" method="post">
  <input type="text" name="strNewsTitle" size="35">
  <textarea name="strNewsCopy" rows="10" cols="33">
  <input type="submit" value="save">
</form>
Then the PHP script. I'm cutting a lot of stuff out, but this little snippet is the part causing the problems. This if statement should be run if nothing is in the strNewsTitle and strNewsCopy variables.

Code: Select all

<?
  // cutting out the DB connection stuff

  $strNewsTitle = $_POST['strNewsTitle'];
  $strNewsCopy = $_POST['strNewsCopy'];

  if ( $strNewsTitle == "" || $strNewsCopy == "" ) {
    $_SESSION['sessionMessage'] = '<span class="error">Please fill in the title and copy fields.</span>';
    header ("Location: addItem.php");
    exit;
  }

  // lots of stuff to do if the fields arent empty
?>
I've also used:

Code: Select all

if ( !$strNewsTitle || !$strNewsCopy ) {
    $_SESSION['sessionMessage'] = '<span class="error">Please fill in the title and copy fields.</span>';
    header ("Location: addItem.php");
    exit;
  }
and I've tried:

Code: Select all

if ( empty($strNewsTitle) || empty($strNewsCopy) ) {
    $_SESSION['sessionMessage'] = '<span class="error">Please fill in the title and copy fields.</span>';
    header ("Location: addItem.php");
    exit;
  }
This if statement will not execute if the fields are empty. I don't get it. Any pointers?

Thanks!
Last edited by hurdy gurdy on Sat Nov 06, 2004 4:57 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Firstly try echoing out the variables to make sure they are what you are expecting. Secondly put this in your script for debugging.

error_reporting(E_ALL);
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

Phenom wrote:error_reporting(E_ALL);
Thanks for the tip, however, the error messages aren't very revealing to me. heres the error message:

Code: Select all

Notice: Use of undefined constant strNewsTitle - assumed 'strNewsTitle' in *blah*/news/add_proc.php on line 14

Notice: Use of undefined constant strNewsCopy - assumed 'strNewsCopy' in *blah*/news/add_proc.php on line 15
I get the same errors whether the fields are empty or not.

I'm making a couple of assumptions when looking at this.. the 'constant' referred to is probably this:

Code: Select all

$strNewsTitle = $_POST[strNewsTitle];
$strNewsCopy = $_POST[strNewsCopy];
The contant is referring to the '$_POST[]' right? $strNewsTitle isn't constant in this case, am I correct?

Funny thing is, even after these errors, it still was added to the DB. :?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

your getting the undefined constant because you didnt use quotes

you did

$_POST[foo]

you should do

$_POST['foo']

but thats not your problem, like the error message said, it assumes you meant 'foo' instead of the constant foo

Code: Select all

<?php


// note that empty() will return true if the user submits string zero 0


if (empty($_POST['strNewsTitle']) || empty($_POST['strNewsCopy'])) {
    $_SESSION['sessionMessage'] = '<span class="error">Please fill in the title and copy fields.</span>';
    header ("Location: addItem.php");
    exit;
} else {
    // both fields filled out
}


?>
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

thanks,

However the if statement still doesnt seem to recognize an empty string no matter how I phrase it.

For some reason, if I dont even touch the strNewsCopy or the strNewsTitle fields in the form and then submit it. The PHP script seems to think that there is something in those fields (even ewhen there isnt) and skips over the error message and header. It should notice that the variables have nothing in them and execute the session and header statements... but it doesn't.

Any ideas.. I feel like I'm drowning in my own ignorance.. I just can't seem to get this simple little script to work.

My appreciation for any and all help

PS - oops forgot to mention that all my $_POST['*'] statements have the single quote in them. The example above was a typo, I just typed it out rather than copy/paste.

Thanks again
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not sure if it's another typo, but:
<input type="text" name="strNewsTitle" size="35">
<textarea name="strNewsCopy" rows="10" cols="33">
should be:

Code: Select all

<input type="text" name="strNewsTitle" size="35" value="">
<textarea name="strNewsCopy" rows="10" cols="33"></textarea>
If that still fails then var_dump($_POST); and post the output.
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post by rehfeld »

hmm... well the problem must be in some of other part of the code, likely a logic mistake.

how is that you are deciding whether or not it is working? by the presence of the session error message?


lets make it real simple, so you can see what is, and whats not the problem



Code: Select all

<?php

if (empty($_POST['strNewsTitle'])) {
    echo 'strNewsTitle is empty, or 0 (zero)';
} else {
    echo "<p>strNewsTitle is not empty. The value is: {$_POST['strNewsTitle']}</p>";
}

if (empty($_POST['strNewsCopy'])) {
    echo 'strNewsCopy is empty, or 0 (zero)';
} else {
    echo "<p>strNewsCopy is not empty. The value is: {$_POST['strNewsCopy']}</p>";
}

?>
hurdy gurdy
Forum Commoner
Posts: 40
Joined: Mon Jun 09, 2003 8:19 pm

Post by hurdy gurdy »

sorry about the delay.. had a killer deadline and wasn't able to come back to this for a while.

So here I am, I tried your test and the values from the form are not being sent to the process page. Some names have changed so I'll include them below:

the original form:

Code: Select all

&lt;form action="_includes/login_proc.php" method="post" enctype="text/plain"&gt;
username : &lt;input type="text" name="username" size="35"&gt;
password : &lt;input type="password" name="password" size="35"&gt;
&lt;input type="submit" value="login"&gt;
&lt;/form&gt;
and your code (altered to reflect the new field names):

Code: Select all

if (empty($_POST['username'])) {
  echo 'username is empty, or 0 (zero)';
} else {
  echo "<p>username is not empty. The value is: {$_POST['username']}</p>";
}
	
if (empty($_POST['password'])) {
  echo 'password is empty, or 0 (zero)';
} else {
  echo "<p>password is not empty. The value is: {$_POST['password']}</p>";
}
When I enter values in the form, the values are not sent to the script above. I guess I should point out that the script does not exists on the same page as the form.

Any ideas? *whimper* help *whimper*
Post Reply