如何通过按钮点击调用类功能

时间:2015-02-02 17:47:12

标签: php

我有一个PHP类,它有一个插入数据的方法。一旦我创建了我的类实例,我不知道如何调用该方法。我已经看到other questions关于如何完成调用方法,但没有一个涉及调用类实例的方法。

我的PHP是:

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

我的表格:

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

如何为InsertDog()实例调用$Dog方法?

2 个答案:

答案 0 :(得分:2)

只需使用:

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

:)

答案 1 :(得分:0)

您只需将所有代码放在一个文件中(index.php),然后就可以使用:

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

或者第二个解决方案是在代码中添加ajax。所以链接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>

编辑

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

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

并添加:

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

你的index.php的url必须真的是server / index.php - 所以如果你使用localhost,它必须是http://localhost/index.php

我没有测试它,但它应该可行