简体   繁体   中英

Store date picker date and save in database

我试图从我在页面上的日期选择器中获取日期,然后将该日期发布到php函数中,该函数将所选的日期存储在数据库中。

I think you have a misunderstanding of how Javascript, HTML forms, and PHP work together.

Javascript (and jQuery) run in the browser. PHP runs on your server.

The browser initiates a request to your server, your server hands off the request to a php process which parses and runs your php. This generates html which is sent back to your browser. The javascript linked to or contained in <script> tags is then loaded, and run if necessary (or triggered by events). This is an oversimplification, but should get across the basics.

So, what is happening is the jQuery is running inside your browser to perform changes to the HTML. Specifically, it is inserting a string formatted as 'MM-DD-YYY' into your form field. When the user then posts this form, those values get added to the HTTP request that is sent to your server. Those values are then accessible in the $_POST variable in your php script, as a key=>value pair, where key is the HTML name='formfield1' attribute of your form. So your would access that as $date = $_POST['formfield1'] .

Javascript, and jQuery specifically are doing nothing but loading a string value into that html field for the user. You can't access Javascript variables from your PHP.

If you post your form code, a more specific answer might be able to be given.

Edit: for an inline datepicker, you will need to configure your datepicker as follows:

$('#datepicker').datepicker({
    inline : true,
    altField : '#hiddenFieldID',
});

where #hiddenFieldID is a hidden input field in your form. Regardless, you have to attach the date value to your form in some way for it to be available in $_POST .

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