表单提交 - 使用PHP类中的一个函数来处理表单(Form Submission - Using a

2019-10-18 02:32发布

我使用的一个项目三个文件: index.phpclass.php ,和styles.css

我的所有功能驻留在class.php文件中的PHP类中:

<?php

class Club
{
    //Private Data
    private $HostName;      //typically "localhost"
    private $UserId;
    private $Password;
    private $DBName;
    private $Con;           //MySQL Connection
    //Public Methods
    //Constructor
    public function __construct($host = NULL, $uid = NULL, $pw = NULL, $db = NULL)
    {
        $this->HostName = $host;
        $this->UserID = $uid;
        $this->Password = $pw;
        $this->DBName = $db;

        //Connect to Database
        $this -> Con = mysqli_connect($host, $uid, $pw, $db);
        if(mysgli_connect_errno($this -> Con))
        {
            echo "Failed to connect to MySQL; " . mysqli_connect_error();
        }

    }
    //Destructor
    public function __destruct()
    {
        //Close connection
        mysqli_close($this -> Con);
    }
    public function DisplayMembers()
    {
            }
            function DisplayAbout()
    {
            }
            function DisplayRegistrationForm()
    {
        echo("<h2>Become a Club Member</h2>

        <form name='register' method='post' action=''>
            <table>
                <tbody>
                    <tr>
                        <td width='80px'>
                            <label>First Name: </label>
                        </td>
                        <td width='300'>
                            <input id='firstname' type='text' name='firstname' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Last Name: </label>
                        </td>
                        <td>
                            <input id='lastname' type='text' name='lastname' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Your Email: </label>
                        </td>
                        <td>
                            <input id='email' type='text' name='email' value='' required/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Gender: </label>
                        </td>
                        <td>
                            <input id='gender' type='radio' name='gender' value='male'>Male<br />
                            <input id='gender' type='radio' name='gender' value='female'>Female
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label>Interested in: </label>
                        </td>
                        <td id='check'>
                            <span style='font-weight: bold;'>Check All that Apply:</span><br />
                            <input id='interests' type='checkbox' name='interests[]' value='Pizza Party'>Pizza Party<br />
                            <input id='interests' type='checkbox' name='interests[]' value='Joining Study Groups'>Joining Study Groups<br />
                            <input id='interests' type='checkbox' name='interests[]' value='Visiting Employer Sites'>Visiting Employer Sites<br />
                            <input id='interests' type='checkbox' name='interests[]' value='Participating in Programming Competition'>Participating in Programming Competitions<br />
                            <input id='interests' type='checkbox' name='interests[]' value='Building Games'>Building Games<br />
                            <input id='interests' type='checkbox' name='interests[]' value='Becoming an Officer of the Club'>Becoming an Officer of the Club
                        </td>
                    </tr>
                    <tr>
                        <td colspan='2' style='text-align: center;'>
                            <input id='submit' type='submit' name='submit' value='Sign Up'/>
                        </td>
                    </tr>
                </tbody>
            </table>    
        </form>");
    }
            function ProcessRegistrationForm()
    {
            }
            private function Get_Members_From_DB()
    {
            }
            private function Get_Members_Interests_From_DB($MemberEmail)
    {
            }
            private function Get_Interests_Types_From_DB()
    {
            }
 }
 ?>

我不能为我的生活弄清楚放什么形式部分的动作部分,以使用ProcessRegistrationFunction()在类中。 所以基本上, DisplayRegistrationForm()被调用的index.php文件中的div ,然后我希望它被处理ProcessRegistrationFunction()在类中。 不过,我似乎无法弄清楚如何做到这一点。

Answer 1:

您可以把您的表单数据的index.php本身比任何数据发送到您的处理功能。 实施这将是最好的办法是改变你的表格来收集数据作为数组。

这里myFormArray将存储所有关于形式的信息

<input id='firstname' type='text' name='myFormArray[firstname]' value='' required/>

<input id='lastname' type='text' name='myFormArray[lastname]' value='' required/>

这个值可以在你的index.php当您提交表单进行访问

所以在你的index.php

 if(isset($_POST["submit"]){
    $formData = $_POST["myFormArray"]; // dont forget to sanitize any post data
   //than you can call your class function and pass this data
   $class = new class();
   $class->ProcessRegistrationFunction($formData);
 }

您ProcessRegistrationFunction($数据)现在将有阵列形式的所有数据..

你可以遍历它和FET的各个值

注:有很多语法错误在你的类..也有实现这个类的更好的方法。对于一个你应该总是避免你的班组长里的html代码..



Answer 2:

同时打字输入正确,这样你们能避免不必要的错误

 'if(mysgli_connect_errno($this -> Con))'

它应该是

 'if(mysqli_connect_errno($this -> Con))'


文章来源: Form Submission - Using a function within a PHP class to process the form