Jump to content

AJAX Array


BettDow

Recommended Posts

Hello!

I'm doing a simple test with questions from a DB. I want to use some AJAX the problem I have is that when I use only one "select" works ok... but if I try to print more than one at the same time then the script doesn't work at all. I guess it's got something to do with an array, but I don't know where to start... any help will be appreciated!

Thanks! Here I leave the code I'm using.

 

 

---------------------------------------------------------

AJAX PART

--------

--------

 

 

var miPeticion = getXMLHTTPRequest();

function validarPregunta(){

var respuesta = document.Test.resp.value;

var url = "action.php?resp="+respuesta;

miPeticion.open("GET", url, true);

miPeticion.onreadystatechange=respuestaAjax;

miPeticion.send(null);

}

function respuestaAjax(){

if(miPeticion.readyState == 4){

if (miPeticion.status == 200){

//alert (miPeticion.responseText);

document.getElementById('i').innerHTML= miPeticion.responseText;

} else {alert("error with server" + miPeticion.statusText);}

}

}

 

 

PHP PART: action.php

--------

--------

 

<?php

$respuesta = $_GET["resp"];

 

$respCorrecta = "525";

 

 

if ($respCorrecta == $respuesta){

$respuestaok = "Has acertado";

echo $respuestaok;

}

else {

$respuestaf = "Has fallado";

echo $respuestaf;

}

?>

 

 

HTML/PHP/DB PART

----------------

----------------

 

<?php

 

$con=mysql_connect('$HOST','$USER','$PASS') or die ('no serv');

mysql_select_db('sate') or die ('no db');

//if (!isset($_POST['submit'])) {

 

$sql = "SELECT * FROM cia ORDER BY RAND() LIMIT 4";

$display = mysql_query ($sql) or die ('query prob');

if (0== mysql_num_rows($display)){

print "no quest in db";

} else {

while ($row = mysql_fetch_array($display)) {

$id = $row["id"];

$cia = $row["CIA"];

echo "<form name='Test' method='GET' action=''>

<select name='resp' onchange='validarPregunta();'>

  <option>Choose your answer</option>

<option value=".$id.">".$cia."</option>

<option value=".$id.">".$id."</option>

</select>

</form>";

}

}

?>

<div id="i">Content for id "i" Goes Here</div>

 

Link to comment
https://forums.phpfreaks.com/topic/223187-ajax-array/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.