PHP form submission using oop

I need a little help understanding how to use OOP in PHP to perform a form submission action. The task is at hand ... I'm trying to learn how to write PHP code using OOP. So far, I understand the general idea of ​​classes, functions, calling functions, inheritance, etc.

I created a simple project for practice that allows the user to search for food in a specific place. So far I have a form with 2 <input> fields. Normally for a form action I would do <form action="actionFileName.php"> , but now that I have a class with a function to handle the form, what do I use for the action value?

I was thinking of creating an instance of the class and calling a function that processes the form, but I get the object was not found! after I submit the form with echo values ​​from the else in hungryClass.php displayed in the address bar.

how can i fix this? Thanks.

What my code looks like: HTML form

 <?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> 

Page in processing 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"; } 

}

Database class

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

? >

+6
source share
4 answers

The form action attribute is for the script name that you want to submit. You want to send your form to your hungry class, which will be processed, but this is not possible until you create your hungry class. You will need to use the script name as the action value in your form. Suppose you want to submit temp.php, your form should look like this:

 <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> 

when these forms are submitted, it will be sent to temp.php. To get your hungry class to handle this form, you need to make an instance of it in temp.php, and call searchMeal with this instance. temp.php should look something like this.

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

or put everything in one file

 <?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> 
+4
source

Here is the simplest OOP code in php you can try as follows

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 } } ?> 

Let me know if you have further questions.

+1
source

You have to submit the form to a php file that processes and processes the form.

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

should look something like this:

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

inside formaction.php you can call your method, of course, you need to include the necessary files:

 <?php $newSearch->searchMeal(); 

Hope this helps.

+1
source

OOP! ..

1. there must be a record, the other main one is the controller. In the following way:

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> 

you cannot study OOP only, MCV you have to study too ....

0
source

Source: https://habr.com/ru/post/920937/


All Articles