 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
12-29-2007, 06:29 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Posts: 6
Thanks: 1
|
best way to ..
What is the best way to extract data from a form and have it posted onto say index.php
I don't want the information to be in the address bar, so I don't use get right?
Thanks for helping the noob.
|
|
|
|
12-29-2007, 06:54 AM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Use post, just have the HTML as <form method="POST">, access it in php by $_POST["var"];
|
|
|
|
12-29-2007, 10:02 AM
|
#3 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
HTML Code:
<form action="yourphpscript.php" method="POST">
<input type="text" name="thenameofthisinput" />
</form>
That's the structure. Let's say you have a login form, it would look something like this:
HTML Code:
<form action="login.php" method="POST">
Username: <br />
<input type="text" name="username" /><br />
Password: <br />
<input type="password" name="pass" /><br />
<input type="submit" name="login" value="Login!" />
</form>
You could then access the value of the input they write in those fields by
PHP Code:
$_POST['thenameoftheinput'];
So, in our login example, it would be like this:
php Code:
$username = $_POST['username']; $password = $_POST['pass'];
And then you'll just go off and see if the username and password matches the users in your database ;)
However, there is also a GET method, which is good for profiles.. but usually, you don't use it in forms.. or at least not me.
Hope this helped!
|
|
|
|
|
The Following User Says Thank You to Tanax For This Useful Post:
|
|
12-29-2007, 04:19 PM
|
#4 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
|
PHP Code:
$_POST['thenameoftheinput'];
Yes, correct. You could also use $_REQUEST but I don't recommend the usage since it contains all the data of $_GET, $_POST and $_COOKIE. In my opinion $_REQUEST is useless and it should be removed in PHP 5.3/PHP6.
They are called superglobals because they are accessible everywhere in your code even though they are just 'ordinary' variables which are normally only accessible within one level (Okay, you could use the "global" and the "static" keywords but that conveys to bad code and just makes it more difficult to understand). All superglobals are arrays. Therefore I'd check before accessing them with isset() if the key does really exist even if it might occur absurd. But imagine somebody wants to find out the local filename of your script so they simulate a POST request and leave out some fields. PHP will throw a warning which contains information like the filename where the error occured and the error type.
I'd recommend to use an own error handler in production systems anyway but it's often forgotten.
Quote:
|
However, there is also a GET method, which is good for profiles.. but usually, you don't use it in forms.. or at least not me.
|
You're right. I only use GET parameters when I want to use them in links (e.g. page navigation). It wouldn't make sense to use an article ID in a POST parameter for displaying it. Otherwise this article won't be listed in most of the cases in search engines because they are not 'accessible' for them (In fact they are but they don't send any POST requests, they only follow GET links on websites as far I know). The next disadvantage is that users might want to share this article with others and aren't able to do that because you don't have a handy address which just needs to be copied and pasted into the browser in order to get the article displayed. Of course you could share the POST parameters (in this case the ID) and the full URL to the script. But it's too much work for the user to send a POST request to the server manually. For those reasons I'm only using POST parameters for user input.
Last edited by deflated : 07-18-2010 at 01:32 PM.
|
|
|
|
12-29-2007, 05:05 PM
|
#5 (permalink)
|
|
The Prestige
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
|
Quote:
Originally Posted by deflated
PHP Code:
$_POST['thenameoftheinput'];
Yes, correct. You could also use $_REQUEST but I don't recommend the usage since it contains all the data of $_GET, $_POST and $_COOKIE. In my opinion $_REQUEST is useless and it should be removed in PHP 5.3/PHP6.
They are called superglobals because they are accessible everywhere in your code even though they are just 'ordinary' variables which are normally only accessible within one level (Okay, you could use the "global" and the "static" keywords but that conveys to bad code and just makes it more difficult to understand). All superglobals are arrays. Therefore I'd check before accessing them with isset() if the key does really exist even if it might occur absurd. But imagine somebody wants to find out the local filename of your script so they simulate a POST request and leave out some fields. PHP will throw a warning which contains information like the filename where the error occured and the error type.
I'd recommend to use an own error handler in production systems anyway but it's often forgotten.
You're right. I only use GET parameters when I want to use them in links (e.g. page navigation). It wouldn't make sense to use an article ID in a POST parameter for displaying it. Otherwise this article won't be listed in most of the cases in search engines because they are not 'accessible' for them (In fact they are but they don't send any POST requests, they only follow GET links on websites as far I know). The next disadvantage is that users might want to share this article with others and aren't able to do that because you don't have a handy address which just needs to be copied and pasted into the browser in order to get the article displayed. Of course you could share the POST parameters (in this case the ID) and the full URL to the script. But it's too much work for the user to send a POST request to the server manually. For those reasons I'm only using POST parameters for user input.
Cheers,
Tim
|
Thanks for your input on my post :)
I don't really understand the whole php throw and get for errorhandling... maybe you know a good tutorial, or can write one yourself?
|
|
|
|
12-29-2007, 08:25 PM
|
#6 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: 127.0.0.1
Posts: 19
Thanks: 7
|
Quote:
Originally Posted by Tanax
Thanks for your input on my post :)
|
No problem.
Quote:
|
I don't really understand the whole php throw and get for errorhandling... maybe you know a good tutorial, or can write one yourself?
|
Sorry, for the delayed answer. I wrote a error handling class some time ago. I took one hour to figure out why the destructor wasn't called when I unset()'ted the instance of the class. 
I've commented that part in the code. If anyone has found a bug or has something to annotate please don't hesiate.  I'm really interested in your opinion about that topic. Here's the class:
PHP Code:
<?php
class ErrorHandler { //Todo: we can only use one ErrorHandler in the whole application as $functionStack is static, how do we solve this problem without //the need to make _exceptionHandler and _errorHandler non-static? private static $functionStack = array ( ) ; /** * Constructor * * @param mixed $function */ public function __construct ( $function = null ) { $className = get_class () ; //_exceptionHandler() and _errorHandler() must be static otherwise __destruct() is called in the end //so that we can't use unset() to restore the error- and exception handlers set_exception_handler ( array ( $className , '_exceptionHandler' ) ) ; set_error_handler ( array ( $className , '_errorHandler' ) ) ; if ($function !== null) { self::setErrorHandler ( $function ) ; } } private function __clone () { } /** * Destructor * */ public function __destruct () { restore_error_handler () ; restore_exception_handler () ; self::$functionStack = array ( ) ; } /** * Sets an error ahndler * * @param mixed $function */ public function setErrorHandler ( $function ) { if (is_callable ( $function )) { self::$functionStack [] = $function ; } } /** * Restores the error handler * */ public function restoreErrorHandler () { if (($key = count ( self::$functionStack ) - 1) >= 0) { unset ( self::$functionStack [ $key ] ) ; } } public static function _errorHandler ( $code , $message , $file , $line , $context , $trace = null ) { if (($key = count ( self::$functionStack ) - 1) >= 0) { if ($trace === null) { $trace = array_slice ( debug_backtrace (), 2 ) ; } $args = array ( $code , $message , $file , $line , $context , $trace ) ; call_user_func_array ( self::$functionStack [ $key ], $args ) ; return true ; } return false ; //use PHP's default error handler } public static function _exceptionHandler ( Exception $e ) { return self::_errorHandler ( $e->getCode (), $e->getMessage (), $e->getFile (), $e->getLine (), $GLOBALS, $e->getTrace () ) ; } }
Ok, so this is just a tutorial on how to use the class and will give you a little insight into the world of errors and exceptions. It will only cover the basics so please visit php.net. They have documented that topic pretty good.
PHP Code:
<?php
error_reporting ( E_ALL ) ;
function errorHandler ( $code , $message , $file , $line , $context , $trace ) { echo 'There was an error in line ' . $line . ' ("' . $message . '") - caught by errorHandler()' . PHP_EOL ; }
function errorHandler2 ( $code , $message , $file , $line , $context , $trace ) { echo 'There was an error in line ' . $line . ' ("' . $message . '") - caught by errorHandler2()' . PHP_EOL ; }
//initialize error handler $errorHandler = new ErrorHandler ( 'errorHandler' ) ;
//now the function errorHandler() should be called echo $notExistentVariable ;
//we can even let the error handler catch exceptions but the problem is everything after the exception won't be executed even though we've caught it so I've commented it out //throw new Exception('message');
//an exception is expected for example when a database class throws an exception because the SQL wasn't executed properly we can even prevent the error handler from catching it try { throw new Exception ( 'message' ) ; } catch ( Exception $e ) { echo 'Caught by try..catch' . PHP_EOL ; }
//it's also possible to 'nest' error handlers by adding one to the stack //the class will always use the error handler on top of the stack //so we'll use restoreErrorHandler() and we're using errorHandler() instead of errorHandler2() $errorHandler->setErrorHandler ( 'errorHandler2' ) ; echo $blah ; $errorHandler->restoreErrorHandler () ;
//okay, let's test if that works, now the error should be handled by errorHandler() echo $blah2 ;
//now destroy the error handler unset ( $errorHandler ) ;
//this warning should be handled by PHP's internal error handler echo $test ;
If everything's working fine you should get something like that:
Quote:
tim@tim-laptop:~/Desktop$ php errorHandler.php
There was an error in line 101 ("Undefined variable: notExistentVariable") - caught by errorHandler()
Caught by try..catch
There was an error in line 119 ("Undefined variable: blah") - caught by errorHandler2()
There was an error in line 123 ("Undefined variable: blah2") - caught by errorHandler()
Notice: Undefined variable: test in /home/tim/Desktop/errorHandler.php on line 130
|
Feedback is desired!
Last edited by deflated : 07-18-2010 at 01:39 PM.
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|