如何通过PHP中的JQuery / Ajax函数将数据发送到php函数

时间:2017-01-06 06:56:19

标签: php ajax oop pdo

我在ajax函数中获取一些数据,我想使用函数(php)将其发送到php。
我写了一些代码,但我不知道究竟什么是正确的方法以及如何将这些数据保存到数据库中。
以下是我的表格,我到目前为止所尝试的内容。

<div class="modal fade" id="add_new_event_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel1">Add New Event</h4>
        </div>
        <div class="modal-body">
            <div class="form-group">
                <label for="first_name">Event Name</label>
                <input type="text" id="event_name" name="fname" placeholder="First Name" class="form-control"/>
            </div>
            <div class="form-group">
                <label for="example-datetime-local-input" class="col-xs-2 col-form-label">Date and time</label>
                <input class="form-control" type="text" id="datetime_local">
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
            <input type="button" class="btn btn-primary" id="AddEvent" value="Add Event" />
        </div>
    </div>
</div>
</div>

在ajax中获取数据

function addEvent() 
{
    //debugger;
    var event_name = $("#event_name").val();
    var wedding_date = $("#datetime_local").val();

    // Add event
    $.post("ajax/addEvent.php", 
    {
        event_name: event_name,
        wedding_date: wedding_date
    }, 
    function (data, status) 
    {
        //console.log(data);
        // close the popup

        alert("Data: " + data + "\nStatus: " + status);
        $("#add_new_event_modal").modal("hide");

        // read records again
        readEvent();

        // clear fields from the popup
        $("#event_name").val("");
        $("#wedding_date").val("");

    });
}

这是连接代码

session_start();
require_once 'db_connection.php';
//if(!class_exists('crud')){ include 'crud.php'; }
class crud
{
    private $conn; //Will be visible in class curd only
    public function __construct()
    {
        $obj = new Connect();
        $db = $obj->dbConnection();
        $this->conn = $db;
    }
    public function addEvent($event_name,$wedding_date)
    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO event1 (event_Name,     wedding_date) VAlUES($event_name ,$wedding_date)");         

            $stmt->bindparam(":event_Name",$event_Name);
            $stmt->bindparam(":wedding_date",$wedding_date);
            $stmt->execute();
            echo "1";
        }
        catch(Exception $e)  
        {   
            die( print_r( $e->getMessage() ) );   
        } 
    }

}

1 个答案:

答案 0 :(得分:0)

将addevent功能代码更改为

public function addEvent()
    {
        try
        {


             $event_name = $_POST['event_name'];
             $wedding_date = $_POST['wedding_date'];
            $stmt = $this->db->prepare("INSERT INTO event1 (event_Name,wedding_date) VAlUES(:event_name ,:wedding_date)");         

            $stmt->bindparam(":event_Name",$event_Name);
            $stmt->bindparam(":wedding_date",$wedding_date);
            $stmt->execute();
            echo "1";
        }
        catch(Exception $e)  
        {   
            die( print_r( $e->getMessage() ) );   
        } 
    }