Alright, so I am trying to get the value of a textarea dynamically added from jquery. But it always seems to get the default value of it.
Heres the code:
jQuery:
$(document).on('click', '.savechanges',function(e){
e.preventDefault()
var rowid = $(this).attr('data-id');
var text = $(".textbox-" + rowid).val();
var datum = $("#date").val();
$.ajax({
type : 'post',
url : 'php/savechanges.php', //Here you will fetch records
data : 'rowid='+ rowid + "&date=" + datum + "&text=" + text, //Pass $id
success : function(data){
alert('Dina ändringar har blivit sparade!');
}
});
});
HTML:
<div class="form-group">
<label for="text">Ändra din text för denna dag</label>
<textarea class="form-control textbox-<?php echo $singleRow['id'];?>" name="textruta" id="text">.
<?php echo $singleRow['textruta'];?>
</textarea>
</div>
<button type="button" class="btn btn-block btn-success savechanges" data-dismiss="modal" data-id="<?php echo $singleRow['id'];?>">
Spara ändringar
</button>
So the issue here is that the $(".textbox-" + rowid).val()
always gets the value added by jQuery and not the updated version.
Any help would be appreciated.
Im create file index.php on my server and create this code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<?php
$singleRow['id'] = 1;
$singleRow['textruta'] = "test";
?>
<div class="form-group">
<label for="text">Andra din text for denna dag</label>
<textarea class="form-control textbox-<?php echo $singleRow['id'];?>" name="textruta" id="text"><?php echo $singleRow['textruta'];?></textarea>
</div>
<input type="text" id="date" value="05/04/2017"/>
<button type="button" class="btn btn-block btn-success savechanges" data-dismiss="modal" data-id="<?php echo $singleRow['id'];?>">
Spara andringar
</button>
<script>
$(".savechanges").on('click',function(e){
e.preventDefault()
var rowid = $(this).data('id');
var text = $(".textbox-" + rowid).val();
var datum = $("#date").val();
console.log("start");
$.ajax({
type : 'post',
url : '/seo-service/ajax/savechanges.php', //Here you will fetch records
data : 'rowid='+ rowid + "&date=" + datum + "&text=" + text, //Pass $id
success : function(data){
console.log(data);
alert('Dina andringar har blivit sparade!');
}
});
});
</script>
after this im create new file "savechanges.php"
<?php
var_dump($_POST);
?>
everything okey. I hope this example help you