1

I have a form with 6 <select> options. All <select> filed will have same values but with different names. Like this:

<form action="" method="post">
    <select name="var1" class="form-control">
     <option value="0">0</option>
     <option value="10">10</option>
     <option value="20">20</option>
     <option value="50">50</option>
     <option value="100">100</option>
     </select>

    <select name="var2" class="form-control">
         <option value="0">0</option>
         <option value="10">10</option>
         <option value="20">20</option>
         <option value="50">50</option>
         <option value="100">100</option>
    </select>
    <input type="submit" name="submit" value="Submit"/>
</form>

I am submitting these information to the same page, But before submit is it possible to filter those variables which are greater than 0? I want to POST only those variables which has value greater than 0. How can i do it?

NikuNj Rathod
  • 1,663
  • 1
  • 17
  • 26
meenal
  • 79
  • 1
  • 11

3 Answers3

2

You can follow the steps as an idea.

  1. Prevent the default event on submit of form or on submit button click with preventDefault(); function.
  2. Check the the value of all select box for greater than 0. If YES then create the query string for the values that are greater than 0.
  3. Query string will look like "var1=3&var3=4".
  4. Use the $.post(); / $.ajax(); functions to submit the data. Reference https://api.jquery.com/jquery.post/

Also you can find the online tutorials for jQuery and ajax. Like w3schools or on https://api.jquery.com

Parag Chaure
  • 2,883
  • 2
  • 19
  • 27
  • Yes this is what i was looking for. Thank you very much. I will try with this now. Hope i will do it properly. – meenal Jul 07 '16 at 05:37
1
$('#removeZerosForm').submit(function() {

    var values = {};
    $.each($('#removeZerosForm').serializeArray(), function(i, field) {
        if (field.value > 0) {
          values[field.name] = field.value;
        }
    });

    $.post('url', values);
});
foreach ($_POST as $key => $val) {
    if ($key !== 'submit') {
        if ($val == 0) {
            unset($_POST[$key]);
        }
    }
}
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • here, `values[field.name] = field.value;`, should i use each filed name? I mean like this. `values[field.var1] = field.value; values[field.var2] = field.value;` – meenal Jul 07 '16 at 05:45
1

An approach using submit event, event.preventDefault(),FormData(),XMLHttpRequest()`

<form action="" method="post">
  <select name="var1" class="form-control">
    <option value="0">0</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
    <option value="100">100</option>
  </select>

  <select name="var2" class="form-control">
    <option value="0">0</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="50">50</option>
    <option value="100">100</option>
  </select>
  <input type="submit" name="submit" value="Submit" />
</form>
<script>
  var form = document.querySelector("form");
    form.addEventListener("submit", function(e) {
      e.preventDefault();
      var data = new FormData();
      var selects = e.target.querySelectorAll(".form-control");
      var values = [];
      for (var i = 0; i < selects.length; i++) {
        // if `select` `.value` is not `0`,
        // add to `values` array`
        if (selects[i].value != 0) {
          values.push([selects[i].name, selects[i].value])
        };       
      };
      // if `values` array contains items
      // append items to `FormData` object
      if (values.length) {
      values.forEach(function(val) {
        data.append(val[0], val[1])
      })
      var request = new XMLHttpRequest();
      request.open("POST", "/echo/js/")
      request.onload = function() {
        values = [];
        console.log(data);
        // log values posted
        for (prop of [...data]) {
          console.log(prop)
        }
      }
      request.send(data)
      }
    }); 
</script>

jsfiddle https://jsfiddle.net/7a67uqar/

guest271314
  • 1
  • 15
  • 104
  • 177