使用datepicker更新mysql数据库字段jquery ajax php

  1. How can I update mysql fields without refreshing the page, from a list of results from the database.
  2. Using datepicker
  3. In a single .php page?

Example:

order_report.php

$query = $this->db->query("select order_id,
date_format(date_payed,'%d-%m-%Y') as date_payed,
from oc_order");

***code left out to save space***

foreach ($res as $orders) {
print $orders['order_id'];

<input type="text" name="datepicker" id="datepicker" value="<?php echo $orders['date_payed']; ?>"/>
$query = $this->db->query("update oc_order SET date_payed='$datepicker' WHERE  order_id='$orders['order_id'];'");
}

Jquery

<script>
$(document).ready(function() {
$("#datepicker").datepicker({
    dateFormat: 'yy-mm-dd'
    });
})
</script>

I'm assuming what you are asking is that you want the datepicker to update your MySQL table when you select on a date without having to refresh that page. The code you provided is a little confusing, but I put something together that may help.

I am guessing that the input field that's in your code is actually supposed to be the html where you are getting the value.

As for keeping it all on one single page... I don't think that will be possible. For best results, you will have to make a new .php page to accept the value of the datepicker, which is passed through the $.ajax call where it says date. If you did it on the same page, there's a possibility you may end up getting a swamp of unwanted html data returned to you via the data parameter in the success function, but it would most likely throw an error and get the error function callback.

In this example, I called the php page process_date.php.

HTML:

<input type="text" id="datepicker" value="<?php echo $orders['date_payed']; ?>"/>

JS:

<script type="text/javascript">
    $(document).ready(function () {
        $("#datepicker").datepicker({
            dateFormat: "yy-mm-dd",
            onSelect: function () {
                var date = $("#datepicker").val();

                $.ajax({
                     type: "POST", //or GET. Whichever floats your boat.
                     url: "path/to/process_page.php",
                     data: { date: date },
                     success: function(data) {
                         alert(data);

                         //Write code here for you successful update (if you want to)
                     },
                     error: function() {
                         alert("Error.");
                     }
                });
            }
        });
    });
</script>

PHP (process_date.php)

<?php

    $date = $_POST['date'];  //if you used GET in your ajax, then use $_GET here instead


    $query = $this->db->query("select * from oc_order");

    ***code left out to save space***

    foreach ($products as $orders) {
        print $orders['order_id'];

        $query = $this->db->query("update oc_order SET date_payed='$date' WHERE  order_id='$orders['order_id'];'");

        echo "Success";
    }
?>

Note: Not saying you didn't, but don't forget to always sanitize your PHP data before sending it to the server in order to avoid being attacked via SQL Injection. Even if you use parameterized queries, it is still a good idea.