php - How not to send empty input value in GET-query

575

I have a form like this

<form action="index.php">
  <input type="hidden" name="id" value="1">
  <input type="hidden" name="order_by" value="">
  <button type="submit">Submit</button>
</form>

It has some jQuery handlers to fill in "order_by" field. If I submit this form with empty "order_by" filed, I get an address like this:index.php?id=1&order_by=

What is the best way I can do to get following address after form submission if the "order_by" is empty:index.php?id=1 ?

872

Answer

Solution:

you can disable empty fields so they don't get added as data

$('form').submit(function(e){
    var emptyinputs = $(this).find('input').filter(function(){
        return !$.trim(this.value).length;  // get all empty fields
    }).prop('disabled',true);    
});
965

Answer

Solution:

in php you can do like this

if(!isset($_GET['order_by']) || !$_GET['order_by'])
    return 'error msg';

or

if(empty($_GET['order_by']))
    return 'error msg';

change html like this

   <form action="index.php" onsubmit="return valid()">
  <input type="hidden" name="id" value="1">
  <input type="hidden" name="order_by" id="order_by" value="">
  <button type="submit">Submit</button>
  </form>

javascript:

  function valid(){
   if(!$('#order_by').val()){
       alert("Order by not found");
       return false;
    }
   return true;
 }
65

Answer

Solution:

$('form#formID').submit(function(e){
     var emptyinputs = $(this).find('select').filter(function(){
     return !$.trim(this.value).length;  
    }).prop('disabled',true);    

     var emptyinputs = $(this).find('input').filter(function(){
     return !$.trim(this.value).length;  
    }).prop('disabled',true);    
});

You can disable empty fields (both select and input) so they don't get added as data, and for a custom form id.

People are also looking for solutions to the problem: php - error on mysqli multiple connection via class

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.