【问题标题】:Form Submission - Using a function within a PHP class to process the form表单提交 - 使用 PHP 类中的函数来处理表单
【发布时间】:2013-04-22 13:27:21
【问题描述】:

我在一个项目中使用三个文件:index.phpclass.phpstyles.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()divindex.php 文件中被调用,然后我希望它由类中的ProcessRegistrationFunction() 处理。但是,我似乎无法弄清楚如何做到这一点。

【问题讨论】:

  • 嗯,首先是__construct 而不是_construct
  • 你在哪里打电话给DisplayRegistrationForm
  • 我在 index.php 中调用 DisplayRegistrationForm。

标签: php html forms class function


【解决方案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($data) 现在将以数组形式包含所有数据..

您可以对其进行迭代并获取各个值

注意:你的类中有很多语法错误。还有更好的方法来实现这个类。你应该始终避免在你的类中使用 html 代码。

丁丁

【讨论】:

  • 那么我要在操作字段中添加什么吗?
  • 你可以把 index.php 或者留空.. 在这两种情况下它都会提交给 index.php
  • 无论我把它放在哪里,if 语句都会给我一个致命代码:致命错误:不能在写入上下文中使用函数返回值
  • 好的,我发现了致命代码。但现在我无法将它输入我的数据库。
  • 我在stackoverflow.com/questions/16271144/…的一个新问题中更新了这一点
【解决方案2】:

在正确输入的同时避免不必要的错误

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

应该是

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

【讨论】:

  • 这不是答案。它应该在 cmets 或编辑请求中。如果你没有/没有足够的代表,哦,好吧。
猜你喜欢
  • 2012-05-27
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2016-11-10
相关资源
最近更新 更多