Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.