I have a table created using a while loop thats displays a list of items so that the user can select an item to delete which is done via ajax.
I would like to use JQuery's
closest attribute but I can't work out the correct syntax. The output I get from this is Object Object
The table
<form id="del_opinion">
<table id="asked_list">
<?php
while($row = mysqli_fetch_assoc($result)){
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<input type="button" name="del_question" value="" class="trash_btn">
<input type="hidden" class="id_question" name="question_id" value="<? echo $row['questionID']; ?>">
<input type="hidden" class="id_user" name="user" value="<?php echo $id; ?>" >
</td>
</tr>
<? } ?>
</table>
</form>
The ajax
<script>
$(document).ready(function() {
$('.trash_btn').click(function(){
var question = $(this).closest('.id_question');
var user = $(this).closest('id_user');
$.ajax({
url: 'opinion_del.php',
type:'POST',
data: 'question='+ question +'&user='+user,
dataType: 'json',
success: function(response){
$('#responses_table').html(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
} // End of success function of ajax form
}); // End of ajax call
});
});
</script>
The php have done for testing
$question_id = $_POST['question'];
$id = $_POST['user'];
$sql = "SELECT * FROM adviceQuestion WHERE questionID = '$question_id'";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
if($result){
$output = $question_id;
}
echo json_encode($output);
If I type a literal value to $output
it works fine and if I type a literal value in either of the jquery vars
they work correctly too so I'm convinced it is something to do with the closest syntax
I have also tried adding a class to the table row called details
and using
var question = $(event.target).closest('.details').find('.id_question');
If I alert question it says [object Object]
var user = $(this).closest('id_user');
Change to this
var user = $(this).closest('.id_user');
You didnt specified a c lass "." dot ))) Maybe will help
OK, sussed it...
Changed this
var question = $(this).closest('.id_question');
var user = $(this).closest('id_user');
to
var question = $(this).next('.id_question').val();
var user = $(this).next('.id_user').val();