0

I am just trying to check if a form variable is empty. The code sets the variables $getsubject and $getsubject to the $_POST of the form, then I am checking if they are, empty and if they are I want to set them to "No Message" or "No Subject". I tried with isset as well and it didn't work. I even tried setting an else statement that does the same thing and it doesn't change it.

$getsubject = $_POST['subject'];
$getmessage = $_POST['message'];

if(empty($getsubject)) {    
    $getsubject = "<No Subject>";
}

if(empty($getmessage)){ 
    $getmessage = "<No Message>";
}
3
  • Do you ever echo $getmessage ? If not, of course it won't display. Commented Nov 4, 2013 at 18:55
  • What do you get if you try a var_dump($getsubject) after you initially assign to the variable, as well as after your empty check? Commented Nov 4, 2013 at 18:55
  • Are you using a html form? Could you show the code? Commented Nov 4, 2013 at 18:56

3 Answers 3

4

I found the problem .. the code is working - however the since there were brackets "<" and ">" ... when I retrieved the data from the SQL table, it was not appearing. Not sure why, but when I removed the brackets it worked.

Sign up to request clarification or add additional context in comments.

2 Comments

The why was because the browser saw the < > and thought you where outputting html, if you viewed source you would see it
If you want to use < and > symbols in your code, you have to use &lt; and &gt; in order for them to echo, "IF" that was your original intent.
0

If you are not sure id the data from the form exist you must use !isset to check it before you declare the variables, so:

if(!isset($getsubject)) {    
    $getsubject = "<No Subject>";
}
else{
$getsubject = $_POST['subject'];
}

if(!isset($getmessage)){ 
    $getmessage = "<No Message>";
}
else{
$getmessage = $_POST['message'];
}

The data from $_POST['subject'];, for example, might not exist, and if you declare it php will give you an error

Comments

-2

I suggest that you use a full if conditional to display the results you are looking for combined with html.

IF subject has content then echo Great subject ELSE then echo No Subject END IF

IF message has content then echo Thank You for the message ELSE then echo No Messgae was entered END IF

^this is the basic logic not the code Also look into the trim php code which will trim blank space off of the submission. This helps eliminate blank responses or spaces counting as characters (will not remove space between characters)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.