【问题标题】:Inserting values into database using PDO in Jquery AJAX PHP在 Jquery AJAX PHP 中使用 PDO 将值插入数据库
【发布时间】:2018-07-26 22:42:04
【问题描述】:

我仍在学习 PHP,我正在尝试使用下拉菜单向数据库中插入新值。到目前为止,这是我所做的:

<table class="table">
<tr>
    <th>Employee Name</th>
    <th>Time</th>
    <th>Priority</th>
    <th>Assignee</th>
    <th>Subject</th>
    <th>Problem</th>
    <th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT * FROM tickets order by ticketno DESC");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
    <td><?php echo $row_message['full_name']; ?></td>
    <td><?php echo $row_message['time']; ?></td>
    <td><?php echo $row_message['priority']; ?></td>
    <?php if ($row_message['assignee']) : ?>
    <td><?php echo $row_message['assignee']; ?></td>
    <?php else : ?>
    <td>
        <form method="post" action="update1.php">
            <input type="hidden" name="ticketno" value="<?php echo $row_message['ticketno']; ?>" />
            <input type="submit" name="accept" value="Accept"></input>
        </form>
    </td>
    <?php endif ; ?>
    <td><?php echo $row_message['subject']; ?></td>
    <td><?php echo $row_message['problem']; ?></td>
    <td>
        <label for=""></label> <select style="font-family: Questrial;" name="status" required>
            <option disabled selected hidden>Select Status</option>
            <option name="status" value="In Progress">In Progress</option>
            <option name="status" value="Closed: Cancelled">Closed: Cancelled</option>
            <option name="status" value="Closed: Solved">Closed: Solved</option>
        </select>
    </td>
</tr>
<?php    }    ?>
</table>

我还有一个示例脚本:

<script>
$(document).ready(function(){
 $('input[type="radio"]').click(function(){
 var gender = $(this).val();
 $.ajax({
 url:"insert.php",
 method:"POST",
 data:{gender:gender},
 success: function(data){
 $('#result').html(data);
 }
 });
 });
});
</script>

我担心的是,如果我使用下拉菜单,我不确定我的 ('input[type="radio"]') 应该是什么。

这是我的表架构:

表架构

【问题讨论】:

标签: php html mysql database phpmyadmin


【解决方案1】:
<!-- page1.php--->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="">Change Status</label>
    <select style="font-family: Questrial;" name="status" id="status" onchange="getdropdownvalue();" required>
        <option disabled selected hidden>Select Status</option>
        <option name="status" value="in_progress">In Progress</option>
        <option name="status" value="cancelled">Closed: Cancelled</option>
        <option name="status" value="solved">Closed: Solved</option>
    </select>
    <div id="result">
    </div>

<script>                            
    function getdropdownvalue(){
        var status =$("#status").val();
        alert("status value is-"+status);
        if(status!=''){

            $.ajax({
                        type:'POST',
                        url:"insert.php",
                        data:"status="+status,
                        success:function(data){
                         if(data.trim() == 'success')
                                    {
                                       $("#result").html("<div style='color:green;'>record inserted successfully </div>");
                                    }else{
                                        // error

                                    }
                                } 
                    });
        }
    }
</script>

<?php 
/**************  insert.php  page ***********/
// you can get status value by $_POST
if(isset($_POST['status'])){
    $status = $_POST['status'];
    // insert query here 
    // if insert successfully you should - 
    //echo "success"; 
}

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 1970-01-01
    • 2018-08-01
    • 2016-12-08
    • 1970-01-01
    • 2012-11-11
    • 2014-08-28
    相关资源
    最近更新 更多