2017-01-06 96 views
-2

我在ajax函數中獲取一些數據,並且我想使用函數(php)將它發送到php。
我寫了一些代碼,但我不確切地知道什麼是正確的方法,以及如何將這些數據保存到數據庫中。
下面是我的表格,我到目前爲止所嘗試的。在阿賈克斯如何通過PHP中的JQuery/Ajax函數發送數據到PHP函數

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(""); 

    }); 
} 

這裏

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

獲得的數據連接代碼

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

}

回答

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

@Hikamt Sijapati仍然一樣,如何將數據從前一頁發送到此頁 –