我需要一点帮助,了解如何在PHP中使用OOP来执行表单提交操作。 手头的任务...... 我正在尝试学习如何使用OOP编写PHP代码。到目前为止,我理解了类,函数,调用函数,继承等的一般概念。
我创建了一个简单的练习项目,允许用户在某个位置搜索用餐。到目前为止,我有一个包含2个<input>
字段的表单。通常对于表单操作,我会<form action="actionFileName.php">
但是现在我有一个具有处理表单的函数的类,我将如何使用该操作值?
我想过创建一个类的实例并调用处理表单的函数但是我找不到一个Object!我提交的表单中包含地址栏中显示的echo
else
语句中的hungryClass.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();
}
}
&GT;
答案 0 :(得分: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>
答案 1 :(得分:2)
您应该将表单提交到将处理和处理表单的php文件。
<form action="<?php $newSearch->searchMeal();?>" method="post" id="searchMealForm">
应该是这样的:
<form action="formaction.php" method="post" id="searchMealForm">
在formaction.php中你可以调用你的方法,当然你需要包含所需的文件:
<?php
$newSearch->searchMeal();
希望这有帮助。
答案 2 :(得分:1)
这是php中最简单的OOP代码 你可以按照以下方式尝试
<强> form.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);
}
?>
<强> DbClass.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
}
}
?>
如果您有任何进一步的疑问,请告诉我
答案 3 :(得分:0)
OOP!..
1.必须有一个条目,其他主要是控制器。以下方式:
action.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 ....