Krystian
Krystian

Reputation: 85

submit empty form input field

I have several input fields and I want it to submit when there is only one field with entered values and the others are empty (updating user data). I worked by now with isset() but this only sends the form when every field is filledout:

if (isset
   ($_POST['submit']) AND 
   ($_POST['firstname']) AND 
   ($_POST['lastname']) AND 
   ($_POST['address']) AND 
   ($_POST['ZIP']) AND 
   ($_POST['phonenumber']) AND 
   ($_POST['mail']) AND 
   ($_POST['group'])
)

Later on I check in the mail template (another file) if there is a value and wheter to show it in the mail or not:

{if !empty($firstname)}{translate text='First Name'}: {$firstname|escape} {/if}

Is my idea ok or is there an easier way to solve this?

Upvotes: 0

Views: 3882

Answers (4)

ZeroZipZilch
ZeroZipZilch

Reputation: 721

Assuming you want to send the form if there's at least one field that's filled, you could use the following if-statement:

if(count($_POST) > 1)

This allows you to submit the form and have at least one field filled, but you can also have more fields filled.

If you want to send the form only if there's one field that's filled, you could change the above if-statement to the following:

if(count($_POST) == 2)

This allows you to have only one field filled. The reason I use "== 2" is because the submit-button is also something that will be sent.

If you want to allow all the fields to be empty, you can use the following if-statement:

if(count($_POST) > 0)

This would allow you to submit the button and leave all the other fields empty.

The reason this works is because $_POST is a pre-defined array-variable.

To ensure that the user only uses fields that you want them to use and still keep the code clean, you can use an array.

Do the following:

$allowed_fields = array('firstname','lastname','address','ZIP','phonenumber','mail','group');

And then just add the following to your if-statement:

if(count($_POST) == 2 AND in_array($allowed_fields, $_POST))

Upvotes: 0

Matt Clark
Matt Clark

Reputation: 28639

One suggestion would to be to use javascript and your onSubmit function on the form in addition to a serverside check. Using that, you can check all of your fields, and alert the user to fill some in BEFORE it gets submitted to the server.

In a javascript function check all of your inputs for a correct input, and allow the data to be sent to the server if it is all filled in correctly, or pop up an alert saying what else needs to be done before it can be submitted.

Doing this check strictly serverside will require a server request to check the input every time, as opposed to having the client check it, and submit it only if everything is correct.

Upvotes: 0

Richard Harrison
Richard Harrison

Reputation: 19403

The first if statement is in conflict with your requirements; you are requiring all fields to be filled in by using the AND operation - use OR and it will work with any single field value.

Validation should/could also be performed on the page itself by using javascript as Matt recommends.

To ensure that only one field is set do the following you could count the number of entries in _POST

if(count($_POST) == 1 AND 
   (isset($_POST['submit']) OR 
    isset($_POST['firstname']) OR 
    isset($_POST['lastname']) OR 
    isset($_POST['address']) OR 
    isset($_POST['ZIP']) OR 
    isset($_POST['phonenumber']) OR 
    isset($_POST['mail']) OR 
    isset($_POST['group'])
   ))

Either way it's not a very elegant way of doing this - but it will work.

Upvotes: 1

Tepken Vannkorn
Tepken Vannkorn

Reputation: 9723

If you want only one value from a field which is set to required (if possible, use javascript, or HTML5 has a required attribute for that), simply ignore other values from other fields:

<?php
     if ( isset( $_POST['submit'] ) ) {
         $wanted_value = addslashes( strip_tags( $_POST['input_name'] ) ); 
         // preventing from sql injection
         // ignore other values
         // and start manipulating it
     }
?>

Upvotes: 0

Related Questions