Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 6th, 2006, 10:02 AM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default form php mysql

hi! I'm making this tests in php with questions I've got in a MySql db and trying to get the form to retrieve the answers from users. The problem I have is that the 'submit' and 'reset' buttons are placed for each question, or they appear one time but It doesn't allow you to choose the answers for all questions, as it understands it all multiple choice question belong to the same form since they have the same 'name'.

I appreciate any suggestions, here is the code:

<html><head><title>TEST</title></head>
<body><form action='<?php print $PHP_SELF;?>' method='post'>
<?php
// Conectamos con el servidor
$con = mysql_connect('localhost', '', '') OR die('No he podido conectar con el servidor');
// Selección de la base de datos donde está nuestras tablas
mysql_select_db('examenes') OR die('No pude seleccionar la base de datos');
// Definimos la consulta SQL dentro de una variable
$sql = "SELECT * FROM preguntas WHERE Num_Examen = 0";
// Ejecutamos la consulta y el resultado lo guardamos en $result
$result = mysql_query($sql) OR die('La consulta SQL no se ha podido realizar');
// Si la consulta se ha realizado, pero no ha devuelto ninguna tupla
if (0 == mysql_num_rows($result)){
    // Decimos que no hay preguntas
    print "No hay preguntas definidas para el examen 0";
// Si ha devuelto alguna tupla
} else {
    // Las vamos leyendo una a una, asignandolas a la variable $tupla
    While ($Pregunta = mysql_fetch_assoc($result)){
       // Imprime numero de Pregunta
       print "Pregunta Nº ";
       print $Pregunta[Num_Pregunta];
       print "<br/>\n";
       // Imprime la pregunta en si
       print $Pregunta[Pregunta]."<br/>\n";
       // Si hay definida una imágen la muestra
       if (0 < strlen($Pregunta[Imagen])){
        print "<img src='$Pregunta[Imagen]'><br/>";
       }
       // Imprime las respuestas posibles
       //print "<pre>\n";
       //print_r($Pregunta);
      // print"</pre>\n";
       print "Respuestas : <br/>\n";
       print "A - $Pregunta[Resp_1] <input type='radio' name='r'><br/>\n";
       print "B - $Pregunta[Resp_2] <input type='radio' name='r'><br/>\n";
       print "C - $Pregunta[Resp_3] <input type='radio' name='r'><br/>\n";
       print "D - $Pregunta[Resp_4] <input type='radio' name='r'><br/>\n";
       print "\n";
    }
}

?><input type='submit' value='Enviar'></form></body>
</html>

  #2 (permalink)  
Old January 8th, 2006, 12:43 AM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

print "A - $Pregunta[Resp_1] <input type='radio' name='r'><br/>\n";
print "B - $Pregunta[Resp_2] <input type='radio' name='r'><br/>\n";
print "C - $Pregunta[Resp_3] <input type='radio' name='r'><br/>\n";
print "D - $Pregunta[Resp_4] <input type='radio' name='r'><br/>\n";

->

?>
<p><input type="radio" value="<?php echo $Pregunta[Resp_1]; ?>" checked name="r">[<?php echo "A.".$Pregunta[Resp_1]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_2]; ?>" name="r">[<?php echo "B.".$Pregunta[Resp_2]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_3]; ?>" name="r">[<?php echo "C.".$Pregunta[Resp_3]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_4]; ?>" name="r">[<?php echo "D.".$Pregunta[Resp_4]; ?>]</p>


The Radio control use value="xyz" to difference itself.. you forgot it in your code. easy fix.
$Pregunta_Selection = $_POST['r'];
Hope this help.
  #3 (permalink)  
Old January 29th, 2006, 12:23 PM
Registered User
 
Join Date: Jan 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry for the delay! I was out for a bit of a rest!!
I tried to use your code but still does the same thing: when I select the radio boton of one question, the one I selected previously unselects itself... :((((

thanks a lot for your help!!!

  #4 (permalink)  
Old February 12th, 2006, 07:34 PM
Friend of Wrox
 
Join Date: Dec 2004
Location: London, London, United Kingdom.
Posts: 154
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You need to change the name of the radio button.

Example:
1st Selection:
<p><input type="radio" value="<?php echo $Pregunta[Resp_1]; ?>" name="r">[<?php echo "A.".$Pregunta[Resp_1]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_2]; ?>" name="r">[<?php echo "B.".$Pregunta[Resp_2]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_3]; ?>" name="r">[<?php echo "C.".$Pregunta[Resp_3]; ?>]</p>

Then next selection:
<p><input type="radio" value="<?php echo $Pregunta[Resp_1]; ?>" name="r2">[<?php echo "A.".$Pregunta[Resp_1]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_2]; ?>" name="r2">[<?php echo "B.".$Pregunta[Resp_2]; ?>]</p>
<p><input type="radio" value="<?php echo $Pregunta[Resp_3]; ?>" name="r2">[<?php echo "C.".$Pregunta[Resp_3]; ?>]</p>

The name is what link the radio together. I hope that help.


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL MiNdWaRp PHP How-To 0 May 14th, 2007 06:25 PM
begin php & mysql - chapter 12, user_form.php jon_stubber Beginning PHP 1 March 9th, 2006 10:57 AM
Storing form array data to MySQL using PHP osemollie Beginning PHP 0 February 2nd, 2006 07:32 AM
How to script search form in PHP/MYSQL javabeans PHP How-To 0 January 12th, 2004 03:53 AM
Editing MySQL data from a PHP form cmiller Beginning PHP 2 September 2nd, 2003 05:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.