Jump to content

Ajax returns NULL


Recommended Posts

Hello!
Sorry for my English :)
I'm beginner and can't understand why ajax request returns NULL when I click on the button with a class "vote_button"
 

$(document).ready(function(){
    $(".vote_button").click(function(){
         var radio_answer = $("input:checked").val();
                        
         alert(radio_answer);
                        
         $.ajax({
             type: 'POST',
             url: baseDir + 'modules/quiz/ajax.php',
             data: radio_answer,
             dataType: 'json',
             success: function(data){
                 alert(data);
             }
         });
    });
});

ajax.php

<?php

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
require_once './quiz.php';

$value = $_POST['radio_answer'];
    
//$quiz = new quiz();
//$models = $quiz->displayAjax();

echo json_encode($value);

Could you help me?)

Link to comment
Share on other sites

if you will dump $_POST, do you see an array of posted values?

Solved :)

 

send data:

 

radio_answer = $("input:checked").val();

alert(radio_answer);

$.ajax({
    type: 'POST',
    url: baseDir + 'modules/quiz/ajax.php',
    dataType: 'json',
    data:  'radio_answer='+radio_answer,
    success: function(data){
        alert(data);
    }
});

Return data from ajax.php

<?php

require_once(dirname(__FILE__).'../../../config/config.inc.php');
require_once(dirname(__FILE__).'../../../init.php');
require_once './quiz.php';

$value = $_POST['radio_answer'];    
    
$quiz = new quiz();
$models = $quiz->displayAjax($value);

echo json_encode($models);

exit();
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...