Server side validation - conditional required fields - logic help

Hello all,

I have a form that work like this on the client side:

If the user clicks some link, extra fields are available to fill in, and those extra fields are mandatory.

If we do not click that link and the extra fields don’t show up to the user, those fields will not count to the validation script.

This works well on client side.

Issue:
On the server side, I can’t find a way to tell: “If the field as NOT been submitted, then it’s not required”, else, it’s required.".

Question:
How can we check if a given input field as NOT been submitted?

Is there a better way for dealing with it?

Thanks in advance,
Márcio

Spot on :tup:

Yes, that’s what I’m telling you

b) advantage: the non-js users are able to fill in the form, even though javascript is disabled!

Yes :weee:

Hello ScallioXTX,

:slight_smile:

I believe I’ve understood, after a good night sleeping, what you really mean.
I was really upset by the fact that all users should see extra fields at the beginning, if they want them to be available on no-javascript user agents.

But this was a wrong assumption. It was not all users we where talking here, correct ? Only those that don’t have js enabled.

So you are telling me that:
hide your elements using JS (onload for example) and NOT using css property for doing so, directly.

This way:
a) if the user has JS script enabled: good for it, it will only see the fields he/she should see.
b) if the user don’t have js enabled, the hide elements function will not run, hence, they will all be available to the user. Disadvantage, he/she will see extra fields on the screen. - but it’s a minor one.

Yes?
:D:D:D:D:D:eye:

Wow YEAH!!! No CERN or NASA questioning any more.
Happy with the world again! :slight_smile:

Thanks for your patience. :slight_smile:
Márcio

Hope that you have got the answer already. If you are not using jquery library ,you may use them this makes javascripts works easier than when you write your own javascripts.

lol, you sure right.

Well, so I believe I really need to understand a little bit more about the information workflow between client and server.

ScallioXTX and hash, thanks for your time on this.

Regards,
Márcio

I see…

Of course the use of ajax will rely on js as well. dummy one.

You can only show the optional fields when a checkbox is ticked iff javascript is enabled.

NOOOOOOOooooooooooo!!! (falling)
:sick:

Ok. :slight_smile:

So:
Having a form that hides some information and removes unnecessary noise, and that is unobtrusive, can’t be done at this time.

Period ?
:shifty:

We have CERN, NASA… but we don’t have a form that hides some information from the user, relying only on server side languages?

And the reason I cannot accept it’s because I’m missing something really basilar about internet protocols right?

Yes. I knew.

Before your first post yes. It was only to justify myself why I was disabling the fields, I was not pointing it as a method to not rely on javascript. :slight_smile:

Or, if the checkbox is not ticked, do not check all optional fields.

Ok, now what still needs clarification:

The same way we do when we select a country from a drop down and then we see the available cities? This could be done using server side only no?
On some old pages, I seem to recall this, a refresh was made to the page, and then we were able to see the cities of that chosen country.
So, yes, an extra request is made to the server, a refresh is made, but maybe http request can ease things up on this side?

Just thinking… what do you think?

K. Regards,
Márcio

Correct

That refresh also relies on javascript. Without javascript it is not possible to refresh a page without the user initializing this (for example by pushing a button).

AJAX can ease it up for you :slight_smile:

You can only show the optional fields when a checkbox is ticked iff javascript is enabled. So what you’re proposing is not in line with my “everything should work when javascript is not availble” approach.

I like your approach ScallioXTX,

And you right, even if, not having js enabled is like a chimera those days, and the propose for disable seems to be only that of doing evil things, I still believe it’s a nice principle that could be applied (and as a principle, it should be applied on other technologies as well).

I’m still not quite getting why do you display the option fields to the end user ?

Instead of hiding the optional fields initially I show them initially and then hide them using javascript.

We should hide extra noise at the beginning and only if show them, only when necessary right? Hence, they should be hidden on the beginning and only when they check the box, they will have the fields displayed. Don’t you agree?

his also means you shouldn’t make the optional fields disabled by default, as that would defeat the whole purpose

Of course the only reason I did it, was because I was hopping that THAT would allow or form field to NOT be submitted, HENCE, allowing the server side verification of isset to work. I was hopping that having those fields disabled, they will not be submitted and then, on the server side, I could have something like:
if (isset($mydisabledfield))
{
echo “this field is required”;
}

supposing that, if the field was disabled on form submission, the isset will return false, and so, this message will only appear IF the disablefield was enabled.

:goof:

Anyway, I will change to your approach on future similar scenarios, since, I believe due to time constrains, and my slowliness coding, I will be unable to change to a checkbox based system. (this sounds fancy - checkbox based system) how well… :s

Regards,
Márcio

I’ve read that when we submit a form field, even if there’s nothing on it,
it will not send NULL to the server, hence, it will be set for sure.

It will always send a string, just an empty one if the field is empty.

Agree with what Scallio said, you should build for no js and enhance. So that would mean your extra fields would be empty and optional by default. E.g. a checkbox for business or personal, if set you might require a tax number, if not ignore it.

I always do this slightly differently:

Instead of hiding the optional fields initially I show them initially and then hide them using javascript. Instead of a link that shows/hides these additional fields I use a checkbox, which you can easily check on the server if it’s checked (if ($_POST[‘checkboxfield’])) and make all validation/processing dependent on that.
The reason I do it this way is that users that have javascript disabled/a browser that is not capable of javascript can still use the form. Whereas with your approach they cannot.
This also means you shouldn’t make the optional fields disabled by default, as that would defeat the whole purpose :slight_smile:

At the beginning of my php form submission file, I’m having something like this:


$field = $_POST['field'];

for all fields.

Then, I was checking if $field is set.

Probably we should evaluate $_POST[‘field’] directly ?

I’ve read that when we submit a form field, even if there’s nothing on it,
it will not send NULL to the server, hence, it will be set for sure.

That’s why you were telling, “if the field don’t exist on the form” ?
Because if they don’t exist on the form, a NULL will happen, and the isset will return false.

The fields are hidden from the user, but, hopping this will work better then hidding, I’ve also disable them by adding the disable=true attribute. Apparently however, for the form submission concerns, this seems to not bring nothing new to the play?

Yes, I’m also hiding the fields:

So simple once someone else tells you! :slight_smile:
I knew this was a logical issue.
This sounds like a trick… nothing wrong for following this path yes? It’s a common use for those situations?

Thanks a lot for your help, again :slight_smile:
Márcio

Correct

Yes. Period. :slight_smile:

Nope, we don’t

I don’t know why you can’t accept it, that’s for you to decide :smiley:

if(!isset($_POST[‘field’]))

This will work fine if the fields don’t exist in the form. If you’re just hiding the fields with css/js, then fill them in with something like XXXNOSUBMITXXX and remove it with js for the user.

if(!isset($_POST[‘field’]) || $_POST[‘field’] === ‘XXXNOSUBMITXXX’)

If you know a way to show/hide fields when a user ticks a checkbox without using Javascript I’d be glad to hear it :slight_smile:

That still relies on javascript to enable/disable fields.

What I’m proposing is the other way around
What you’re suggesting is: if the field is not set then I don’t need to check it (because javascript didn’t enable the field).
What I’m suggesting is: if the checkbox is ticked then I need to check all optional fields (no javascript involved).