Techron Posted November 3, 2008 Share Posted November 3, 2008 Too simple? I hate to ask. I need to retrieve form data from an html form for a class / class object. What is easiest method to do this?? Quote Link to comment https://forums.phpfreaks.com/topic/131144-retrieve-form-data-for-class-object/ Share on other sites More sharing options...
trq Posted November 3, 2008 Share Posted November 3, 2008 Variables posted via a form will be found either within the $_POST or $_GET arrays depending on your forms method. Quote Link to comment https://forums.phpfreaks.com/topic/131144-retrieve-form-data-for-class-object/#findComment-681045 Share on other sites More sharing options...
Adam Posted November 3, 2008 Share Posted November 3, 2008 You can just return them like normal, can't you? try this: test-class.php: <?php class Test { function printVars() { foreach ($_GET as $key => $val) { print 'GET: [' . $key . '] ' . $val . '<br />'; } foreach ($_POST as $key => $val) { print 'POST: [' . $key . '] ' . $val . '<br />'; } } } ?> test.php?some=vars&to=print: <?php include 'test-class.php'; $test = new Test(); $test->printVars(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/131144-retrieve-form-data-for-class-object/#findComment-681046 Share on other sites More sharing options...
JonnoTheDev Posted November 3, 2008 Share Posted November 3, 2008 That code will produce errors. If there are no post values and only url params you will get a foreach error on the foreach($_POST) as not array will exist. Same other way around. As Thorpe said if your form uses a POST method the values will be contained in the $_POST array and in the $_GET array if using the GET method. To display the array after form submission use print_r(); // display submitted input // using POST method print_r($_POST); You can then access the data as you would a normal array. Quote Link to comment https://forums.phpfreaks.com/topic/131144-retrieve-form-data-for-class-object/#findComment-681048 Share on other sites More sharing options...
Adam Posted November 3, 2008 Share Posted November 3, 2008 ahh okay, testing at work with errors disabled, didn't think about that. Well to be hoenst wasn't really supposed to be anything more than a simple demonstration that you can still access $_GET and $_POST in the same way.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/131144-retrieve-form-data-for-class-object/#findComment-681051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.