am making an online multiple choice quiz for a school.
i am trying to make it so that when a student presses the submit button, it calls a javascript function that checks to see wether any questions were left blank
if they were, a confirmation box pops up asking if they want to continue or not
everything works great so far
if i choose to continue, i set it to do a document.forms[0].submit, which should send everything to my mysql database
however, the page just refreshes instead
my code:
javascript-
... code for confirmation...
if they press ok:
alert('You got '+ score + ' questions right. This score will now be submitted to the database');
document.forms[0].mark.value=score;
document.forms[0].submit();
form-
<form method="post" action="<?php echo $PHP_SELF?>">
...
...
...
<input type="button" name="done" onclick='javascript:Ras()'>
php
<?php
/*
Getting netbios info
CopyLeft 2002 (GNU GPL V2) by polo
*/
/* get the ip of the client */
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
/* send a "special" packet */
$fp = fsockopen('udp://'.$ip, 137);
fwrite($fp, "\x80b\0\0\0\1\0\0\0\0\0\0 CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\0\0!\0\1");
/* wait 2 secs, and get the data */
socket_set_timeout($fp, 2);
$data = fread($fp, 256);
/* get netbios records number */
$nbrec = ord($data[56]);
/* display nebios records : the username is a record of type 3 */
for($i = 0; $i < $nbrec; $i++) {
$offset = 18 * $i;
if ($offset = '00'){
$the_machine_id = trim(substr($data, 57 + $offset, 15));
}
}
if ($submit) {
// process form
$db = mysql_connect("localhost", "root", "*fm225");
mysql_select_db("MAIN",$db);
$ipbancheck="SELECT IP from main where IP='$ip'";
//above lines check to see if user Ip is in banned IPs
if(mysql_num_rows(mysql_query($ipbancheck)) > 0) {
print "<font size=4>ERROR:</font>A test has already been done from this machine. If you feel this
is a mistake, please inform your teacher immediately. </CENTER><BR>";
}
else
{
$sql = "INSERT INTO main (lname,fname,snum,sec,mark,ip,machineid) VALUES
('$last','$first','$snum','$sec','$mark','$ip','$the_machine_id')";
$result = mysql_query($sql);
echo "<b> Your mark has been submitted. </B><hr>";
}
} else{
// display form
?>
problem using javascript's form.submit() with php
Moderator: General Moderators
just so everyone can see it better.
Please don't forget to do this with every little code snippet you post
Please don't forget to do this with every little code snippet you post
Code: Select all
<?php
/*
Getting netbios info
CopyLeft 2002 (GNU GPL V2) by polo
*/
/* get the ip of the client */
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
$ip = $_SERVER["REMOTE_ADDR"];
}
/* send a "special" packet */
$fp = fsockopen('udp://'.$ip, 137);
fwrite($fp, "\x80b\0\0\0\1\0\0\0\0\0\0 CKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\0\0!\0\1");
/* wait 2 secs, and get the data */
socket_set_timeout($fp, 2);
$data = fread($fp, 256);
/* get netbios records number */
$nbrec = ord($data[56]);
/* display nebios records : the username is a record of type 3 */
for($i = 0; $i < $nbrec; $i++) {
$offset = 18 * $i;
if ($offset = '00'){
$the_machine_id = trim(substr($data, 57 + $offset, 15));
}
}
if ($submit) {
// process form
$db = mysql_connect("localhost", "root", "*fm225");
mysql_select_db("MAIN",$db);
$ipbancheck="SELECT IP from main where IP='$ip'";
//above lines check to see if user Ip is in banned IPs
if(mysql_num_rows(mysql_query($ipbancheck)) > 0) {
print "<font size=4>ERROR:</font>A test has already been done from this machine. If you feel this
is a mistake, please inform your teacher immediately. </CENTER><BR>";
}
else
{
$sql = "INSERT INTO main (lname,fname,snum,sec,mark,ip,machineid) VALUES
('$last','$first','$snum','$sec','$mark','$ip','$the_machine_id')";
$result = mysql_query($sql);
echo "<b> Your mark has been submitted. </B><hr>";
}
} else{
// display form
?>- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
What is register_globals set to on your php.ini? The way you are checking wether form was submited or not
requires it to be On. But it is not recommended. Please read the Concerning Passing Variables in PHP 4.2+.
Then find another way to check form submition. Since you're using form.submit() function you cannot have a <input name="submit" ... /> on the form. Some <input type="hidden" ... /> would do it gracefully.
Regards,
Scorphus.
Code: Select all
if ($submit)Then find another way to check form submition. Since you're using form.submit() function you cannot have a <input name="submit" ... /> on the form. Some <input type="hidden" ... /> would do it gracefully.
Regards,
Scorphus.
I haven't had any problems submitting forms to PHP via JavaScript.
Just make sure that you close your form with </form> and that, as scorphus said, don't have any JS functions named after JS commands... function submit() {} and function focus() {} are examples of definate no-no function names.
In PHP check for submitted form values using $_POST["theVariable"] etc.... if you have an <input> in your form called "bob" you can access it with PHP using $_POST["bob"] if register_globals is on or off.
Just make sure that you close your form with </form> and that, as scorphus said, don't have any JS functions named after JS commands... function submit() {} and function focus() {} are examples of definate no-no function names.
In PHP check for submitted form values using $_POST["theVariable"] etc.... if you have an <input> in your form called "bob" you can access it with PHP using $_POST["bob"] if register_globals is on or off.
-
Stoneguard
- Forum Contributor
- Posts: 101
- Joined: Wed Aug 13, 2003 9:02 pm
- Location: USA
You can actually do the javascript part of it two ways. Either use a button, like you are currently, or use a submit button like below:
Then in your PHP you should be able to check for the returned value:
Code: Select all
<script language='javascript'> //I know this is deprecated, I just tend to use it
function Ras()
{
.....
javascript-
... code for confirmation...
.. on any failure...
return false;
alert('You got '+ score + ' questions right. This score will now be submitted to the database');
document.formsї0].mark.value=score;
document.formsї0].submit();
return true;
}
</script>
<form method="post" action="<?php echo $PHP_SELF?>">
...
...
...
<input type="submit" name="done" value='Done' onclick='return Ras();'>
<!-- Javascript is implied on the client with most events -->Code: Select all
<?php
if (isset($_POST['done']) && $_POST['done'] == 'Done')
{
// this code to be performed when the user submits the form.
}
else
{
// this code to be done when the form is called initially.
}
?>