Jump to content

Simple if (isset())


dyna1118

Recommended Posts

First off - Not trying to be an ahole but what kinda coding BB won't paste test from a text editor or IDE. I simply can't paste my code in here.

 

I don't code any one language continuously, any Lang when I need it, so I am no expert. The file that I uploaded, since I can't paste into this pane anyway possible. The output of this is either Brand: or Brand: (whatever you enter into the form). I doesn't evaluate to false like you would think it is suppose to. My ultimate goal is to print (echo) only is the form field is filled in. e.G Brand: Sony. If the field is not filled in, I want no output. With this code I get Brand:. it never evaluates completely to false (else - Not Set). Again I don't want anything at all if the form field is left blank, but I am getting Brand: Can someone please explain isset or provide a simple solution. And Maybe how to paste code into this pane. I have tried everything. or maybe they don't want code pasted here. It is my first time here.

 

isset.txt

Link to comment
Share on other sites

first of all, there's no issue posting code from a programming editor -

if (isset($_POST['brand'])) {
          print "BRAND: " . $brand . "<br />";   
  
          } else {
  
          print "NOT SET";  
          }

you would need to state how you were attempting to post the code and what sort of problem or error you got in order get help with what was going wrong. i posted the above simply by selecting it (left-button drag over the text) and copying it (right-button. select the copy option) in my editor, then right-button, select the paste option, in the forum post where i wanted it to be placed.

 

the php isset() statement does exactly what its name implies and what the php.net documentation states, it tests if a php variable is set. other than php null values, it doesn't care what's in the variable being tested. if a form has been submitted, any named text, password, textarea, and usually submit form fields, that exist within the form tags will cause the corresponding php $_POST (or $_GET, if using a get method form or a link) variable to be be set, regardless of the value from the form field.

 

only named un-checked check-box and un-selected radio-button form fields, that are part of any form, won't be set when a form has been submitted.

 

to test what's in a php variable, you need to actually test the value in the variable. by definition, all submitted form data are string data. therefore, one method would be to test if a variable is or is not an empty string -

if($_POST['brand'] != ''){
    // the contents of the variable is not an empty string
} else {
    // the contents of the variable is an empty string
}

if you want to disallow values that consist entirely of white-space characters, use trim() on the data first.

 

you can also use empty()  to perform this test. however, php considers all the following things to be 'empty' -

 

■"" (an empty string)
■0 (0 as an integer)
■0.0 (0 as a float)
■"0" (0 as a string)
■NULL
■FALSE
■array() (an empty array)
■$var; (a variable declared, but without a value)

 

 

therefore, if a 0 is a valid value for a form field, a "0" would be in the $_POST variable, and it would be considered empty() by php.

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.