简体   繁体   中英

Submit whole form when select option is changed without reload page

I need to submit the whole form when I change a select option and save it to mysql without reload page.

I have this code, but only POST the select option value and I need to POST the hidden values in the form too.

<form class="form-horizontal" method="POST" action='#' name="dateForm">
  <input type='hidden' name='cond_acao' class='form-control' value="edit_seccao_formando">
  <input type="hidden" name="id_formando" value="<?=$linha_formandos[id_formando];?>">
  <select name="id_seccoes" class="form-control" onchange="return postSelection">
       <option selected="selected" value="1">car</option>
       <option value="2">boat</option>
       <option value="3">plane</option>     
    </select>
  <div id='response_seccoes'></div>
  <script>
    function postSelection(selectObject) {
      var id_seccoes = window.dateForm.id_seccoes.value = selectObject.options[selectObject.selectedIndex].value;
      var dataString = "id_seccoes=" + id_seccoes;
      $.ajax({
        type: "post",
        url: "url.php",
        data: dataString,
        success: function(response) {
$('#response_seccoes').html("ok").fadeIn(100).delay(2000).fadeOut("slow");
          //$("#list").html(response);
        }
      });
      return false;
    };
  </script>
</form>

It's possible to serialize instead of especified the strings?

Can anyone help me please? Thank you

you are using Jquery, I suggest you monitor the select button for change using a jquery function like

$("id_seccoes").change(function(){
    //call your post method here.
});

Replace your code with this.

<form class="form-horizontal" method="POST" action='#' name="dateForm">
<input type='hidden' name='cond_acao' class='form-control' value="edit_seccao_formando">
<input type="hidden" name="id_formando" value="<?=$linha_formandos[id_formando];?>">                           
<select name="id_seccoes" class="form-control">
<option value="1">car</option>
<option value="2">boat</option>
 <option value="3">plane</option>      
</select>
</form> 
<div id='response_seccoes'></div>

<script>
$(document).ready(function(){
    $('[name="id_seccoes"]').change(function(){
        $.post('url.php',$('[name="dateForm"]').serialize(),function(response){
            $('#response_seccoes').html("ok").fadeIn(100).delay(2000).fadeOut("slow");
        });
    });
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM