0

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();
?>
3
  • 1
    Have you watched the request / response in your browser's console while the AJAX request was occurring? It will likely reveal the problem to you. Commented Apr 16, 2014 at 18:28
  • 2
    did you try doing var_dump($_POST) to see what you're really getting on the server? Plus, you're vulnerable to SQL injection attacks. Commented Apr 16, 2014 at 18:31
  • I believe the error is coming from its running the code faster than the server can keep up... When I did var_dump it still had the old post data from the previous ajax request. Commented Apr 16, 2014 at 19:09

2 Answers 2

1

In your url: 'addquestion.php' ajax section, you have not passed the kid, kans parameters, rather you have passed in addanswer.php ajax section call. Thats is the reason you got the undefined index error.

data: {qcat: q_cat, qlevel: q_lvl ...
Sign up to request clarification or add additional context in comments.

6 Comments

Well that section 'addquestion.php' is working perfectly fine... no errors only the 'addanswer.php' isn't working
answercounter is undefined !
And, running ajax inside the loop is not a good practice. You can pass the data in the form array
sorry answer counter is defined as a global in another part of the jquery, I have no idea how to pass the data like that, it took me forever to think of this solution.
Thanks for the help, the ajax in the for loop is the only thing not working. I am looking up the .serializearray() function as it seems to be the route to go just need to get it worked in for each answer field the admin adds.
|
0

You have to check if the $_POST variables are filled. So, to correct your code:

In the addquestion.php:

$k_id = (isset($_POST['kid']) && $_POST['kid'] != null) ? $_POST['kid'] : '';
$k_ans = (isset($_POST['kans']) && $_POST['kans'] != null) ? $_POST['kans'] : '';

5 Comments

This isn't the whole solution to his problem though, this is just a little validation. He probably does not want to insert blanks into the database.
The errors he gets shows that the $_POST values are empty. He has to check them(probably they are not sent). -Edit- Damn, I missed your point. You are right.
Thyey're probably not sent - and that is what has to be solved before server-side validation can occur.
I need to go through and add validation checks anyway but I usually do it last for some reason horrible practice I need to break myself of, but the error is somewhere in sending them just can't figure out what
The error says literally: "Your $_POST variables are not sent". Check out the form where you post it(and open your developer tools and take a look at the network tab).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.