Jump to content

Define variable names by file name


xn1

Recommended Posts

As you can see here I have the following code

 

<?php
session_start();

session_register('hazard2');

//INITIALIZE ERROR VARIABLES
$errorFound = false;
$errorIcon['hazard2'] = '';

if (isset($_POST['submitbutton'])) { 

//SET SESSION VARIABLES
$_SESSION['hazard2'] = $_POST['hazard2'];

     //TEST FORM INFORMATION
     if(count($_POST['hazard2']) != 3) {
        $errorFound = true;
$errorIcon['hazard2'] = 'Error Question 2';
     }
    
     //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING

     if (!$errorFound) {
header( "Location: hazardresult.php" );
		}

     }

?>

 

It's lazy I know, but the script is on a file called hazard2.php, is there anyway to have all of the hazard2's in the code come from the file name. So when I make hazard3, I don't have to change all the hazard2's to hazard3's

 

Thank you

Link to comment
Share on other sites

As you can see here I have the following code

 

<?php
session_start();

session_register('hazard2');

//INITIALIZE ERROR VARIABLES
$errorFound = false;
$errorIcon['hazard2'] = '';

if (isset($_POST['submitbutton'])) { 

//SET SESSION VARIABLES
$_SESSION['hazard2'] = $_POST['hazard2'];

     //TEST FORM INFORMATION
     if(count($_POST['hazard2']) != 3) {
        $errorFound = true;
$errorIcon['hazard2'] = 'Error Question 2';
     }
    
     //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING

     if (!$errorFound) {
header( "Location: hazardresult.php" );
		}

     }

?>

 

It's lazy I know, but the script is on a file called hazard2.php, is there anyway to have all of the hazard2's in the code come from the file name. So when I make hazard3, I don't have to change all the hazard2's to hazard3's

 

Thank you

as Ken stated, yourdoingitwrong.jpg

 

but you could do it like this! :)

 

<?php
session_start();

$file = $_GET['file'];
session_register($file);

//INITIALIZE ERROR VARIABLES
$errorFound = false;
$errorIcon[$file] = '';

if (isset($_POST['submitbutton'])) { 

//SET SESSION VARIABLES
$_SESSION[$file] = $_POST[$file];

     //TEST FORM INFORMATION
     if(count($_POST[$file]) != 3) {
        $errorFound = true;
$errorIcon[$file] = 'Error Question 2';
     }
    
     //IF NO ERRORS WERE FOUND, CONTINUE PROCESSING

     if (!$errorFound) {
header( "Location: hazardresult.php" );
		}

     }

?>

 

and then instead of

 

http://xyz.com/hazard2.php

 

do:

 

http://xyz.com/hazard.php?file=hazard2

Link to comment
Share on other sites

First question, what am I doing wrong?

 

The aim of the functions is to, test the answer to the question on the page, to make sure an option is selected, if selected move on to the next while storing the answer into a session. What's the easier way?

 

Second question, can you link me a guide to the ?file=hazard2 way of doing things.

 

And thank you for the file name help.

Link to comment
Share on other sites

I gave you code that makes it work, it SHOULD work, but its untested :)

 

as far as the sessions, yes its a good idea to store the answers to the questions in a session, makes it alot easier obviously, HOWEVER, you don't need a new session PER answer, store it all in an array, in the user's default session :)

Link to comment
Share on other sites

Had to change it slightly to remove the .php but it did work perfectly thank you.

 

So I'd only need session start in the first page? And would I only need to define the array on the first page too? On the results page I plan on explaining all of the answers that were wrong by checking them all. Would I be able to do this with the array?

Link to comment
Share on other sites

yes, on every page that you are wanting to ACCESS/MODIFY the session variable you will call session_start, it will then load whatever session that the user's session id represents!

 

You only need to define something once in a session variable, after that it will exist in any further interactions with that particular session, unless ofcourse you unset that particular variable :)

 

so for example..

 

session_start();
$file = $_GET['file'];
if (!isset($_SESSION['answers'])) $_SESSION['answers'] = array();
$_SESSION['answers'][$file] = $_POST['answer'];

Link to comment
Share on other sites

Ok, what I've done then is create a starting page where I've set a session array called answers and a session variable to count the number of questions aswered (this will be so that eventually I can keep loading random questions until i hit a target then go to a results page) called qcount.

 

Am I doing it right now? This is after you click on start test in the start page. (This is question1.php)

 

<?php
session_start();

$errorFound = false;
$errorIcon['question1'] = '';

if(isset($_SESSION['qcounter'])){

if(isset($_POST['submitbutton'])){

	if($_POST['question1'] = ''){
	$errorFound = true;
	$errorIcon['question1'] = 'Please Select An Answer';
	}

	if(!$errorFound){
	$_SESSION['answers'][] = $_POST['question1'];
	$_SESSION['qcounter'] = $_SESSION['qcounter']+1;
	header ("Location: question2.php");
	}
}

} else {
header ("Location: teststart.php");}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.