douyun8885 2013-04-28 23:33
浏览 41
已采纳

表单提交 - 使用PHP类中的函数来处理表单

I am using three files on a project: index.php, class.php, and styles.css.

All my functions reside within a php class in the class.php file:

<?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()
    {
            }
 }
 ?>

I cannot for the life of me figure out what to put in the action section of the form portion in order to use the ProcessRegistrationFunction() within the class. So basically, the DisplayRegistrationForm() is called in the index.php file in a div, and then I want it to be processed by the ProcessRegistrationFunction() within the class. However, I cannot seem to figure out how to do this.

  • 写回答

2条回答 默认 最新

  • 普通网友 2013-04-28 23:47
    关注

    you can send your form data to index.php itself and than send whatever data to your process function. the best way to implement this will be is to change your form to collect data as an array.

    here myFormArray will store all the information about the form

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

    this value can be accessed in your index.php when you submit the form

    so in your 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);
     }
    

    your ProcessRegistrationFunction($data) will now have all the data in an array form..

    you can iterate over it and fet the individual values

    Note: there are lot of syntax error in your class.. also there is better way to implement this class.. for one you should always avoid html code inside your classs..

    Dins

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳