简体   繁体   English

从HTML表单中的类中调用PHP函数

[英]Call PHP function in a class from a HTML form

I have a class which is maintain guest information. 我有一堂课是维护客人信息。 So in that class I have following method to insert guest data into a guest_info table 因此,在该类中,我有以下方法将访客数据插入guest_info表中

require_once "BaseModel.php";

class GuestModel extends BaseModel
{
    public static $table = "guest_info";

    public static function guestInfoFormData(){
         $title = $_POST['title'];
         $firstname = $_POST['firstname'];
         $lastname = $_POST['lastname'];
         $nicpassport = $_POST['nicpassport'];
         $contactnumber = $_POST['contactnumber'];
         $addressline1 = $_POST['addressline1'];
         $addressline2 = $_POST['addressline2'];
         $addressline3 = $_POST['addressline3'];
         $country = $_POST['country'];
         $guestinfo = array($title,$firstname,$firstname,$nicpassport,$addressline1,$addressline2,$addressline3,$country);
         insertInto($guestinfo);
    }
}

How can I call this method from my HTML form and pass the guest data to it? 如何从HTML表单中调用此方法并将访客数据传递给它?

<?php GuestModel::guestInfoFormData(); ?>

确保在GuestModel之前需要/包括GuestModel类。

You can use GuestModel::guestInfoFormData() , but you can't do it in a html file, it has to be a php file. 您可以使用GuestModel::guestInfoFormData() ,但不能在html文件中进行操作,它必须是php文件。

Don't forget to escape all input values, now you pass all user input to insertInto , so you have to escape the data before using them in a sql query to prevent sql injections. 别忘了转义所有输入值,现在您将所有用户输入传递给insertInto ,因此在sql查询中使用它们以防止sql注入之前,必须转义数据。

Btw: Why you create a class, when you call a global function from it? 顺便说一句:当您从类中调用全局函数时,为什么要创建一个类呢? Maybe it's a good idea to move the global function insertInto into this (or another, better) class :) 将全局函数insertInto移到此类(或另一个更好的类)中也许是个好主意:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM