khalidorama Posted November 5, 2007 Share Posted November 5, 2007 Hi, I am developing simple exam proprame in PHP. I want when I click the answer to update a record in a table for the chosen answer, so users can complete the test any time later. So,whenever the user clicks one of the choices, a corresponding record to the chosen answer should be added to DB. Technically, I have some difficulties. I want to put onclick event handler for the radio group buttons I am using. So , I need to pass exam question and chosen answer variables to javascript. It seems that I am doing a mistake cus I can't pass the variables. Could you please advise? <html> <script type="text/javascript src="record.php"></script> <body> <?php $db = mysql_connect("localhost", "sarabicc_khaled", "khaled"); mysql_select_db("sarabicc_Test", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>"); $nav = new navbar; // set how many records to show at a time $nav->numrowsperpage = 1; $sql = "SELECT * FROM exam_questions"; // the third parameter of execute() is optional $result = $nav->execute($sql, $db, "mysql"); // handle the returned result set $rows = mysql_num_rows($result); for ($y = 0; $y < $rows; $y++) { $data = mysql_fetch_object($result); $selected = $_REQUEST['group']; $selquery = "insert into exam_tests(questions,answers) values(\"1\",\"2\")"; if (isset($selected)) { mysql_query($selquery); } echo $data->id ." - " . $data->question . "<br>\n"; echo "<br>\n"; // build the returned array of navigation links $links = $nav->getlinks("sides", "on"); for ($y = 0; $y < count($links); $y++){ echo $links[$y] . " "; } function dolinks() { $links = $nav->getlinks("sides", "on"); for ($y = 0; $y < count($links); $y++){ echo $links[$y] . " "; } } echo "<form action =\"navbar.php\" name=\"bar\" method=\"get\">"; echo "<table border=2>"; echo "<tr>"; echo "<td align=left>"; echo "<INPUT TYPE=\"radio\" NAME=\"group\" VALUE=\"1\" onclick=\"populate(document.bar.group[0].value,'$_REQUEST[row]');\">"; echo "<font size=\"4\">$data->ch1</font>"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td align=left>"; echo "<INPUT TYPE=\"radio\" NAME=\"group\" VALUE=\"2\" onclick=\"populate(document.bar.group[1].value,$_REQUEST[row]);\" >"; echo "<font size=\"4\">$data->ch2</font>"; echo " </td>"; echo "</tr>"; echo "<tr>"; echo "<td align=left>"; echo "<INPUT TYPE=\"radio\" NAME=\"group\" VALUE=\"3\" onclick=\"populate(document.bar.group[2].value,$_REQUEST[row]);\>"; echo "<font size=\"4\">$data->ch3</font>"; echo "</tr>"; echo "<tr>"; echo "<td align=left>"; echo "<INPUT TYPE=\"radio\" NAME=\"group\" VALUE=\"4\" onclick=\"populate(document.bar.group[3].value,$_REQUEST[row]);\>"; echo "<font size=\"4\">$data->ch4</font>"; echo "</tr>"; echo "</table>"; } echo "<hr>\n"; echo "</form>"; /* Class navbar Copyright Joao Prado Maia (jpm@musicalidade.net) Sweet little class to build dynamic navigation links. Please notice the beautiful simplicity of the code. This code is free in any way you can imagine. If you use it on your own script, please leave the credits as it is. Also, send me an e-mail if you do, it makes me happy Below goes an example of how to use this class: =============================================== <?php $nav = new navbar; $nav->numrowsperpage = 3; $sql = "SELECT * FROM links "; $result = $nav->execute($sql, $db, "mysql"); $rows = mysql_num_rows($result); for ($y = 0; $y < $rows; $y++) { $data = mysql_fetch_object($result); echo $data->url . "<br>\n"; } echo "<hr>\n"; $links = $nav->getlinks("all", "on"); for ($y = 0; $y < count($links); $y++) { echo $links[$y] . " "; } ?> */ class navbar { // Default values for the navigation link bar var $numrowsperpage = 10; var $str_previous = "Previous question"; var $str_next = "Next question"; // Variables used internally var $file; var $total_records; // The next function runs the needed queries. // It needs to run the first time to get the total // number of rows returned, and the second one to // get the limited number of rows. // // $sql parameter : // . the actual SQL query to be performed // // $db parameter : // . the database connection link // // $type parameter : // . "mysql" - uses mysql php functions // . "pgsql" - uses pgsql php functions function execute($sql, $db, $type = "mysql") { global $total_records, $row, $numtoshow; $numtoshow = $this->numrowsperpage; if (!isset($row)) $row = 0; $row = $_REQUEST['row']; $start = $row * $numtoshow; if ($type == "mysql") { $result = mysql_query($sql, $db); $total_records = mysql_num_rows($result); $sql .= " LIMIT $start, $numtoshow"; $result = mysql_query($sql, $db); } elseif ($type == "pgsql") { $result = pg_Exec($db, $sql); $total_records = pg_NumRows($result); $sql .= " LIMIT $numtoshow, $start"; $result = pg_Exec($db, $sql); } return $result; } // This function creates a string that is going to be // added to the url string for the navigation links. // This is specially important to have dynamic links, // so if you want to add extra options to the queries, // the class is going to add it to the navigation links // dynamically. function build_geturl() { global $REQUEST_URI, $REQUEST_METHOD, $HTTP_GET_VARS, $HTTP_POST_VARS; list($fullfile, $voided) = explode("?", $REQUEST_URI); $this->file = $fullfile; $cgi = $REQUEST_METHOD == 'GET' ? $HTTP_GET_VARS : $HTTP_POST_VARS; reset ($cgi); while (list($key, $value) = each($cgi)) { if ($key != "row") $query_string .= "&" . $key . "=" . $value; } return $query_string; } // This function creates an array of all the links for the // navigation bar. This is useful since it is completely // independent from the layout or design of the page. // The function returns the array of navigation links to the // caller php script, so it can build the layout with the // navigation links content available. // // $option parameter (default to "all") : // . "all" - return every navigation link // . "pages" - return only the page numbering links // . "sides" - return only the 'Next' and / or 'Previous' links // // $show_blank parameter (default to "off") : // . "off" - don't show the "Next" or "Previous" when it is not needed // . "on" - show the "Next" or "Previous" strings as plain text when it is not needed function getlinks($option = "all", $show_blank = "off") { global $total_records, $row, $numtoshow; $extra_vars = $this->build_geturl(); $file = $this->file; $number_of_pages = ceil($total_records / $numtoshow); $subscript = 0; for ($current = 0; $current < $number_of_pages; $current++) { if ((($option == "all") || ($option == "sides")) && ($current == 0)) { if ($row != 0) $array[0] = '<A HREF="' . $file . '?row=' . ($row - 1) . $extra_vars . '" onclick=\"submit();\">' . $this->str_previous . '</A>'; elseif (($row == 0) && ($show_blank == "on")) $array[0] = $this->str_previous; } if (($option == "all") || ($option == "pages")) { if ($row == $current) $array[++$subscript] = ($current > 0 ? ($current + 1) : 1); else $array[++$subscript] = '<A HREF="' . $file . '?row='. $current . $extra_vars . '" onclick=\"submit();\">' . ($current + 1) . '</A>'; } if ((($option == "all") || ($option == "sides")) && ($current == ($number_of_pages - 1))) { if ($row != ($number_of_pages - 1)) $array[++$subscript] = '<A HREF="' . $file . '?row=' . ($row + 1) . $extra_vars . '" onclick=\"submit();\">' . $this->str_next . '</A>'; elseif (($row == ($number_of_pages - 1)) && ($show_blank == "on")) $array[++$subscript] = $this->str_next; } } return $array; } } ?> Quote Link to comment Share on other sites More sharing options...
khalidorama Posted November 5, 2007 Author Share Posted November 5, 2007 Here is the code of record.php file: <? //"ip.php" example- display user IP address on any page Header("content-type: application/x-javascript"); echo "function populate(var1 ,var2 ) { alert(var1." ".var2); }"; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.