Jump to content

Checking if variable is empty


stjonesMSI
Go to solution Solved by Ch0cu3r,

Recommended Posts

Hello.  Novice PHP coder here.

 

I am working on a web form.  I would like to check on one field (that I put into a variable) if that form field is empty when the user pressed SUBMIT, and if so, to put a text string into the variable for that field content.

 

In looking at the PHP manual and examples online, I have

if (empty(trim($VARIABLE_NAME_HERE))) {
    $VARIABLE_NAME_HERE = "Put text here";
}

When I test it with contents in the field, I get the contents in the email  But when I test with leaving the form blank, I get a blank in the email and not the text "Put text here".  What am I missing?

 

I tried using is_null as well and have the same problem. 

 

Thanks!

 

Steve

Link to comment
Share on other sites

 

But when I test with leaving the form blank, I get a blank in the email and not the text "Put text here".  What am I missing?

Without seeing your code it is hard to tell you why it this is. I suspect you are using the wrong variable? Or you are changing the variables value after you send the email.

Link to comment
Share on other sites

The PHP file is over 700 lines of code, so it's a bit much to post.

 

If I leave those three lines in and put some text into the form field and then hit SUBMIT, I get the field contents emailed to me just fine.  If I comment out those 3 lines out and put some text into the form field and then hit SUBMIT, I get the field contents emailed to me just fine again.  If I comment out those 3 lines out and leave blank the form field and then hit SUBMIT, I get the field contents emailed to me just fine again - it's just blank (like it should be). BUT - if I leave those three lines IN and leave the form field BLANK, upon SUBMIT my web form goes to a blank web page (rather than the "thanks for submitting" page it does for all the other scenarios) and I don't get an email.

 

Could it be something abut empty vs. is_null vs. isset?  it seems all these test for a variable's content?  Is one preferred over the other?

 

Thanks!

 

Steve

Link to comment
Share on other sites

Yep - has that exact code in it and it has caught lots of errors over the past couple of weeks as I'm learning this.  But in this case no errors appear on the web page that opens.  It's totally blank.

 

Steve

Edited by stjonesMSI
Link to comment
Share on other sites

 

The PHP file is over 700 lines of code, so it's a bit much to post.

Only need to post the relavent code. I recommend posting three lines you are commenting out and the 10 lines before it

 

 

BUT - if I leave those three lines IN and leave the form field BLANK, upon SUBMIT my web form goes to a blank web page

If you are getting blank page then PHP has encountered an unrecoverable error. Those two lines should force PHP to show errors. If not then look at your servers error logs

Edited by Ch0cu3r
Link to comment
Share on other sites

Here is the code with the final variable names with lots of lines between deleted.  The problematic 3 lines are commented out in this piece and marked in red.  Also, in all the places it reads "EMAIL ADDRESS REMOVED" in the code on our server there is a real email address there.

<html>
<head>
<title>Sending HTML email using PHP</title>
</head>
<body>
<?php
 

error_reporting(E_ALL);
ini_set('display_errors', '1');
 


if($_SERVER['REQUEST_METHOD'] == "POST")


$to = "EMAIL ADDRESS REMOVED" ;
//$to = "EMAIL ADDRESS REMOVED" ;        
$from = "EMAIL ADDRESS REMOVED";     
//$cc = "EMAIL ADDRESS REMOVED";
$subject = "ONLINE MSI JOB APPLICATION FORM";

//$message = "Testing 123.\r\n\r\nwww.morgansmithllc.com\phpdave.php";

$APPLICANT_NAME = $_POST['applicant_name'];
$APPLICANT_NAME = strip_tags($APPLICANT_NAME);
$APPLICANT_ADDRESS = $_POST['applicant_address'];
$APPLICANT_ADDRESS = strip_tags($APPLICANT_ADDRESS);
$APPLICANT_CITY = $_POST['applicant_city'];
$APPLICANT_CITY = strip_tags($APPLICANT_CITY);
$APPLICANT_STATE = $_POST['applicant_state'];
$APPLICANT_STATE = strip_tags($APPLICANT_STATE);
$APPLICANT_ZIP = $_POST['applicant_zipcode'];
$APPLICANT_ZIP = strip_tags($APPLICANT_ZIP);
$APPLICANT_HOME_PHONE = $_POST['applicant_home_phone'];
$APPLICANT_HOME_PHONE = strip_tags($APPLICANT_HOME_PHONE);
$APPLICANT_MOBILE_PHONE = $_POST['applicant_mobile_phone'];
$APPLICANT_MOBILE_PHONE = strip_tags($APPLICANT_MOBILE_PHONE);
$APPLICANT_EMAIL = $_POST['applicant_email'];
$APPLICANT_EMAIL = strip_tags($APPLICANT_EMAIL);
$APPLICANT_SOCIAL_SECURITY = $_POST['applicant_social_security'];
$APPLICANT_SOCIAL_SECURITY = strip_tags($APPLICANT_SOCIAL_SECURITY);
//if (empty(trim($APPLICANT_SOCIAL_SECURITY))) {
//    $APPLICANT_SOCIAL_SECURITY = "No answer entered";
//}

$APPLICANT_DRIVERS_LICENSE = $_POST['applicant_drivers_license'];
$APPLICANT_DRIVERS_LICENSE = strip_tags($APPLICANT_DRIVERS_LICENSE);

-- LOTS OF DATA PULLED FROM FORMS AND PUT INTO VARIABLES AND EMAIL FORMATTED WITH TEXT AND THE VARIABLES DELETED -- 

";







$headers = "MIME-Version: 1.0" . "\r\n";

$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";

//$headers .= "Content-type:text/plain;charset=iso-8859-1" . "\r\n";

$headers .= "From: " . $from . "\r\n" ."Reply-To: " . $to . "\r\n" . 'X-Mailer: PHP/' . phpversion();

$retval = mail($to, $subject, $message, $headers);
//$retval = mail($cc, $subject, $message, $headers);


echo "<br>";

 if( $retval == true )
   {
      //echo "Message sent successfully to >".$to."<";
      echo "<h1>Thank you!</h1>\r\n<br>";
      echo "Your application for employment has been successfully transmitted to <strong>Morgan Smith Industries</strong>.<br>";
   }
   else
   {
      echo "Message could not be sent to >".$to."<";
   }

echo "<br<br><br>";

echo date("D M d, Y G:i a");
echo "<br><br>";

// phpinfo();

?>
</body>
</html>
Link to comment
Share on other sites

  • Solution

If you are not running PHP5.5 then you cannot use trim with empty

 

Quote from php.net/empty

 

 

Note:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

 

So you need to first trim the data and then pass the data to empty

$APPLICANT_SOCIAL_SECURITY = trim($_POST['applicant_social_security']); // trim applied here
$APPLICANT_SOCIAL_SECURITY = strip_tags($APPLICANT_SOCIAL_SECURITY);
if (empty($APPLICANT_SOCIAL_SECURITY)) {
    $APPLICANT_SOCIAL_SECURITY = "No answer entered";
}
Link to comment
Share on other sites

unfortunately, someone in one of the OP's other threads made a suggestion that would have produced a trimmed copy of all the input data at once, and paved the way to processing all the form values as a set, instead of writing out repeated code for each separate input value -

 

instead, use an array for the errors and and array for the form data.

...

initialize the $data array by making a copy of the $_POST array, trimming each element (some people allow leading/trailing white-space characters in password type fields.) if you are submitting arrays data in the post data, you can use a recursive user written trim function with array_walk_recursive() to operate on all elements of the submitted post data. then, use elements in the $data array everyplace you reference post data - $data['some_field_name']

 

Link to comment
Share on other sites

 

If you are not running PHP5.5 then you cannot use trim with empty

 

Quote from php.net/empty

 

So you need to first trim the data and then pass the data to empty

$APPLICANT_SOCIAL_SECURITY = trim($_POST['applicant_social_security']); // trim applied here
$APPLICANT_SOCIAL_SECURITY = strip_tags($APPLICANT_SOCIAL_SECURITY);
if (empty($APPLICANT_SOCIAL_SECURITY)) {
    $APPLICANT_SOCIAL_SECURITY = "No answer entered";
}

Thank you!  That did the trick!

Link to comment
Share on other sites

unfortunately, someone in one of the OP's other threads made a suggestion that would have produced a trimmed copy of all the input data at once, and paved the way to processing all the form values as a set, instead of writing out repeated code for each separate input value -

I am a novice at PHP, as I have stated in pretty much all of my posts.  I am learning my way through it on this current project.  I realize my coding is not as efficient as it would be had I been doing it for years or were it my major field of study or employment.  I am taking one piece / step at a time and trying to not only "do it" but also understand each baby step so I can actually LEARN the language.  I know what an array is - but I am not trying to do anything with arrays now.  I am trying to learn what I need at the moment.  I will get to arrays.  And then once I learn about them and PHP I will revisit my code an update it.  But as of right now, the text you quote and chastise me for not pursuing is 80% gobbley-gook to me.  I find it extremely frustrating that when a question gets asked (and not just in this PHP Forum but other forums for other products I use) that someone always answers a question by not actually answering the question but instead either a) heaping on other ways of doing things that they think are better/more advanced/more appropriate while providing no real assistance to the person asking the question. or b) chastise the person asking the question for not using a previous response that was not helpful to the poster.  I am 52 years old.  I taught as a tenured college professor for 15 years.  I am not stupid.  Early in my teaching career I realized that you respond to each student's question AT THEIR LEVEL.  I am not trying to be ungrateful - I am just frustrated with responses to questions that are posted that are not really answered.  Above, "Ch0cu3r" gave a very helpful and exact answer to what I asked at a level that was simple and direct for a novice and didn't tell me to jump to a more advanced level of knowledge to fix the problem at hand that simply would have made the task even harder to complete.

Link to comment
Share on other sites

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.