How to send data to php function through JQuery/Ajax function in PHP

Question

I am getting some data in an ajax function and I want to send it to php using a function(php).
I wrote some code but I don't know exactly what is the right way and how to save this data to databse now.
Below is my form, what I tried so far.

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

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

    });
}

here is connection code

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

}


Show source
| pdo   | php   | oop   | ajax   | sqlsrv   2017-01-06 07:01 1 Answers

Answers to How to send data to php function through JQuery/Ajax function in PHP ( 1 )

  1. 2017-01-06 09:01

    Change addevent function code as

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

Leave a reply to - How to send data to php function through JQuery/Ajax function in PHP

◀ Go back