I have an administration form and when you click on a button it adds more answer fields that you can fill out. When the user clicks the add question it updates two tables the 'kumiquestions' table and the 'kumianswers' table.
It does this through ajax in jquery. It works to insert the question but as soon as I added the code to insert the answers I get these errors:
Notice: Undefined index:
kid in Z:\xampp\htdocs\Kumihangul\admin\addquestion.php on line 4
Notice: Undefined index:
kans in Z:\xampp\htdocs\Kumihangul\admin\addquestion.php on line 5
This is my Jquery:
$('#add-question-form').click(function() {
//Varables to store form data
var q_cat = $('#qtype').val();
var q_lvl = $('#qlevel').val();
var q_txt = $('#qtext').val();
var q_quest = $('#qquest').val();
var q_audio = $('#qaudio').val().replace("C:\\fakepath\\", "");
var q_info = $('#qinfo').val();
$.ajax({
type: 'POST',
url: 'addquestion.php',
data: {qcat: q_cat, qlevel: q_lvl, qtext: q_txt, qquestion: q_quest, qaudio: q_audio, qinfo: q_info },
success: function(data) {
$('#temp').html(data);
}
});
//insert answers and questions
var k_id = $(document).find('#lastautoinc').val();
//loop through answer fields
for (var i = 1; i <= answercounter; i++) {
var k_ans = $(document).find('#qanswer'+i).val();
$.ajax({
type: 'POST',
url: 'addanswer.php',
data: {kid: k_id, kans: k_ans },
success: function(data) {
}
});
}
});
This is my addquestions.php
<?php
include '../conn/connect.php';
//Get Variables from AJAX POST
$k_id = $_POST['kid'];
$k_ans = $_POST['kans'];
//Make sure proper variables are INTs
(int) $k_id;
//SQL INSERT Statement
$sql = "INSERT INTO kumianswers (kumiquestionsid, answer) VALUES ('$k_id', '$k_ans');";
//Run QUERY
$lquery = $conn->prepare($sql);
$lquery->execute();
?>
var_dump($_POST)to see what you're really getting on the server? Plus, you're vulnerable to SQL injection attacks.