简体   繁体   中英

Inserting values into database using PDO in Jquery AJAX PHP

I'm still learning PHP and I'm trying to insert new values to the database using a dropdown. So far this is what I've made:

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

I also got a sample script:

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

My concern is, I'm not sure what should be my ('input[type="radio"]') if I'm using a dropdown.

Here's my table schema:

Table Schema 图片描述

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

?>

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