I'm finding some information here and there about $PHP_SELF but never specific to the question I have. I'm not sure if I'm going about this right, but I'm using a form for user input, specifically a registration page - and I'd rather only have one page the user has to fill out instead of 2 or more

- so -

Within that form is another form, which is a dropdown box that consists of 2 choices. What I'm trying to do is depending on the answer of the dropdown box, have the page display a set of questions specific to an answer that is given within the dropdown box values, but I'm trying to have this new set of questions display on the same page, possibly without refreshing it, so that the information the user has already typed in the first form (that the dropdown form is embedded inside of) is not lost.

I know it'd probably be easier with javascript, or at least it seems like javascript's style, or to do this on 2 seperate pages, but I'm trying to get php down and I'm fairly convinced there's a way to do this on one page.

when I use $PHP_SELF and select a value in the dropdown box, it doesn't even refresh to the same page, the page just kinda sits there. Can anyone show me what I'm doing wrong, or point me in the right direction?

<html>
<form method="post" action="processRegistration.php">
//several tables with text fields for user to enter information in
<form method="post" action="<?php echo $PHP_SELF;?>">
//dropdown box w/yes and no for options
</form>

<?php
$problem = $_POST['problem'];
echo $statement;
switch($statement)
 case "Yes":
 //echo question set for yes
 break;
 case "No":
 //echo question set for no
 break;
 default:
 //echo unforseen error message
 break;

?>

<input type="submit" value="Submit Registration/>
//closing stuff </form></html> etc

I know most of that was html but the part I think is the problem is being written in php - and this is for a site mainly written in php and mysql, this just happens to be an html form (because I try to seperate one language from another) used in this site - specifically being called by a .php page, and I've never attempted to make a form quite like this one before - usually just straightforward fill it out n click stuff but never on the same page based off another dropdown menu.

any help is much appreciated - thanx!

Recommended Answers

All 4 Replies

php run serverside,
it has to refresh the page to display differently,
Javascript runs clientside php makes no changes once the page is sent, AJAX Javascript

PHP_SELF is a pre-defined variable that you can define as:

$self = $_SERVER;

To make it work strictly with PHP, define a form with the action being to $self (after defining it as above). You can define a form with just the drop-down for the first pass and if it is set then display a different form based on what they chose. The screen will refresh and you will display the second form. That can have an action of $self as well but obviously you need to test the values that you receive to know what form they submitted. You may want to use a consistent hidden variable for that.

That's the theory. If you have a reasonable understanding of PHP and HTML you should be able to code that up in a flash. Another thing to note is that you need braces for the switch statement.

Chris

ah thanks for the help.
lack of braces was a typo - I was just kinda running thru the code quickly when I typed it out lol

refresh isnt a big deal, I just wasnt sure if I could pull it off in php or not, but being serverside it makes sense that u would have to refresh. I didn't really take that into consideration.

thank you for the help tho, I apprecaite it :)

I wouldn't use $_SERVER. It has an XSS vulnerability.

If someone made the URL:

index.php#<script>alert(document.cookie)</script>

PHP would display it as such. Anyone can choose a malicious URL that will become part of your form.

If you have a form and don't specify an "action" url, it defaults to the same page, so it is redundant to give it the same page using $_SERVER;

You can also just use action="?" which is the same thing as the same page, without any previous parameters in the URL.

eg:

index.php?this=that

becomes:

index.php?
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.