通过ajax表单引导表单向php发送动态复选框值

时间:2016-12-29 09:30:27

标签: php jquery sql-server ajax bootstrap-modal

我想提交一个Bootstrap表单,其中包含一个动态复选框字段,该字段包含来自另一个表字段的数据,我希望它通过JQuery的ajax发送给PHP。请告诉我如何使用ajax函数从bootstrap模态中获取数据。这是我的代码。 HTML代码

<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!";
    /*}*/
}

&GT;

1 个答案:

答案 0 :(得分:0)

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





            }
})

}