Richard, that is exactly what I was looking for. thanks alot
Paul, I didn't know its possible to use error_reporting(), that's a good hint thanks. btw if you guys know any simple php application that I can study and learn faster than going through books please let me know. I tried Wordpress but its too complicated for a beginner like me.

"Paul M Foster" <pa...@quillandmouse.com> wrote in message news:20090728043523.gs14...@quillandmouse.com...
On Mon, Jul 27, 2009 at 09:01:16PM -0700, Richard S. Crawford wrote:

>> <form action="pass.php" method=POST>
>> username : <input type=text name=user ><br />
>> password : <input type=password name=pass> <br />
>> <input type=submit value="go"><p>
>> </form>
>> <?php
>> $user=$_POST['user'];
>> $pass=$_POST['pass'];
>> if(($user=="myname")&&($pass="mypass"))
>> echo "access granted";
>> else
>> echo "access denied";
>> ?>
>> getting "Notice: Undefined index: user" and "Notice: Undefined index: >> pass". >> changing form action to another page will solve the problem but i want >> to be
>> able to use $_POST array on the same page, how can i do it?
>> thanks in advance
>> /Arash


It's hard to respond when it's unclear exactly what you want to do.
I'm guessing that you want to simply have the same page show both the
form, and also process the form, so that you can cut down on the
number of pages you have to create.

The way I do this is by first checking to see if the $_POST array has
been set; if it has not been set, then I know that the form wasn't
filled out, and so the script needs to print out the form. If the
array has been set, on the other hand, then the script can process the
form data. Here's a real quick example of what I mean:

if (!isset($_POST)) { ?> // The $_POST array is not set, so display the form
<form action="pass.php" method=POST>
username : <input type="text" name="user" ><br />
password : <input type="password" name="pass"> <br />
<input type="submit" value="go"><p>
<?php } else { // The $_POST array *is* set, so process the data
if(($user=="myname")&&($pass="mypass")) {
echo "access granted";
} else {
echo "access denied";

Hope that helps.

Richard's right. This is the best way to do this.

Your original code had two problems. First, you're getting the error
message because you're allowing E_NOTICE level error messages. You can
turn these off with the error_reporting() function.

Second, as written, your page paints the form and then directly tests
the status of the variables before the user can respond; the whole page
of PHP code executes and then waits for user response. It's only when
the user responds and the page is revisited that the variables can
legitimately be tested. That's why Richard's method works. The
variables are only tested when the page is re-entered after the user


Paul M. Foster

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to