0

I'm French so sorry for my bad English, I'm trying to make a quiz for phoegap

I've a ajax_actions.php who works

<?php
require('./PDOsql.class.php');
require('./Country.class.php');
require('./Question.class.php');
require('./Answer.class.php');


header('Access-Control-Allow-Origin: *');

if(isset($_POST["actionname"])&&!empty($_POST['actionname'])){
$actionname = $_POST['actionname'];

if($actionname=='getQuestion'){
$name = $_POST['name'];
$c = new Country();
$c->getIdByName($name);
$CountryId = $c->getId();

$q = new Question();
$a = new Answer();

$allQuestion = array();
$allQuestion = $q->getQuestionByCountryId($CountryId);

$allAnswer = array();
foreach ($allQuestion as $question) {
  $allAnswer[] = array(
    $a->getAnswerByQuestionId($question['id'])
  );
}

die(
  json_encode(
    array(
      'state'=>'success','allQuestion'=>$allQuestion,'allAnswer'=>$allAnswer
    )
  )
);
  }
}

and I've 2 classes : Question :

<?php


class Question {

private $id="";
private $text="";
private $connect;

public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
    return $this;
}

public function getText()
{
    return $this->text;
}

public function setText($text)
{
    $this->text = $text;
    return $this;
}

public function getQuestionByCountryId($CountryId){
    $this->connect = new PDOsql();
    $sql='SELECT * FROM Question WHERE countryId = ? ORDER BY RAND() LIMIT     5';
    $opt = $CountryId;

    $value =  $this->connect->query($sql,$opt);
    $A_Question = array();
    while($question = $value->fetch()){
        $A_Question[] = array(
            'id'=>$question['id'],
            'text'=>$question['text']
        );
    }
    $this->connect = null;
    return $A_Question;
}
}

And I've a Answer class :

<?php


class Answer {

private $id="";
private $text="";
private $isGood="";
private $connect;

public function getId()
{
    return $this->id;
}

public function setId($id)
{
    $this->id = $id;
    return $this;
}

public function getText()
{
    return $this->text;
}

public function setText($text)
{
    $this->text = $text;
    return $this;
}

public function getIsGood()
{
    return $this->isGood;
}

public function setIsGood($isGood)
{
    $this->isGood = $isGood;
    return $this;
}


public function getAnswerByQuestionId($QuestionId){
    $this->connect = new PDOsql();
    $sql='SELECT * FROM Answer WHERE QuestionId = ?';
    $opt = $QuestionId;

    $value =  $this->connect->query($sql,$opt);
    $A_Answer = array();
    while($answer = $value->fetch()){
        $A_Answer[] = array(
            'id'=>$answer['id'],
            'text'=>$answer['text'],
            'isGood'=>$answer['isGood']
        );
    }
    $this->connect = null;
    return $A_Answer;
}
}

And a js function call when I click :

    function getQuestion(name){
    $.ajax({
        type: "POST",
        url: url,     
        dataType: "json",
        data : {
            actionname : 'getQuestion',
            name:name
        },
        success: function(data) {
            allQuestion = data.allQuestion;
            allAnswer = data.allAnswer;
            for (var i = allQuestion.length - 1; i >= 0; i--) {
                alert(allQuestion[i]["text"]);
                for (var j = 0; j < 4 ; j++) {
                    alert("answer "+j+" "+allAnswer[i][j]["text"]); //HERE THAT DOESNT WORKS !
                };
            };
        },
        error: function(data) {
            alert("error !");
        }
    });
}

And the problem is, I can't have my answer list :/ Thank you for your help

Raph
  • 123
  • 2
  • 11

2 Answers2

1

The A in ajax(Asynchronous JavaScript + XML) is for asynchronous. This means, when an ajax request is fired off, it is unknown the amount of time it will take to complete. Thus, you cannot ensure the order in this case.

What is sequential is the loop will iterate and fire off multiple ajax requests. However, between each iteration, it is highly unlikely(though possible) that a request would be complete and fire off the success handler. Again, asynchronous programming is not sequential programming.

See Asynchronous vs synchronous execution, what does it really mean?

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53
-1

Ok, so I don't have a choice I need to put my ajax 2 in my ajax 1 :/

Raph
  • 123
  • 2
  • 11
  • @Ralph, I 'guess'. If you want to ensure that ajax1 ajax2 ajax1 ajax2, you will have to alert them before $.ajax...perhaps if you let me know of your exact goals I can be of better help – AmmarCSE May 24 '15 at 13:35
  • I change this post for explain my other problem – Raph May 24 '15 at 13:49
  • @Ralph, when you do console.log(data) in the success callback, what happens? – AmmarCSE May 24 '15 at 14:27