Solved

if condition true, alert and redirect

Posted on 2008-10-05
37
1,710 Views
Last Modified: 2013-12-12
Hello,
I am trying to redirect the user with an alert message if a condition is true.
The page redirects without showing the alert bos.
What;s more, rest of the php script does not stop bu continues running.

This is for a test page: If the test has already been completed in the session or in an earlier session, I want to stop access to that page.
Here is the relevant part of the code:
<?php // EngTest1.php
include("phpinclude/accesscontrol.php");
   dbConnect(my database);
$sql="SELECT complete FROM eng1test WHERE fk_userid='".$uid."'";
$result=mysql_query($sql);
if (!$result) {
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.');
    }
$EngStatus['complete']=$row['complete'];
if ($EngStatus['complete']==1){
echo '<script language="javascript">confirm("Project details have been updated!")</script>';

echo '<script language="javascript">window.location = "http://ccgi.nyorulmaz.plus.com/MathsTest1.php"</script>';

}else
{
if (!isset($_POST['submit'])){
    // Display the English Test form
    ?>
<html>
The test form starts here..............

</html>
rest of the php code................
<?php
}
else
//This code runs if the form has been submitted
    //dbConnect('nyorulmaz_pn');
    // Process English test submission
// Create an empty array to hold the error messages.
$arrCorAns = array();
$arrPunc = array();
    // Each time there's a correct answer, add "correct#" to the array
    // using the field name as the key.
    if ($_POST['q1']==1)
        $arrCorAns['q1'] = 'correct1';
    if ($_POST['q2']==1)
        $arrCorAns['q2'] = 'correct2';
etc.........and

// now we insert it into the database

$sql = "INSERT INTO eng1test (fk_userid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, scoreng1, punc_mark, complete)
VALUES ('".$uid."','".$_POST['q1']."','".$_POST['q2']."','".$_POST['q3']."','".$_POST['q4']."','".$_POST['q5']."','".$_POST['q6']."','".$_POST['q7']."','".$_POST['q8']."','".$_POST['q9']."','".$_POST['q10']."','".((count($arrCorAns) + count($arrPunc)*0.4)*100/14)."','".(count($arrPunc))."', '1')";

    if (!mysql_query($sql))
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.\\n' . mysql_error());
}
?>
<script>
window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';
</script>
0
Comment
Question by:Erikal
  • 16
  • 11
  • 10
37 Comments
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22646582
to do a javascript alert you need to have alert("alert text here"); I do not see that anywhere in your code. I understand your question correctly. you would do the following. In the javascript script section.


    if (!mysql_query($sql))

        $error('A database error occurred in processing your '.

              'submission.\\nIf this error persists, please '.

              'contact eric@lpf-learning.co.uk.\\n' . mysql_error());

}

?>

<? if ($error == null){

?>

<script>

window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';

</script>

<?

} else {

?>

<script>

alert(<?=$error?>);

window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';

</script>

<?

}

?>

Open in new window

0
 

Author Comment

by:Erikal
ID: 22646642
Thank you for your help.
I am afraid I am a beginner at this and I am not quite sure if I understand this.
Basically, The above code is for one of three test pages (this is the English one)
If the user have already completed and submited the English test, they should not be able to go back and retake it. They should be alerted to this fact and when they press the OK button of the alert, should be directed to the second test page, which is maths.
With my code above, the page redirects, whether or not the condition (complete field being updated) is true or not.
Also, the rest of the php script apparently keeps running, as every time I test the page a new record is added to the English database table.


0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22646673
ok then you need to do a database query to see if the english test has been completed. Something similar to the following.

$result = mysql_query("check if this user has submited the english test before");
if (!$result){
     //There was a mysql error that php encountered
}
if (mysql_num_rows($result)){
    //The use has submitted the pae so give them the allert error, then forward them
}
else {
     //Put the stuff in the database
}

If the code keeps running you can put a die() in right after you forward the user, and it will stop the php code from running.

Let me know if that helps.
0
 

Author Comment

by:Erikal
ID: 22646759
OK
I tried to implemet what you suggested.
Now I get the following erro message:
Parse error: parse error, unexpected '}' in /share/storage/03/ny/nyorulmaz/EngTest1.php on line 18
Below is how I wrote the code:
<?php // EngTest1.php
include("phpinclude/accesscontrol.php");
   dbConnect('nyorulmaz_pn');
$result = mysql_query("SELECT complete FROM eng1test WHERE fk_userid='".$uid."'");
if (!$result) {
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.');
    }
if (mysql_num_rows($result)){
?>
<script>
alert(<?=$error?>);
window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';
</script>
<?
die()
}
else {
    // Display the English Test form
    ?>
<html>
...
...
...
<?php
//This code runs if the form has been submitted
    //dbConnect('nyorulmaz_pn');
    // Process English test submission
// Create an empty array to hold the error messages.
$arrCorAns = array();
$arrPunc = array();
    // Each time there's a correct answer, add "correct#" to the array
    // using the field name as the key.
    if ($_POST['q1']==1)
        $arrCorAns['q1'] = 'correct1';
    if ($_POST['q2']==1)
        $arrCorAns['q2'] = 'correct2';
    if ($_POST['q3']==1)
        $arrCorAns['q3'] = 'correct3';
    if ($_POST['q4']==1)
        $arrCorAns['q4'] = 'correct4';
    if ($_POST['q5']==1)
        $arrCorAns['q5'] = 'correct5';
    if ($_POST['q6']==1)
        $arrCorAns['q6'] = 'correct6';
    if ($_POST['q7']==1)
        $arrCorAns['q7'] = 'correct7';
    if ($_POST['q8']==1)
        $arrCorAns['q8'] = 'correct8';
    if ($_POST['q9']==1)
        $arrCorAns['q9'] = 'correct9';
    if ($_POST['q10']==1)
        $arrCorAns['q10'] = 'correct10';
    if ($_POST['q11']==" ")
        $arrPunc['q11'] = 'correct11';
    if ($_POST['q12']==",")
        $arrPunc['q12'] = 'correct12';
    if ($_POST['q13']=="\"")
        $arrPunc['q13'] = 'correct13';
    if ($_POST['q14']==" ")
        $arrPunc['q14'] = 'correct14';
    if ($_POST['q15']==" ")
        $arrPunc['q15'] = 'correct15';
    if ($_POST['q16']=="!")
        $arrPunc['q16'] = 'correct16';
    if ($_POST['q17']=="\"")
        $arrPunc['q17'] = 'correct17';
    if ($_POST['q18']=="\"")
        $arrPunc['q18'] = 'correct18';
    if ($_POST['q19']==",")
        $arrPunc['q19'] = 'correct19';
    if ($_POST['q20']=="\"")
        $arrPunc['q20'] = 'correct20';
    if ($_POST['q21']==" ")
        $arrPunc['q21'] = 'correct21';
    if ($_POST['q22']==".")
        $arrPunc['q22'] = 'correct22';
    if ($_POST['q23']=="\"")
        $arrPunc['q23'] = 'correct23';
    if ($_POST['q24']=="\"")
        $arrPunc['q24'] = 'correct24';


// now we insert it into the database

$sql = "INSERT INTO eng1test (fk_userid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, scoreng1, punc_mark, complete)
VALUES ('".$uid."','".$_POST['q1']."','".$_POST['q2']."','".$_POST['q3']."','".$_POST['q4']."','".$_POST['q5']."','".$_POST['q6']."','".$_POST['q7']."','".$_POST['q8']."','".$_POST['q9']."','".$_POST['q10']."','".((count($arrCorAns) + count($arrPunc)*0.4)*100/14)."','".(count($arrPunc))."', '1')";

    if (!mysql_query($sql)){
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.\\n' . mysql_error());
}}
?>
<script>
window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';
</script>
0
 
LVL 82

Expert Comment

by:hielo
ID: 22646893
read comments in code:
<?php // EngTest1.php
include("phpinclude/accesscontrol.php");
   dbConnect(my database);
   
//HIELO SAYS: if fk_userid is a numeric data type, do NOT put apostrophes around the value
$sql="SELECT complete FROM eng1test WHERE fk_userid=".$uid;
$result=mysql_query($sql);
if (!$result) {
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.');
    }
//HIELO SAYS: you need to "extract" the data first
$row=mysql_fetch_assoc($result); 
$EngStatus['complete']=(int)$row['complete'];
if ($EngStatus['complete']==1){
	echo '<script language="javascript">confirm("Project details have been updated!")</script>';
	echo '<script language="javascript">window.location = "http://ccgi.nyorulmaz.plus.com/MathsTest1.php"</script>';
	exit;
}
else
{
	if (!isset($_POST['submit']))
	{
    // Display the English Test form
    ?>
	<html>
	The test form starts here..............
	
	</html>
	rest of the php code................
<?php
	}
	else//HIELO SAYS: if this else is a multi-statement block you need to use braces where appropriate
	{
		//This code runs if the form has been submitted
	    //dbConnect('nyorulmaz_pn');
	    // Process English test submission
		// Create an empty array to hold the error messages.
		$arrCorAns = array();
		$arrPunc = array();
	    // Each time there's a correct answer, add "correct#" to the array
	    // using the field name as the key.
	    if ($_POST['q1']==1)
	        $arrCorAns['q1'] = 'correct1';
	    if ($_POST['q2']==1)
	        $arrCorAns['q2'] = 'correct2';
		etc.........and 
		// now we insert it into the database 
		$sql = "INSERT INTO eng1test (fk_userid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, scoreng1, punc_mark, complete)
		VALUES ('".$uid."','".$_POST['q1']."','".$_POST['q2']."','".$_POST['q3']."','".$_POST['q4']."','".$_POST['q5']."','".$_POST['q6']."','".$_POST['q7']."','".$_POST['q8']."','".$_POST['q9']."','".$_POST['q10']."','".((count($arrCorAns) + count($arrPunc)*0.4)*100/14)."','".(count($arrPunc))."', '1')"; 
		if (!mysql_query($sql))
     	   error('A database error occurred in processing your '.
     	         'submission.\\nIf this error persists, please '.
     	         'contact eric@lpf-learning.co.uk.\\n' . mysql_error());
	}
	echo "<script>window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';</script>";
}
?>

Open in new window

0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22646903
ok your code above is fine, just on line 17 you need a ; after the die()
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22646907
you could also use a confirm box like Sage did, the only difference of a confirm to a alert, is a confirm has a ok and a cancel button. You will need to have a javascript if statement if you did the confirm box to see what button the user hit, then based on what button they hit. decides where you send them.

Let me know if you need anything else.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22647069
>>like Sage did
Sage is just a rank. There are many Sages on this forum :)
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647077
Sry i am a newby. I will learn what is up here soon.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22647148
No problem. That is a very common mistake here.

On another note, welcome
0
 

Author Comment

by:Erikal
ID: 22647216
Hello again Hielo,
I am nearly there I think.
However, the the script seems to be going straight through for all 3 tests, even though 'complete' is not yet updated to '1' for the second and third tests, as the test can not be viewed or submitted. The first test somehow updates the non form related fileds only.
Also the alert box only appears for the first test page and sunsequent two do not even display the alert dialog.
I am not sure what I am doing wrong here?
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647254
ok can you post the code for all 3 pages please.
0
 

Author Comment

by:Erikal
ID: 22647280
Right,
I am attaching all three test pages.
I have taken out the html sections, where the rest of the page content and the form sits.
The only php code inthose sections is the form action bit, which is same for all three pages:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">

EngTest1.txt
MathsTest1.txt
VRTest1.txt
0
 
LVL 82

Expert Comment

by:hielo
ID: 22647290
>>However, the the script seems to be going straight through for all 3 tests, even though 'complete' is not yet updated to '1'
you lost me there. I'm not sure what part of the script you are referring to. If you are saying that all of them return a record and the first if always is executed, then perhaps what you need to something like:
$sql="SELECT complete FROM eng1test WHERE complete != 1 AND fk_userid=".$uid;
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647302
ok whell one thing i see that might not be desirable is that the jovascript confirm box is pretty pointless with how it is set up, because if you hit cancel or ok, then the user will still be directed to the correct page.

ok the code seems to make sence to me in the english example. I am wondering if there is a  error in your database somewhere that is not giving you the correct output. I would echo out your $row['complete']; and see if you are receiving the expected output.

I also do not understand why you are assigning $EngStatus['complete'] to $row['complete'];.

I do not see a array named EngStatus defined anywhere.
0
 

Author Comment

by:Erikal
ID: 22647328
>>you lost me there. I'm not sure what part of the script you are referring to.
When I sign in with a new username and login, I am directed to the test access pages, from there I can access the English test.
As the complete field is not yet updated (since the test from - which also contain the 'complete'filed- is neither dispalyed nor manually submitted, there should be no updating. But, the fields like 'complte' is updated to '1' and array count values and all from input valuse are updated to '0'.
The English page displays the alert message and when I click the OK button, it rapidly goes through the other two test pages, without dispaying the test form and without inserting anything into their corresponding database tables and jumps to the Results page.
I am not sure if I have managed to explain what happens.

0
 

Author Comment

by:Erikal
ID: 22647332
BTW:
I have spotted the alert redirection URLs all going to the same page and corrected them, but nothing changes.
0
 

Author Comment

by:Erikal
ID: 22647347
>>jamespbyrne: said: ok whell one thing i see that might not be desirable is that the jovascript confirm box is pretty pointless with how it is set up, because if you hit cancel or ok, then the user will still be directed to the correct page. -----------

If it helps:  All three pages and the subsequent results and scores page were working without any problem. Things began going wrong when I attempted to introduce the 'no return to completed pages' concept.
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647405
whell to me it sounds like the redirect is not working because of some code on pages 2 and three. So page 1 redirects to page 2 right then if the entry is in page 2 it forwards to page 3 is that correct.

And right now it is forwarding without mentioning the confirm box that it should do before it redirects again right?

I just want to make sure we are on the same page before i work on this somemore.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22647452
>> But, the fields like 'complte' is updated to '1' and array count values and all from input valuse are updated to '0'.
OK. Try changing:
if (!isset($_POST['submit']))

to:
if (!isset($_POST['complete']))

assuming you have a hidden field with name="complete"
0
 

Author Comment

by:Erikal
ID: 22647478
>> jamespbyrne: And right now it is forwarding without mentioning the confirm box that it should do before it redirects again right?

What you assume is correct.
However, the problem is none of these pages should redirect before the test is completed and submitted.
So the problem is not only that they redirect without mentioning the confirm box, but also they redirect prematurely.
0
 

Author Comment

by:Erikal
ID: 22647488
>>hielo: assuming you have a hidden field with name="complete"

The complete filed is not a hidden field. it is the last filed in each of the 3 test page sql tables.
They should only be updated to '1' when the form is completed and manually submitted when the user clicks the submit button of the form.
Does it have to be a hidden field and should I still change
if (!isset($_POST['submit']))
to:
if (!isset($_POST['complete']))
0
 

Author Comment

by:Erikal
ID: 22647505
>>hielo: assuming you have a hidden field with name="complete"
Am I geting this all wrong?
When you say hiiden field for 'complete do you meam a form input field or databse table field?
0
 

Author Comment

by:Erikal
ID: 22647514
Thank you very much for all your efforts.
This is taking so much of your time and I like to split the question into three different questions if it is OK with you.
Being a beginner, I did not realise it would be such an involved issue and I think this is worth much more points. As maximum points allowed is 500 I think it is only right that while keping this thread going on the English page, I should raise 2 new questions for you for the other test pages.
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647538
what he means but hidden field is code like <input type='hidden' name='complete' value='0'>

this will create a hidden form field that the user can not see, the deal is that it will be interpreted as having the value as 0 when you actually go to get the form data from php.

Also can you give me the links to the other questions. I am going to head to bed for tonight, but i would like to look at this again tomorow, if it is still open.
0
 

Author Comment

by:Erikal
ID: 22647562
Sure, but you need to tell me what you mean the links to other questions.
How do i do that?Please let me know before you go to bed.
0
 
LVL 1

Expert Comment

by:jamespbyrne
ID: 22647574
oh ya no problem, sry i did not make it clar i mean like the link hat your browser shows in the address bar, it will be like experts-exchange.com/...

It just makes it easer to find on my part. I am new to this site and do not really know how to use it that well yet. But i would like to help you solve this issue.

So just to be clear where we are right now, you go to the english test, then it gives you the javascript confirm box, the confirm box then takes you to the secondpage, which then frowards you to the third page, which then forwards you someplace else. is that correct?
0
 

Author Comment

by:Erikal
ID: 22647603
yes that is correct, not forgetting that the redirecting is done prematurely, before the user even sees the page, let alone has a chance to take thes test.

here are the two links:
1- http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23789658.html

2- http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/Q_23789653.html
0
 

Author Comment

by:Erikal
ID: 22647609
Hi hielo,
I hope you 've also seen the links to two new questions on this.

Many thanks for your continuous help on this and other previous questions.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22650202
In post ( ID: 22647328) you said:
"As the complete field is not yet updated (since the test from - which also contain the 'complete'filed- is neither dispalyed nor manually submitted, there should be no updating..."

which led me to believe that somewhere on your page you have an html hidden input field. That's what I was referring to.

After re-reading post ID: 22647328, it sounds like you have something like:
EnglishTest.php (which is meant to represent the code we have been modifying) but to get to EnglishTest.php you have some other page (preEnglishTest.php) that has a submit with name="submit" AND that preEnglishTest.php page submits/posts to EnglishTest.php. Under those circustances, then it is expected that EnglishTest.php behaves in the way you described.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22650471
Here's an example of what I am talking about:
<form method="post" action="/MathsTest1.php">

    User ID: <input type="text" name="uid" size="8" /><br />

    Password: <input type="password" name="pwd" SIZE="8" /><br />

    <input type="submit" value="Log in" />

  </form>
 

when that form is submitted, in MatheTest1.php this:

if( isset($_POST['submit']) ) WILL BE TRUE, even though you have NOT submitted the real quiz/test answers.
 

Currently you have:
 

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

{

    // Display the English Test form

?>

<html>

--------

---------

</html>

<?php

}

else

{...}
 

In between that HTML you must have some other html form that causes the script to post to itself. The question is, what is the value of the submit button. It should be different from the original login form (which has value="Log In"), so you should be checking for the value of the NON-Login form.
 

Perhaps it will make more sense if you look at the attached code:
 

<?php // EngTest1.php

include("phpinclude/accesscontrol.php");

dbConnect('nyorulmaz_pn');

   

//HIELO SAYS: if fk_userid is a numeric data type, do NOT put apostrophes around the value

$sql="SELECT complete FROM eng1test WHERE fk_userid='".$uid."'";

$result=mysql_query($sql);

if (!$result) {

        error('A database error occurred in processing your '.

              'submission.\\nIf this error persists, please '.

              'contact eric@lpf-learning.co.uk.');

}

//HIELO SAYS: you need to "extract" the data first

$row=mysql_fetch_assoc($result); 

$EngStatus['complete']=(int)$row['complete'];

if ($EngStatus['complete']==1){

	echo '<script language="javascript">confirm("You have already completed the English Test. Press OK to be directed to the Maths Test")</script>';

	echo '<script language="javascript">window.location = "http://ccgi.nyorulmaz.plus.com/MathsTest1.php"</script>';

	exit;

}

else

{

	if ( "Submit Test" != strval($_POST['submit']) )

	{

    // Display the English Test form
 

    //The value of this submit button should be different from that of the  login form

	//notice how the value of this submit form is what we use for our test case above

    ?>

<html>

--------

	<form>

	  <input type="submit" name="submit" value="Submit Test"/>

	</form>

---------

</html>

<?php

	}

else

	{

//This code runs if the form has been submitted

    //dbConnect('nyorulmaz_pn');

    // Process English test submission

// Create an empty array to hold the error messages.

$arrCorAns = array();

$arrPunc = array();

    // Each time there's a correct answer, add "correct#" to the array

    // using the field name as the key.

    if ($_POST['q1']==1)

        $arrCorAns['q1'] = 'correct1';

    if ($_POST['q2']==1)

        $arrCorAns['q2'] = 'correct2';

    if ($_POST['q3']==1)

        $arrCorAns['q3'] = 'correct3';

    if ($_POST['q4']==1)

        $arrCorAns['q4'] = 'correct4';

    if ($_POST['q5']==1)

        $arrCorAns['q5'] = 'correct5';

    if ($_POST['q6']==1)

        $arrCorAns['q6'] = 'correct6';

    if ($_POST['q7']==1)

        $arrCorAns['q7'] = 'correct7';

    if ($_POST['q8']==1)

        $arrCorAns['q8'] = 'correct8';

    if ($_POST['q9']==1)

        $arrCorAns['q9'] = 'correct9';

    if ($_POST['q10']==1)

        $arrCorAns['q10'] = 'correct10';

    if ($_POST['q11']==" ")

        $arrPunc['q11'] = 'correct11';

    if ($_POST['q12']==",")

        $arrPunc['q12'] = 'correct12';

    if ($_POST['q13']=="\"")

        $arrPunc['q13'] = 'correct13';

    if ($_POST['q14']==" ")

        $arrPunc['q14'] = 'correct14';

    if ($_POST['q15']==" ")

        $arrPunc['q15'] = 'correct15';

    if ($_POST['q16']=="!")

        $arrPunc['q16'] = 'correct16';

    if ($_POST['q17']=="\"")

        $arrPunc['q17'] = 'correct17';

    if ($_POST['q18']=="\"")

        $arrPunc['q18'] = 'correct18';

    if ($_POST['q19']==",")

        $arrPunc['q19'] = 'correct19';

    if ($_POST['q20']=="\"")

        $arrPunc['q20'] = 'correct20';

    if ($_POST['q21']==" ")

        $arrPunc['q21'] = 'correct21';

    if ($_POST['q22']==".")

        $arrPunc['q22'] = 'correct22';

    if ($_POST['q23']=="\"")

        $arrPunc['q23'] = 'correct23';

    if ($_POST['q24']=="\"")

        $arrPunc['q24'] = 'correct24';
 
 

// now we insert it into the database
 

$sql = "INSERT INTO eng1test (fk_userid, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10, scoreng1, punc_mark, complete)

VALUES ('".$uid."','".$_POST['q1']."','".$_POST['q2']."','".$_POST['q3']."','".$_POST['q4']."','".$_POST['q5']."','".$_POST['q6']."','".$_POST['q7']."','".$_POST['q8']."','".$_POST['q9']."','".$_POST['q10']."','".((count($arrCorAns) + count($arrPunc)*0.4)*100/14)."','".(count($arrPunc))."', '1')";
 

    if (!mysql_query($sql))

        error('A database error occurred in processing your '.

              'submission.\\nIf this error persists, please '.

              'contact eric@lpf-learning.co.uk.\\n' . mysql_error());

	}

echo "<script>window.location.href='http://ccgi.nyorulmaz.plus.com/MathsTest1.php';</script>";

}

?>

Open in new window

0
 
LVL 1

Accepted Solution

by:
jamespbyrne earned 250 total points
ID: 22650937
The code above will redirect the page after the form gets submited. But i think that he wants to query the db then if somthing in he db is true, he wats to forward to the next page.

ok so here is ow i would edit the code if it was my project. Let me know if this works.

This would not really fix anything though, but in my min it makes it cleaner.

I would also temporarly get rid of the window.location in the javascript and see if it it hitting that page. (add and echo) and make sure the if statement is hitting correctly.
<?php // EngTest1.php
 

include("phpinclude/accesscontrol.php");
 

   dbConnect('nyorulmaz_pn');
 

   
 

//HIELO SAYS: if fk_userid is a numeric data type, do NOT put apostrophes around the value
 

$sql="SELECT complete FROM eng1test WHERE fk_userid='".$uid."'";
 

$result=mysql_query($sql);
 

if (!$result) {
 

        error('A database error occurred in processing your '.
 

              'submission.\\nIf this error persists, please '.
 

              'contact eric@lpf-learning.co.uk.');
 

    }
 

//HIELO SAYS: you need to "extract" the data first
 

$row=mysql_fetch_assoc($result); 
 

if ($row['complete'=='1'){

?>

<script language="javascript">

confirm("You have already completed the English Test. Press OK to be directed to the Maths Test");

window.location="http://ccgi.nyorulmaz.plus.com/MathsTest1.php"

</script>';

<?
 

	exit;
 

}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 22651036
>>The code above will redirect the page after the form gets submited. But i think that he wants to query the db then if somthing in he db is true, he wats to forward to the next page.
What he wants is to query the db first. IF a complete==1, go to next page
Otherwise check if the form was submiited. If no show blank form. This is the problem. He is not getting the blank form. Instead, the inner-else part executes all the time, thereby entering blank data into db. He needs to check for the explicit value of the submit button on the test page. Simple checking if
!isset($_POST['submit']) is NOT enough.
0
 

Author Comment

by:Erikal
ID: 22652665
Hello again,
I am sorry I am just back from teaching. Let me have a look at answers and come back to you.

Just one thing: I now realise what you mean by the hidden field for completed.
I actually have an include file (accesscontrol.php) which controls the login in and session_start etc.
I also have the registration file called assess_reg php.

I have actually created another table with hidden fields from in the assess_reg page with inputs named:
Eng1Comp, Maths1Comp, VR1 comp and they all have the value '0'.
When the user registers and the registration is successful, this table is popiulated by the hidden fileds.
This was my dilema: as I should only update this table values when the individual test pages are submitted. Since each user will only regiter once with a unique user name, I tied this table with this userid.

As I said I will study yuor recent contributions and come back to you asap.
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 250 total points
ID: 22654177
if you use this line:
if ( "Submit Test" != strval($_POST['submit']) )

it is expected that you have:



<input type="submit" name="submit" value="Submit Test"/>
 

NOT the other way around:

<input type="submit" name="Submit Test" value="submit"/>

Open in new window

0
 

Author Closing Comment

by:Erikal
ID: 31503271
You both have been incredable helpful and patient with me.
Thank you very much indeed.
0
 
LVL 82

Expert Comment

by:hielo
ID: 22654746
>>But i think that he wants to query the db then if somthing in he db is true
Yes indeed. As a matter of fact that accepted solution looks ALMOST, like a copy and paste from post ID: 22646893
except mine doesn't have this error:
if ($row['complete'=='1'){
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now