How to grab data from post to a class

旧巷老猫 提交于 2020-01-21 05:21:25

问题


If I have a form such as (not all the code just one field and my input):

<div id="login_form">
<form id="registration" method="post" action="results.php">
<label for="first_name"> First Name: </label>
<input type="text" id="first_name" name="first_name" maxlength="100" tabindex="1" />

<input type="submit" id="login_submit" name="submit" value="Submit"/ align="right">

</form>
</div>

Is it possible to grab the data from the form and then output that data to another page. The way I have it now I am just echoing the post from a form to a page:

<?php

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];


echo 'Hello ';
echo "{$first_name} {$last_name}";
echo '<br/>';
echo '<br/>';
echo 'We have your email address as: "';

echo "{$email_address}"; 
echo '"';


 ?> 

Im not sure how to get started but I would like the data from post to go to a class first. So if I create a class called registeredUser how would I grab that data from my form?


回答1:


Hello I think you need some tutorials on OPP first

You tube

  • http://www.youtube.com/watch?gl=NG&feature=related&hl=en-GB&v=_JEEQ-OPVAY

Presentation

  • http://www.slideshare.net/mgirouard/a-gentle-introduction-to-object-oriented-php

Other Links

  • http://buildinternet.com/2009/07/an-introduction-to-object-oriented-php-part-1/

  • http://blog.teamtreehouse.com/getting-started-with-oop-php5

Example of what you want

class RegisterUser {
    private $firstName;
    private $lastName;
    private $emailAddress;

    function __construct() {
        $this->firstName = isset($_POST['first_name']) ? $_POST['first_name'] : null;
        $this->lastName = isset($_POST['last_name']) ? $_POST['last_name'] : null;
        $this->emailAddress = isset($_POST['email_address']) ? $_POST['email_address'] : null;
    }

    function start() {
        if (empty($this->firstName) || empty($this->lastName) || empty($this->emailAddress)) {
            throw new Exception("Empty Post not allowed");
        }

        else
        {
            // Do some stuiff
            echo " Registration Done";
        }
    }
}

$register = new RegisterUser();
if(!empty($_POST))
{
    $register->start();
}


来源:https://stackoverflow.com/questions/12556805/how-to-grab-data-from-post-to-a-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!