1

I've got a PHP class which has 1 method for inserting data. Once I create my instance of the class, I'm not sure how to call the method. I've seen other questions about how to accomplish calling a method, but none deal with calling the method of a class instance.

My PHP is:

<?php

$servername = "localhost";
$username = "kevin";
$password = "ally";
$database = "test";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert"))
{
    $Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
}

class Dog
{
    public $name = "Dog Name";
    public $size = 0;
    public $color = "255:255:255";
    public $typeName = "type Name";
    public $typeDescription = "type Description";

    public function Dog($name, $size, $color, $type, $description){
        $this->name = $name;
        $this->size = $size;
        $this->color = $color;
        $this->typeName = $type;
        $this->typeDescription = $description;  
    }

    public function InsertDog(){
        $query = "SELECT Id from tbl_type WHERE Name = $typeName";
        $result = mysqli_query($conn, "INSERT INTO tbl_type($this->typeName, $this->$typeDescription)") or die("Query fail: " . mysqli_error());
        $result2 = mysqli_query($conn, "INSERT INTO tbl_Dog($this->$name, $this->$size, $this->$color, $query)") or die("Query fail: " . mysqli_error());
    }

    public function UpdateDog(){
    }
}
?>

And my form:

<form action="index.php" method="post">
    Dog Name:<br />
    <input name="txtName" type="text" /><br />
    <br />
    Size:<br />
    <input name="txtSize" type="text" /><br />
    <br />
    Color:<br />
    <input name="txtColor" type="text" /><br />
    <br />
    type Name:<br />
    <input name="txttype" type="text" /><br />
    <br />
    type Description:<br />
    <input name="txtDescription" style="width: 419px; height: 125px" type="text" /><br />
    <br />
    <input name="btnInsert" type="submit" value="Insert" />
</form>

How can I call the InsertDog() method for the $Dog instance?

2 Answers 2

2

Simply use:

...
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
$Dog->InsertDog();

:)

Sign up to request clarification or add additional context in comments.

Comments

0

You can simply have all your codes in one file (index.php) and then you can use:

$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
$Dog->InsertDog();

Or second solution is add ajax to your code. So link jquery:

<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

edit

<input name="btnInsert" type="submit" value="Insert" /> 

to

<input name="btnInsert" type="submit" value="Insert" id="buttonId" />

and add:

    <script>
    $( document ).ready(function() {
      $( "#buttonId" ).click(function() {
         $.ajax({
            type: "POST",
            url: "index.php",
            data: { txtName: "DogsName", txtSize: "DogSize", txtColor: "color", txttype: "type", txtDescription: "dscr"v}
         })
         .done(function( msg ) {
            alert( "Data Saved: " + msg );
         });
        });
    });
    </script>

url to your index.php must really server/index.php - so if you use localhost, it must be http://localhost/index.php

I didn't test it, but it should works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.