Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP form submission using oop

Tags:

php

I need a little help understanding how to use OOP in PHP to perform form submission action. Task at hand... I am 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 have created a simple project for practice that allows a user to search for a meal in a certain location. So far, I have a form with 2 <input> fields. Normally for form action, I would do <form action="actionFileName.php"> but now that I have a class with a function to process the form, what do I use for the action value?

I thought of creating an instance of the class and calling the function that processes the form but I get a Object not found! page after I submit the form with the echo values from the else statements in hungryClass.php displaying in the address bar.

how do 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 to process 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();
    }
}

?>

like image 707
zoey Avatar asked Jul 20 '12 05:07

zoey


2 Answers

The action attribute of the form is for the script name you want to submit to. You want to submit your form to your hungry class to be processed, but this can not be done until you have instantiated your hungry class. You will need to use a script name as the action value in your form. Lets say you want to submit to 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>

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

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

or to 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>
like image 99
hackattack Avatar answered Oct 19 '22 18:10

hackattack


You should submit the form to a php file which will handle and process the form.

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

should be something like:

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

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

<?php
$newSearch->searchMeal();

Hope this helps.

like image 24
code90 Avatar answered Oct 19 '22 16:10

code90