Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect logic

Status
Not open for further replies.

mcquam

Technical User
Feb 26, 2007
89
0
0
GB
I need to redirect users depending on their response to form input questions (it is a questionnaire and respondents don't have to answer all questions). What is the best way to do it? I have tried a redirect like this:
Code:
<?php if ($q1 == "1") Header("Location: index.php?p=q01_02"); ?>
Where the GET array contains the next question page.
Is there an easier way than this?
 
You could always dynamically load or otherwise include the file that has the next set of questions into the the processing script.

But without knowing where the question are coming from its hard to say for sure what would be easier.

Perhaps the redirect is a bit much if index.php is doing all the question loading.

Maybe just submitting the form to it and checking it there would be enough.





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I'm not sure I understand how it would work to dynamically load the file. Do you mean selecting the next page and then using POST?

So the logic would be in the form? So where I have for example:
Code:
<form  action="index.php?p=q01_02" method="POST" />
I would add my 'if $q1=="1"' or whatever?
 
Assuming index.php does everything, you can just submit the form to it instead of redirecting.

For instance:

Code:
<form action="index.php" method=POST>
<input type="text" name="question">
<input type="send" value="Load next question">
</form>

if(isset($_POST['send'])){
if($_POST['question']==1){
include('questions01_02.php');
}
if($_POST['question']==2){
include('questions03_04.php');
}
...

}

Assuming your questions are in files or you can connect to the DB and load the appropriate questions etc...



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Yes, my questions are one per file. Index.php has this switch:
Code:
switch ($p) {

	case 'q01_01':
		$page = 'questions/q01_01.inc.php';
		$page_title = 'Question 1.1';
		break;

	case 'q01_02':
		$page = 'questions/q01_02.inc.php';
		$page_title = 'Question 1.2';
		break;

I added your code within my existing page submit conditional like this:

Code:
    if($q1==1){
    include('index.php?p=q01_02');
    }
    if($q1==2){
    include('index.php?p=q01_03');
    }
I tried what you suggest but am getting "failed to open stream" errors for the include but that is no doubt my fault with the uri?
 
You can't include index.php into itself. You want to include the question files directly instead.

I'm assuming there's more code after the switch to pull in the files.

What I'm saying is to skip the step of redirecting, and instead include the files directly.


Basically what you are doing is going around in circles 3 times before loading the page.

You get the value from the questions then convert that number to a code, send it onto index.php have it look at the code and decide what file to load and then maybe redirect again or load the file or whatever instead of taking the value directly from the question and loading the file.

In other words loose the redirect which is not really needed if you are already at index.php and just go straight to loading the file.

Code:
switch ($q1) {

    case '1':
        $page = 'questions/q01_01.inc.php';
        break;

    case '2':
        $page = 'questions/q01_02.inc.php';
        break;
}

    include($page);


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Sorry for the delay and thanks again for your help Vacunita.

I have been trying to figure out how your solution would work.

I need a switch option for each question, which I have already. Do I need a further switch for possible answers within each question?

 
vacunita said:
You can't include index.php into itself

you can, but it may not make sense to do so. if the OP wants the contents of the page from the webserver he could just prepend to the index.php page.

but it definitely is not the right method.
 
Wouldn't that create an endless loop?

If you include index.php into itself, then the included index.php will also include index.php into itself, and that will include another index.php into itself etc..., "ad infinitum".

I'm sure after a while of inclusions something is bound to happen. Maybe a fatal memory leak in the server leading to a crash?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi Phil

i wasn't commenting on whether the result would work or whether it would disappear up its own backside. merely that it is technically possible to include the 'output' of a page via http.

but in this case, no I don't think it would loop ad infinitum as include() via http would include the html output of the page (i.e. what a browser would get) rather than the code itself.

for example
Code:
echo include('[URL unfurl="true"]http://www.google.co.uk');[/URL]
would create a rather crude proxy for the google front page.
 
Nor was I, I was just pointing something out.

BTW, I just tested this, and even though the include returns the output of the included script, it still has to run it. So yes in fact it keeps running forever.

Try this script for instance:

It ran 1325 times before I stopped it.

Code:
<?PHP
if(!isset($var)){
$var=0;
}
else{
$var++;
}
echo "This is the $var time this script runs";
include("myloop.php");
?>

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
OK I'm happy that that is not the best way but maybe you can help me with the logic.

So I have a switch for each question on index.php. Now I need to select that on the basis of a posted field. Do I use the form action along the lines of:
Code:
<form  action="index.php?p=<?php if ($q1==1) {
        echo 'q01_02';} ?>" method="POST" />

Or do I do something like:
Code:
 if ($q1==1) {
        $nextq = 'q01_02';
    } elseif ($q1==2) {
        $nextq = 'q01_03';
    } else {
        $nextq = 'q01_10';
    }

<form  action="index.php?p=<?php echo $nextq; ?>" method="POST" />

I tried the second option and can't get it to recognise the variable.
 
@vacunita

you're right. i was looking at it from the original calling script's perspective (which will run only once). however it will spawn multiple other scripts unless the conditional logic is set not to do so.
 
@mcquam no need for the extra parameters on the form if you are already posting the form and have access to the field being posted.


The logic would be to use the value you get from your original from, (the one that has the question you get the first value from) and use that directly, instead of adding a second redirection.

So instead of doing this:
Code:
<?php if ($q1 == "1") Header("Location: index.php?p=q01_02"); ?>

Just get the value directly and proceed to include the file you need.

Code:
<?PHP
 if ($q1==1) {
        $nextq = 'q01_02';
    } elseif ($q1==2) {
        $nextq = 'q01_03';
    } else {
        $nextq = 'q01_10';
    }

include("questions/[red]$nextq[/red].inc.php");

Just skip all the redirection and include it after you've determined what needs to be included.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
@vacunita So what you are suggesting is just to get rid of the index.php switch. I suppose the only benefit from the switch really is to avoid duplicating the header and footer includes every time and maybe a bit of security but maybe that's not such a big deal in this case.

Thanks for your help.
 
No the switch or the set of if's are fine, either way produces the same immediate result. Its just the extra redirection that is not necessary. You seem to be doing everything from index.php so why do you need to redirect again to it?


Using your if's or using the switch the end result is the same, but instead of redirecting again to index.php with a new value in the GET array, just load the adequate page based what you got from your if's or switch at that point.




----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I have a registration site where each user must acknowledge a series of rules to proceed. The process I used could be used for a question / answer setup.

Use 2 tables:
Questions: a key and the question text
UserAnswers: A key, question key, user reply.

When the user hits the page the program checks the user answers table for the max question key for that user. It then loads the next question. If their is no next question the user is directed to the next page set.

If their is a question display the question text and prompt for their answer. Save their answer and recall the question page. In effect they will loop through un til they have answered all of the questions.
 
Thanks for your suggestion alan.

I have tried this out as a test and it comes back to the same problem: I need to specify some logic somewhere that specifies the next page. Unfortunately my page progression could be 1,2,4,17,18 etc rather than 1,2,3. I can use the database record for the last answer though.

It looks like I need a separate page just to look up where to go next.

So maybe I need to update my switch page to include 2 levels of logic; one for the referring page and one for the answer.
 
The way I did it all the questions would be one PHP page. The only other page is the page after the questions.

If the questions are not sequential add an order column or a table with custom display orders. You just keep hitting the "next" one till your done. Then you goto the done page.


Basically

questions.php goes to
questions.php goes to
questions.php goes to
questions.php goes to
questions.php goes to
questions.php goes to
done.php

The questions page just looks up the user ID finds the last question in the order and displays the next one. The submit takes you back to questions which saves the answer and then repeats the process. This continues till their out of questions.
 
That's a great idea. I never thought of that,

I've started testing it out and I've just pasted a whole form into each question field which I've never done before but it seems to work fine.

I'm now faced with the problem of how to get a variable field name. My responses table has a row for each respondent id and a question in each row. Currently the rows are named q01_01_01, q01_01_02, q01_01_03, q01_02_01, q01_02_02, etc. as some questions are checkboxes or text with more than 1 answer. I have been trying to get my head around that.

Many thanks though for pointing me in the right direction.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top