7

我需要一点帮助来了解如何在 PHP 中使用 OOP 来执行表单提交操作。手头的任务...我正在尝试学习如何使用 OOP 编写 PHP 代码。到目前为止,我了解类、函数、调用函数、继承等的一般概念。

我创建了一个简单的练习项目,允许用户在某个位置搜索餐点。到目前为止,我有一个包含 2 个<input>字段的表单。通常对于表单操作,我会这样做,<form action="actionFileName.php">但现在我有一个具有处理表单功能的类,我将什么用于操作值?

我想创建一个类的实例并调用处理表单的函数,但我得到一个找不到对象!在我提交表单后的页面,其中包含在地址栏中显示的语句中的echo值。elsehungryClass.php

我该如何解决?谢谢。

我的代码是什么样的:HTML 表单

<?php  require_once 'hungryClass.php';
  $newSearch = new hungryClass();
?>
<form action="<?php $newSearch->searchMeal();?>" method="post" id="searchMealForm">
  <input type="search" size="35" placeholder="What Food Are you looking for?" id="mealName" class="meal"/>
  <input type="search" placeholder="City Area" id="mealLocation" class="meal">
  <input type="submit" value="Satisfy Me" id="findMeal" />
</form>

处理表单的页面 (hungryClass.php)

<?php 
  require_once('dbConnect.php');
  class hungryClass{

       public function searchMeal(){
         //call connection function.
          $connect = new dbConnect();

         //validate input
         if(isset($_POST['mealName'])){
             $meal = $_POST['mealName'];

           //ensure value is a string.
           $cleanse_meal = filter_var($meal, FILTER_SANITIZE_STRING);
           echo $cleanse_meal;
        }
        else{
       echo "Please supply the meal you crave";
        }

      //validate location
       if(isset($_POST['mealLocation'])){
     $location = $_POST['mealLocation'];

         //validate and sanitize input. ensure value is a string.
    $cleanse_location = filter_var($location, FILTER_SANITIZE_STRING);
        echo $cleanse_location;

       }
       else{
     echo "Please supply a location";
       }

}

数据库类

<?php

class dbConnect{
private $host = "localhost";
private $user = "stacey";
private $pass = "";
private $db_name = "menu_finder";

private $connect;
//private static $dbInstance;

public function __construct(){
    try{
        $this->connect = new mysqli($host, $user, $pass, $db_name);
        if(mysqli_connect_error()){
           die('connection error('.mysqli_connect_errno().')' . mysqli_connect_error());
        }
    }

    catch(Exception $e){
        echo $e->getMessage();
    }
}

?>

4

4 回答 4

5

表单的 action 属性是您要提交到的脚本名称。您想将表单提交给饥饿类以进行处理,但这在您实例化饥饿类之前无法完成。您将需要使用脚本名称作为表单中的操作值。假设您要提交到 temp.php,您的表单应如下所示

<form action="temp.php" method="post" id="searchMealForm">
  <input type="search" size="35" placeholder="What Food Are you looking for?" id="mealName" class="meal"/>
  <input type="search" placeholder="City Area" id="mealLocation" class="meal">
  <input type="submit" value="Satisfy Me" id="findMeal" />
</form>

然后当这个表单提交时,它将被发送到 temp.php。为了让你饥饿的类处理这个表单,你需要在 temp.php 中创建它的一个实例,并使用这个实例调用 searchMeal。temp.php 应该看起来像这样

<?php
  require_once 'hungryClass.php';
  $newSearch = new hungryClass();
  $newSearch->searchMeal();
?>

或将所有内容放在一个文件中

<?php
require_once 'hungryClass.php';

if($_SERVER['REQUEST_METHOD'] == 'POST') {
  $newSearch = new hungryClass();
  $newSearch->searchMeal();
  exit();
}
?>
<form action="<? echo $_SERVER['PHP_SELF']?>" method="post" id="searchMealForm">
  <input type="search" size="35" placeholder="What Food Are you looking for?" id="mealName" class="meal"/>
  <input type="search" placeholder="City Area" id="mealLocation" class="meal">
  <input type="submit" value="Satisfy Me" id="findMeal" />
</form>
于 2012-07-20T05:59:54.967 回答
2

您应该将表单提交到将处理和处理表单的 php 文件。

<form action="<?php $newSearch->searchMeal();?>" method="post" id="searchMealForm">

应该是这样的:

<form action="formaction.php" method="post" id="searchMealForm">

在 formaction.php 中你可以调用你的方法,当然你需要包含所需的文件:

<?php
$newSearch->searchMeal();

希望这可以帮助。

于 2012-07-20T06:02:27.680 回答
1

这是php中最简单的OOP代码,您可以通过以下方式尝试

表单.php

<?php
  require_once 'DbClass.php';
?>
<form action="formaction.php" method="post" id="searchMealForm">
  <input type="search" size="35" name='search1' placeholder="What Food Are you looking for?" id="mealName" class="meal"/>
  <input type="search" placeholder="City Area" name='search2' id="mealLocation" class="meal">
  <input type="submit" value="Satisfy Me" id="findMeal" name='butsearch' />
</form>

formaction.php

<?php
 require_once 'DbClass.php';
 $obj = new DbClass();
 if(isset($_REQUEST['butsearch']))
 {
    $ser = $_REQUEST['search1'];
    $ser2 = $_REQUEST['search2'];
    $inf0 = array('ser1'=>$ser,'ser2'=>$ser2)
    $obj->search($info);
 }
?>

数据库类.php

<?php
//if any file needs to be included, include here
 class Dboper
 {
   public function __construct() {
     //DB Connection Code here
   }
   function serach($params)
   {
     $ser1 = $params['ser1'];
     $ser2 = $params['ser2'];
     //write query to search here
    // call the corresponding page to display the result
   }

 }

?>

如果您有任何进一步的疑问,请告诉我

于 2012-07-20T05:50:15.567 回答
0

哎呀!..

1.必须有一个入口,另一个主要是控制器。方法如下:

动作.php

<?php
   include 'common.inc.php'; //they are hungryClass,dbConnect etc that you need required;
    $do=$_POST['do'];
   $hungry=new hungryClass();
   if(!empty($do)){
   if(method_exists($hugry,$do)){
        $hugry->$do();
   }else
        echo 'method not exists;'
   }
   }
  ?>

2.form.html

<form action="action.php" method="post" id="searchMealForm">
  <input type="search" size="35" placeholder="What Food Are you looking for?" id="mealName" class="meal"/>
  <input type="search" placeholder="City Area" id="mealLocation" class="meal">
  <input type="submit" value="Satisfy Me" id="findMeal" />
</form>

你不能只学习OOP,MCV你也必须学习......

于 2012-07-20T06:03:28.303 回答