Sharen Deleon Obida
Sharen Deleon Obida

Reputation: 1

Saving value from html input > date using ajax

I'm trying to save date using html input = date and ajax. but it didn't save.

html code:

<td class="dtreq"><input type="text" class="dtreq" name="dtreq" id="dtreq"></td>

Ajax:

$('#save').click(function(){
var dtreq = [];

$('.dtreq').each(function(){
    dtreq.push($(this).val());
    });

    $.ajax({
      url:"insert_punchlist_form.php",
      method:"POST",
      data:{dtreq:dtreq},
      success:function(data){
        alert(data);
        $("td[contentEditable='true']").text("");
        $('select').prop('selectedIndex',0);
        for(var i=2; i<= count; i++){
          $('tr#'+i+'').remove();
        }
      }
    });
});

Upvotes: 0

Views: 242

Answers (3)

Behrouz Goudarzi
Behrouz Goudarzi

Reputation: 71

$('#save').click(function(){
var dtreq = [];

$('input.dtreq').each(function(){

    dtreq.push($(this).val());
    });
    
    console.log(dtreq);
    $.ajax({
      url:"insert_punchlist_form.php",
      method:"POST",
      data:{dtreq:dtreq},
      success:function(data){
        alert(data);
        $("td[contentEditable='true']").text("");
        $('select').prop('selectedIndex',0);
        for(var i=2; i<= count; i++){
          $('tr#'+i+'').remove();
        }
      }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td class="dtreq"><input type="text" class="dtreq" name="dtreq" id="dtreq"></td>
<td class="dtreq"><input type="text" class="dtreq" name="test1" id="test1"></td>
<td class="dtreq"><input type="text" class="dtreq" name="test2" id="test2"></td>
<button id="save">Save</button>

Upvotes: 2

Ronak Chauhan
Ronak Chauhan

Reputation: 706

Hello I have modified your code please check.

$('#save').click(function(){
var dtreq = [];

$('.dtreq').each(function(){
    console.log($(this).val());
    dtreq.push($(this).val());
    });

    $.ajax({
      url:"insert_punchlist_form.php",
      method:"POST",
      data:{dtreq:dtreq},
      success:function(data){
        alert(data);
        $("td[contentEditable='true']").text("");
        $('select').prop('selectedIndex',0);
        for(var i=2; i<= count; i++){
          $('tr#'+i+'').remove();
        }
      }
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td class="dtreq"><input type="text" class="dtreq" name="dtreq" id="dtreq"></td>

<button id="save">Save</button>

Upvotes: 0

Konstantinos A
Konstantinos A

Reputation: 126

data : { "dtrec" : dtrec } the key in the data JSON object should be quoted or else it is parsed as a variable and of course the variable dtrec of the value part of data JSON object should be serialized as valid json

Upvotes: 0

Related Questions