Link to home
Start Free TrialLog in
Avatar of Erikal
Erikal

asked on

Redirect only if condition true

I need to redirect pages if a db table is updated with user clicking the form submit button.
Also, I need an alert box popping up before redirect, so that the user is not suddenly plunged into tne next page, as these are timed test pages and the user should have a breather before going on to next test page, by clicking the confirm button when ready.
Avatar of sistemu
sistemu
Flag of Romania image

Hi,

after you updated the database, echo this page:
the '5' in the meta tag is the number of seconds before the redirect.
<html>
 <head> 
<title>Redirect ...</title> 
<META http-equiv="refresh" content="5;URL=http://your.new.webpage"> </head> 
<body>
<!-- if redirect not working: -->
<a href = "http://your.new.webpage"> Click here if you are not redirected</a>
<script type="text/javascript">
alert("Popup message");
 
</SCRIPT>
</body>
</html>

Open in new window

Avatar of hielo
>>if a db table is updated with user clicking the form submit button.
The value of that button is extremely important. You will need to make sure that value is received before attempting to rediredt the user, otherwise you will need to show the form -EX:
...
/* see if the form was actually submitted by checking for the specific value of the submit button on your form */
if ( "Submit Test" != strval($_POST['submit']) )
{
  /* if you make it in here, the form has not been submitted, so show the form. Notice that (in this example) the value of the input field will need to be "Submit Test"
  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 method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
	  <input type="submit" name="submit" value="Submit Test"/>
	</form>
---------
</html>
<?php
 exit;
}
else
{
 //update your db here
...
?>
<script>
alert("You are about to go to page 2")
window.location.href="page2.php";
</script>
<?php
 exit;
}
?>

Open in new window

Avatar of jamespbyrne
jamespbyrne

Can you post the code you have for this one.
Avatar of Erikal

ASKER

OK I have changed the name of the submit button for EngTtest1 to 'Submit_Eng1"
I have , as I said earlier, also adden hidden input fieds to the registration form (Eng1Comp, Maths1Comp , VR1Comp ets). When registation is successful, these fileds in the assess_reg table are set to value '0'.

I have also modofied the code in the EngTest1.php to incorporate these changes:
I think smoeyhing gone wrong with these modofocations as I now get database error alert, which keeps repeating itself each time alert OK is clicked and I can not get rid of it, except by forced closing the IE.

Here is the code:
========================
<?php // EngTest1.php
include("phpinclude/accesscontrol.php");
   dbConnect('nyorulmaz_pn');
   
$sql="SELECT eng1comp FROM assess_reg WHERE 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['eng1comp']=(int)$row['eng1comp'];
if ($EngStatus['eng1comp']==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_Eng1" !=strval($_POST['submit']))
      {
    // Display the English Test form
    ?>
<html>
...
...
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
...
...
<input type="submit" name="Submit_Eng1" value="Maths Test" STYLE="width: 126; height: 22; background-color: #fc3084; color: #ffffff; font-family: Arial; font-size: x-small; font-weight: bold; cursor: hand">
...
...
</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)
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))."')";

    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());
$sql = "UPDATE assess_reg SET eng1comp='1' WHERE userid='".$uid."'";

    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 language="javascript">window.location.href="http://ccgi.nyorulmaz.plus.com/MathsTest1.php"</script>';
}
?>
==============================================
I have also attached here the registration page, called assess_reg, which now also has the hidden fields for Test status
echo your mysql_error(). and let us know that this says. You need to put this in your (!$result) then it will tell you what is wrong.
Avatar of Erikal

ASKER

This is what I get:
Parse error: parse error, unexpected '}', expecting ',' or ';' in /share/storage/03/ny/nyorulmaz/EngTest1.php on line 12
ok is that coming out when you echo mysql error. is like 12 on your code just a comment?

also do you have a php function called error or did you want your error to be a variable?
Avatar of Erikal

ASKER

BTW I forgot to answer hielo/s question: 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);
 fk_userid is the unique username the user registers with, it is a string.
Avatar of Erikal

ASKER

The error function is housed in an include file (common.php), which is infact an include filr in accesscontrol.php included on the EngTest1.php (the English test page we are currently working on.
Avatar of Erikal

ASKER

Sorry, here is the complete code for that common.php:

<?php // common.php
function error($msg) {
    ?>
    <html>
    <head>
    <script language="JavaScript">
    <!--
        alert("<?=$msg?>");
        history.back();
    //-->
    </script>
    </head>
    <body>
    </body>
    </html>
    <?
    exit;
}
?>
ok so what the like 12 we are talking about look like? can you post the code from like 12 please
Avatar of Erikal

ASKER

if (!$result) {
        error('A database error occurred in processing your '.
              'submission.\\nIf this error persists, please '.
              'contact eric@lpf-learning.co.uk.');
//echo mysql_error()
    }..........................................................................................This is line 12................................
//HIELO SAYS: you need to "extract" the data first
$row=mysql_fetch_assoc($result);
$EngStatus['eng1comp']=(int)$row['eng1comp'];
if ($EngStatus['eng1comp']==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_Eng1" !=strval($_POST['submit']))
      {
    // Display the English Test form
    ?>
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Erikal

ASKER

That's absolutely fantastic Hielo.
It worked great. I just need to adopt the same code in the other 2 test pages.
It offers the English test form on first entry and after the test is submitted it directs you to the maths test with the alert box.

I don't know how to thank you. You are great!

I also appreciate jamespbyrne's efforts and help.
>>It worked great.
Glad to hear that. Luckily for you, I saw your login page on one of your test page, which cleared everything else. So I expect the answers on the other posts to be dead on.

>>I don't know how to thank you.
"The day will come when I will need a favor from you..."
 Ring a Bell? :)
Avatar of Erikal

ASKER

Here is the link to the question on the session start issue  I encounterd with the mathsTest1.php and VRTest1.phppages:
https://www.experts-exchange.com/questions/23792118/session-start-headers-already-sent-error.html