Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

best way to check if button pressed and text box as no value

Posted on 2006-06-15
11
Medium Priority
?
1,102 Views
Last Modified: 2008-03-06
Hi what is the best way to check an empty text box

at the moment im using

if ($_POST['sel_search'] && $_POST['prt_list']==""){

}
            elseif ($_POST['prt_list']!=""){

}


thanks in advance
0
Comment
Question by:avo42
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 9

Expert Comment

by:LinuxNubb
ID: 16916126
I'm not sure you can get much better than what you have.
0
 
LVL 29

Assisted Solution

by:TeRReF
TeRReF earned 200 total points
ID: 16916173
Well, to rule out spaces, you could use:
if ($_POST['sel_search'] && trim($_POST['prt_list'])==""){

}
          elseif (trim($_POST['prt_list'])!=""){

}
0
 
LVL 7

Assisted Solution

by:ljubiccica
ljubiccica earned 200 total points
ID: 16916183
Hey...

i would use
if(isset($_POST['sel_search']) && $_POST['prt_list']=="")

Ljubiccica
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 8

Assisted Solution

by:Autogard
Autogard earned 200 total points
ID: 16916185
My only suggestion would to stay consistent in your code.  If you are checking == "" and != "" I would also use:

if ($_POST['sel_search'] != ""
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 16917607
I would suggest:

if(isset($_POST['sel_search']) && !empty($_POST['prt_list'])) {
}

-r-
0
 
LVL 6

Assisted Solution

by:ixti
ixti earned 400 total points
ID: 16917789
In addition to comments above:

1. If form was submitted from your site and form has text box named 'my_text', then even i it will be empty on submit, then it will be always set. So you don't have to check every field is it was set or not. I mean:
  ---
  <?php
    // We need to check if one of fields were submitted only once, just to be sure
    // if form was submitted. You can give a name to submit button, so you'll be able
    // to check if form was submitted, but for me it's much easier to check one of
    // input fields, just because i never use value of submit buttons.
    if (isset($_POST['var1'])) {
      $error = array();
      if ('' == trim($_POST['var1'])) $error[] = 'Var1 is empty';
      if ('' == trim($_POST['var2'])) $error[] = 'Var2 is empty';
      if ('' == trim($_POST['var3'])) $error[] = 'Var3 is empty';
      if ('' == trim($_POST['var4'])) $error[] = 'Var4 is empty';
      if (0 < count($error)) {
        foreach($error as $msg) echo 'Error: ' . $msg . '<br />\n';
      } else {
        // ... here we can do any actions with submitted data ... :))
      }
    }
    ?>

  <form method="POST">
  <input type="text" name="var1" />
  <input type="text" name="var2" />
  <input type="text" name="var3" />
  <input type="text" name="var4" />
  <input type="submit" value="submit" />
  </form>
  ---

2. The main what you have to do is to check where from was processed from, just to encrease security. I suggest you to read PHP Security Guide (http://phpsec.org/projects/guide/) for getting more info about security in PHP.

3. You can use my simple class to process forms: http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21776115.html#16424179 It's undocumented but it's really simple, and you'll find an example there wich will explain almost everything :))
0
 
LVL 5

Assisted Solution

by:aminerd
aminerd earned 300 total points
ID: 16917856
Also, remember that == does not compare types; consider:

$test = 0;
if ($test == '') echo 'Nothing';

$test = NULL;
if ($test == '') echo 'Nothing';

$test = FALSE;
if ($test == '') echo 'Nothing';

You'll get three "Nothing"s. NULL and FALSE may not be an issue, but if you need to allow for zeros, you'll want to try a different route. For instance, the === operator, which IS type-sensitive.
0
 
LVL 6

Expert Comment

by:ixti
ID: 16917931
aminerd,
Values processed by form is always STRINGS. So if you'll use type-sensitive === operator then you'll have some troubles.

For example:
<form>
<input type="text" name="int" />
<input type="submit" value="go" />
</form>

if you'll enter 123 to this form and press submit button, you'll have:
$_GET['int'] = '123';
not integer 123, but string '123':

<?php
if ($_GET['int'] === 123) { echo 'This won't work anyway!'; }
if ($_GET['int'] === '123') { echo 'This works!'; }
if ($_GET['int'] == '123') { echo 'And this works!'; }
if ($_GET['int'] == 123) { echo 'And this works too!'; }
?>

If you will not input anything in that field, then $_GET['int'] will be '', not NULL:
<?php
if ($_GET['int'] === NULL) { echo 'This won't work anyway!'; }
if ($_GET['int'] === '') { echo 'This works!'; }
if ($_GET['int'] == '') { echo 'And this works!'; }
?>

And if you'll put false (or true) in that field, it will also be 'false' or 'true', noy bollean false ore true:
<?php
if ($_GET['int'] === false) { echo 'This won't work anyway!'; }
if ($_GET['int'] == false) { echo 'This won't work too!'; } // Note this!
if ($_GET['int'] === 'false') { echo 'This works!'; }
if ($_GET['int'] == 'false') { echo 'And this works!'; }
?>
0
 
LVL 2

Assisted Solution

by:mazzachre
mazzachre earned 200 total points
ID: 16918809
If you are concerned about performance on the server I would use a small javascript to test the fields before submitting the form.
0
 

Author Comment

by:avo42
ID: 16919833
Hi all

what would be the simplyes method to do that as i dont know javascript at all

thanks all for your input
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 16919848
You cannot rely on javascript solely. Mazzachre should know better.

There are plenty people who are not using javascript and therefor can enter unvalidated data unless you also validate serverside.

-r-
0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question