Creating forms in php

Hi,

I’m a newbie with php and I am having a problem creating a form and passing the input to another php page. The below code will generate the form and for form action if I use echo “<form action=’” . $_SERVER[‘PHP_SELF’] . “’>”; to test it by just looping it on itself works fine but when i try to get to pass the $_GET to page2.php I get 404 errors even seen a 401 forbidden. Can some one help me out and point out what I’m missing in my form action?

[php]

<?php function drawForm($value, $pwd, $files){ echo ""; echo " username : "; echo "
"; echo "Password: "; echo "
"; echo "
"; if(is_dir("courses")) { $fileNames = scandir("courses"); foreach($fileNames as $fileName) { if($fileName != "index.txt" && $fileName != "." && $fileName != "..") { echo "$fileName
"; } } } echo ""; echo ""; } drawForm($value, $pwd, $file); <? [/php] Thanks! Scriber

First of all, let me recommend a quick clean up for your function code, that might make it a little easier to see what’s going on with your code:

[php]
function drawForm($value, $pwd, $files){
$form = <<<DOC

Username:

Password:


DOC;
$fileNames = scandir(“courses”);
foreach($fileNames as $fileName){
if($fileName != “index.txt” && $fileName != ‘.’ $fileName != ‘…’){
$form .= “$fileName”;
}
$form .= “
”;
}

echo $form;
}
[/php]

None of the above actually addresses your question except to say that this is a little cleaner than having a million echo statements in a function body, but that’s really beside the point. Your specific issue is likely related either to your page2.php file not being in the same directory as the php file that contains your form, (because the action attribute wants the location of the file, not just it’s name…if page2.php is in a different directory, you need to give it the appropriate path), or potentially to the permissions on page2.php not being set correctly. Make sure that your script is at least world-readable, which it honestly should be by default.

Hey stlewis

Thanks for cleaning up the form hadn’t seen the <<<DOC DOC; tag before haven’t been able to see much documented about it either. Only way I’ve read or been show to do HTML and php was with echo or print.

Also it might be clean but now it doesn’t even generate a form 8(

Should point out on mine to get it to work I had to escape out the “” i.e.
“”;

Any help would be greatly appreciated. I find this php stuff so frustrating yet so satisfying when I actually get it to work.

Scriber,
First, do you really need to create the form in PHP?: Why don’t you just create a form?
Normally a form is created in HTML and it calls the PHP file. The PHP file processes the inputs
and ECHO’s back the results.
If you really need to build a form from inside PHP it is a bit more complicated. One trick that I use most every day is to let the PHP script run and produce the page. (in other words load it into a browser.) Then, RIGHT-CLICK on it and select VIEW-SOURCE. This will show you what your code is really outputting. This way, you can see where a quote is missing or a page is mistyped. Sometimes you can not see that by reading the source, you need to view the “rendered” output to the browser.

If not needed to be all in PHP, here is the standard way to create your form from the original post:
[php]

username :
Password:

<? if(is_dir("courses")) { $fileNames = scandir("courses"); foreach($fileNames as $fileName) { if($fileName != "index.txt" && $fileName != "." && $fileName != "..") { echo "$fileName
"; } } } } ?> [/php] NOTE: the variables you sent to the function were created outside the page, so I am not sure how this function was originally called. (where did the values come from.) So, you may have to add the part where they came from above my sample. If that made sense. Good luck...

Why do you need to waste the space with another page? just use if(isset()) to see if the submit button has been pressed :slight_smile:

Well, PHP is SERVER-SIDE and HTML and FORMS are CLIENT-SIDE, so it is much easier to debug if you do it the way it is designed. Doing it in one page changes the way the page works. Posting to yourself will eventually page out the browser and fill memory, unless you clear the buffers on each call of yourself.

But, I didn’t say it had to be done that way, I was asking why you wanted it done that way. I doubt you are making an entire website out of one page that only calls itself. So, back to your problem, look at the source that is being create by your page and see where the REAL error is. Then we can help.

Thanks for the help. What I was trying to do was populate the form pick list with a list of text files in a folder called courses. Could think of how to do that in HTML. Will try and figure this out tonight

Thanks,

Well, since the folder is server-side, you need to use server-side code. This would be PHP or Javascipt-server-side. I like to do this with PHP. PHP would pull the folder list and populate the HTML for you. Then, the output of that is pushed into the browser which means that the PHP code disappears and you end up with just HTML or whatever client-side code you put on your page. ??? Did that make sense? Sometimes I type while thinking… LOL

Well, the code I sent to you before is basically how you do it. You may have to change how it selects the folder. I created that code from your previous code, so it is not tested. Try it and let us know if it works. If not we can help getting it solved for you…

Thank you Ernie Alex and all others that responded I got it working with this bit of HTML/PHP code. hadn’t thought of putting the outside of the php. Live and learn. This was my first post on this forum and I am very impressed with the responses Thanks again!

[php]

<?php ob_start(); function drawForm($value, $pwd, $files){ echo " username : "; echo "
"; echo "Password: "; echo "
"; echo "
"; if(is_dir("courses")) { $fileNames = scandir("courses");
    foreach($fileNames as $fileName)
    {
        if($fileName != "index.txt" &&
           $fileName != "."         &&
           $fileName != "..")
        {
            echo "<option value =\"$fileName\">$fileName</option><br/>";
        }
    }

}
echo “”;

echo "</form>";

}

drawForm($value, $pwd, $file);
ob_end_flush();
?>

[/php]

I don’t want to sound patronising or stupid when I ask, have you created page2.php?
You could be sending the form to a page that doesn’t exist (404).

Leebut is correct.

But, also, if all of your code is on one page and you do not need to go to another page, you can have the action on the form go nowhere. This works if all the code is on one page. ( Action="" ) Of course, I do not suggest to place all the code on one page. Makes it harder to debug.

Good to hear you got it working. We will mark this SOLVED. If you have further questions, please open a new post… See you in the bitstream…

Sponsor our Newsletter | Privacy Policy | Terms of Service