I know I'm making a stupid mistake... but I started using ajax just an hour ago. I have a problem with variables in JS.
When i write +'id_komputery'+ then js return error : "Uncaught SyntaxError: missing ) after argument list". The same with id='id_komputery'
$.ajax({
url:'add.php',
method:'POST',
data:{
producent_new:'producent_new',
nazwa_new:'nazwa_new'
},
success:function()
{
var id_komputery = '<?= $id_komputery ?>';
console.log(id_komputery);
$("#main_table > tbody").append('<tr id='id_komputery' ><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value='id_komputery' /></td><td width="50" align="center">+'id_komputery'+</td></tr>');
}
});
Replace this line:
$("#main_table > tbody").append('<tr id='id_komputery' ><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value='id_komputery' /></td><td width="50" align="center">+'id_komputery'+</td></tr>');
with this one:
$("#main_table > tbody").append('<tr id="' + id_komputery + '"><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value='id_komputery' /></td><td width="50" align="center">'+id_komputery+'</td></tr>');
You are wrong in the column 'id_komputery'
. It misses the concat operator You have to change this with:
'<tr id="' + id_komputery + '" >...'
'...<input type="checkbox" name="komp_id[]" class="delete_komputery" value="' + id_komputery + '" />'
Or with es2015+, you can use interpoller expression:
$("#main_table > tbody").append(`<tr id="${id_komputery}"><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value="${id_komputery}" /></td><td width="50" align="center">${id_komputery}</td></tr>`);
You forgot to put "'" near +'id_komputery'+ '</td></tr>')
(you have +'id_komputery'+ </td></tr>')
) and javascript consider it as varaible and is invalid variable and you should not quote the vairable here +'id_komputery'+
this becomes simple string literal, instead you just write - id_komputery
$("#main_table > tbody").append('<tr id='id_komputery' ><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value='id_komputery' /></td><td width="50" align="center">+'id_komputery'+'</td></tr>')
You are forgetting several quotes around the append line plus a minor change.
$("#main_table > tbody")
.append('<tr id='id_komputery' >
<td width="50" align="center">
<input type="checkbox"
name="komp_id[]"
class="delete_komputery"
value='id_komputery' />
</td>
<td width="50" align="center">+'id_komputery'+
</td></tr>'
);
Above is your syntax formatted.
Under is the syntax with the quotes fixed.
$("#main_table > tbody")
.append('<tr id="id_komputery">
<td width="50" align="center">
<input type="checkbox"
name="komp_id[]"
class="delete_komputery"
value="id_komputery"/>
</td>
<td width="50" align="center">'
+id_komputery+
'</td></tr>'
);
I have corrected the your code. Replace the your code with below code:
$.ajax({
url:'add.php',
method:'POST',
data:{
producent_new:'producent_new',
nazwa_new:'nazwa_new'
},
success:function()
{
var id_komputery = '<?= $id_komputery ?>';
console.log(id_komputery);
$("#main_table > tbody").append('<tr id="'+ id_komputery +'"><td width="50" align="center"><input type="checkbox" name="komp_id[]" class="delete_komputery" value="'+ id_komputery +'" /></td><td width="50" align="center">'+ id_komputery +'</td></tr>');
}
});