简体   繁体   中英

Send dynamic checkbox value to php via ajax form bootstrap form

I want to submit a Bootstrap form that contains a dynamic checkbox field which is having data from another table's field and I want it to send to PHP via JQuery's ajax. Please tell me how to make ajax function to get data from bootstrap modal.here is my code. HTML code

<div class="form-group" id="myResponse">
                <label for="event1">Event</label>
                <?php
                    $sql = "SELECT event_name FROM event1";
                    $stmt = sqlsrv_query($conn, $sql);
                    if( $stmt === false) 
                    {
                       die( print_r( sqlsrv_errors(), true));
                    }

                    $numFields = sqlsrv_num_fields($stmt);

                    while(sqlsrv_fetch($stmt)) 
                    {
                       // Iterate through the fields of each row.
                       for($i = 0; $i < $numFields; $i++) 
                        { 
                            echo '<input type="checkbox" name="event[]"/>'." ";
                            echo sqlsrv_get_field($stmt, $i)." ";

                        }
                       echo "<br />";
                    }
                ?>  
            </div>

php code

<?php
//Database inclusion
include_once 'db_connection.php';
//get values
if(isset($_POST['addGuest']))
{   
    $first_name     =   $_POST['first_name'];
    $last_name      =   $_POST['last_name'];
    $email          =   $_POST['email'];
    $event1         =   $_POST['event1'];

    //name can contain only alpha characters and space
    /*if (isset($_POST['addRecord'])) 
    {*/
        $tsql = "INSERT INTO dbo.demo (first_name, last_name, email, event1) values (?, ?, ?, ?)";
        $var = array($first_name, $last_name, $email ,$event1 );
        $stmt = sqlsrv_query( $conn, $tsql, $var);
        if( $stmt === false ) 
        {
            die( print_r( sqlsrv_errors(), true));
        }
        echo $successmsg = "Successfully Registered!";
    /*}*/
}

?>

    Hi first of all change this filed to and onsubmit call addform button

    $event1         =   $_POST['event'];

    enter code here

 function addform()
{
$.ajax({
            url : 'process.php',
            type : 'POST',
            data : $('#form').serialize(),
            success: function(data){





            }
})

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM