Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting the result of a QUERY into a DIV

I am having an issue with a function that should display the result of a query in a DIV. The function is:

function(data){$("#question_label").html(data)},

Here is my JAVASCRIPT:

script type="text/javascript">
        var i=1;
        $(document).ready(function(){
            $("#input01, #input02, #input03, #input04, #input05").click(function(){
                 var value01 = $(this).attr('value');          
                 var value02 = i;
                 $.post('input_answers.php',{value:value01,id_question:value02}, 
function(data){$("#question_label").html(data)}, 
function (){i+=1} );
                     });    
            });          
        </script>

Here is the HTML:

<div id="question_label"></div>

Below is the (simplified) 'input_answers.php' file:

<?php
$query = "SELECT label_question FROM `questions` WHERE id_question='".$_POST['id_question']."' ";
$result = mysqli_query($connect,$query) or die('Could not execute the query ' . mysql_error()) ;    
echo '<table>';    
while ($data= mysqli_fetch_assoc($result)) {
  echo '
  <tr>  <td>'.$data["label_question"].'</td>
  </tr>';
 }    
echo '</table>'; 
?>

When I take the function(data) out of my code, it runs. But when I add it I get some error message in my console:

Uncaught TypeError: (h.dataType || "*").toLowerCase is not a function at Function.ajax 

Can anyone help me here?

Thank you!

like image 950
SamanthaAlexandria Avatar asked Nov 29 '25 16:11

SamanthaAlexandria


1 Answers

The 4th parameter of jQuery.post() is dataType, which need to be a string, not a function.

$.post('input_answers.php'
    {value: value01, id_question: value02}, 
    function(data){
        $("#question_label").html(data);
    }
);

If you want to increase i every request, increase it inside the "success" function :

$.post('input_answers.php'
    {value: value01, id_question: value02}, 
    function(data){
        $("#question_label").html(data); 
        i++;
    }
);
like image 109
Syscall Avatar answered Dec 02 '25 04:12

Syscall



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!