简体   繁体   中英

After showing output using jQuery ajax the value doesn't pass using PHP post method

Hi I try to sum two data using AJAX and display it into hidden text input after that, I will pass the variable using PHP post method.

There are no problems in my jQuery AJAX code but the problem is after I submit the button, the value I retrieve from textbox total_rec[] is blank.

Here's my code below.

HTML:

<form method="post" action="<?php echo base_url() ?>user/recitem_insert"> 
<table>
<thead>
<tr><th>Test</th></tr></thead> 
<tbody> 
<tr><td>
<input type="hidden" name="last_item_rec[]" value="<?php echo $row->rec_qty; ?>">
<input type="text" name="item_rec[]" id="txt" disabled="">
<input type="hidden" name="total_rec[]" value="">
</td><tr>
</tbody>
</table>   
</form>

JQUERY AJAX:

<script>
$(document).ready(function(){
    $("input[name=item_rec\\[\\]]").on('keyup',function(){
        var one = $(this).parents('tr').find('input[name=last_item_rec\\[\\]]').val();
        var two = $(this).parents('tr').find('input[name=item_rec\\[\\]]').val();       

        sum =  parseInt(one) + parseInt(two);

        $(this).parents('tr').find('input[name=total_rec\\[\\]]').val(sum);        
    });
});
<script>

PHP CODE: ( recitem_insert.php )

$total_rec = $_POST['total_rec'];

for($i=0;$i<sizeof($check);$i++){   
    for($j=0;$j<sizeof($total_rec);$j++){
        $query=mysqli_query($con,"UPDATE tblstock 
                             SET             
                             rec_qty='$total_rec[$j]'
                             WHERE id = '$check[$i]'
                             ")or die(mysqli_error($con));  
    }               
}

As you told that the item contain 2 or more value. So you can use class instead of name.

HTML

<input type="hidden" class="last_item_rec" name="last_item_rec[]"  value="23" />
<input type="text" class="item_rec" name="item_rec[]"  id="txt" />
<input type="hidden" class="total_rec" name="total_rec[]" value="" />

jQuery

$(function(){
$(".item_rec").on('keyup',function(){
  var one = $(this).parents('tr').find('.last_item_rec').val();
  var two = $(this).parents('tr').find('.item_rec').val(); 
  sum =  parseInt(one) + parseInt(two);
  console.log(sum);

  $(this).parents('tr').find('.total_rec').val(sum);        
});
});

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