0

There is a way to send only non empty inputs (( those who value is not equal to "" )) when user do form submission?

i wanna to acheive this cuz my site offers forms to filter data, and use GET method to indicate query string in the url, but clearly something like this

site.mx/voucher/search/?id=1,2,3,4&date_from=&date_until&comment=&status=6

will be much more prettiest in this way

site.mx/voucher/search/?id=1,2,3,4&status=6

ty ;)

6
  • 1
    Does it matter what the URL looks like when you're submitting the form with jQuery (I assume you're talking about ajax here) ? Commented Mar 22, 2014 at 0:31
  • it matter for me, i like perfection* or at least aspire to her Commented Mar 22, 2014 at 0:33
  • Well, what does the ajax function look like now ? Commented Mar 22, 2014 at 0:34
  • is not an ajax function, is the classic process when user push submit button and then, the form is submitted ;) Commented Mar 22, 2014 at 0:35
  • Then you're not submitting anything with jQuery ? Commented Mar 22, 2014 at 0:40

3 Answers 3

2

Based on what you're saying, you could possibly do something like this with jQuery:

jQuery(function() {
    jQuery(form).on('submit',function(e) {
        e.preventDefault();
        var urlSuffix = '';
        jQuery(this).find(input).each(function() {
            if(jQuery(this).val() !== '' && jQuery(this).val() !== null) {
                if(urlSuffix == '') {
                    urlSuffix += '?';
                } else {
                    urlSuffix += '&';
                }
                urlSuffix += jQuery(this).attr('name') + '=' + jQuery(this).val();
            }
        });
        window.location = 'site.mx/voucher/search/' + urlSiffix;
    });
});

This function first disables your form's "default" submit action, then adds all of the form's fields to the URL and redirects.

However the simpliest method is to simply point the form's action to: site.mx/voucher/search/ and change the method to "GET"

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, or I guess you can do this! Intercept the browser behaviour and effectively create a poor-man's ajax. +1
yea, i like this approach, maybe it can be enhanced disabling empty fields before form submit is done, and enable again when form is already submitted. (( it will provoke send only non empty fields ))
There really isn't any need to since the page isn't being re-rendered. It's being re-loaded with different parameters in the $_GET array. Technically, if you want the query to be EXTREMELY clean, you could run the entire thing with $_POST or $_REQUEST instead of $_GET, and have the form simply submit to # or nothing at all (action="")
Yes, if you use this approach, you do not actually SUBMIT the form. You are redirecting the page with a 'GET' url you have composed of the form elements that have values. Thus, when the page returns, the form fields should be blank. However, if you have other code that reads any URL values and resets the form values (to show what was searched, for instance), then you need to handle that.
1

You can do this by selecting only non empty fields using a CSS selector:

$("form#search :input[value!='']").serialize()

I can't comment due my reputation is still under 50, but can you show us how you submit the form?

1 Comment

i want to use the native form submit event ;)
0

If your form action is a 'GET' and you are using the browser's native form post/get action to do that, you can't rewrite your browser behaviour (well, maybe you can, but you can't control every one else's browser).

If you DO want to use ajax and manually call the submission form via js/ajax, you can rewrite your URL query string and post that cleaner version. Otherwise.....

3 Comments

im thinking in catch* the submit event, disable empty fields, and when form is submitted, then enable empty fields again
If you 'catch' or preventDefault the submit, you won't actually be submitting the form. Bdb.jack's approach, which is not bad, is effectivly a redirect that loads a 'new' requested page with appended form values (which is what a 'GET' action looks like. Form actions are more often 'POST' actions). So you do not need to disable any form fields. You simply evaluate empty ones and the page is effectively 'reloaded' (via GET method).
If you do a true ajax, you also are not actually going to submit the page but make a separate request that takes action, returns something, and then you update/change your screen as desired via js depending on the results and logic you want to implement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.