0

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();
        }
      }
    });
});
5
  • dtreq is a same class for td and input ?? .. Also id should be unique don't use the same id for more than one element Commented May 18, 2019 at 2:16
  • i removed the class at input. still same. Commented May 18, 2019 at 3:59
  • if you removed the class from input you need to use $(this).find('input').val() because td doesn't have value Commented May 18, 2019 at 4:04
  • Possible duplicate of jQuery - Getting form values for ajax POST Commented May 18, 2019 at 4:16
  • I have updated your code check my answer. Commented May 18, 2019 at 4:59

3 Answers 3

2

$('#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>

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

1 Comment

Good point, the selector 'input.dtreq'! The importance of this could not be demonstrated in @RonakChauhan's answer, because the html is faulty: the <td>s are floating around without being in a proper table. Maybe you can fix this in your answer?
0

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

Comments

0

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>

Comments

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.