简体   繁体   English

将JavaScript变量提交到PHP表单

[英]JavaScript variables to PHP Form on submit

I use the following code : 我使用以下代码:

// Datepicker / Timepicker jQuery Script
$('#datetimepicker1').datetimepicker({
    format : "YYYY-MM-DD"
}).on("dp.change", function (e) {
    datetimepicker1 = $('#datetimepicker1').val();
});

How do I get my PHP form to get the variable 'datetimepicker1' on form submission to send it to my SQL database. 如何获取我的PHP表单以在表单提交时获取变量“ datetimepicker1”,以将其发送到我的SQL数据库。

Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

as posted in the comments - you can do it through AJAX, or you can set a hidden input value in your form so that it passes with the rest of the form on submission. 如评论中所述-您可以通过AJAX进行操作,也可以在表单中设置一个隐藏的输入值,以便在提交时与表单的其余部分一起传递。 The following creates a hidden input which you would put into your existing form, and then on the change in the date-time picker, will add the value to this hidden input. 下面的代码创建一个隐藏的输入,将其放入现有表单中,然后在日期时间选择器中进行更改后,会将值添加到该隐藏的输入中。 The form can then submit as normal and the value will be passed with the rest of the form variables. 然后可以正常提交表单,并且该值将与其余表单变量一起传递。

//input in form
<input type='hidden' name='new_dateTime' value='' />


// Datepicker / Timepicker jQuery Script
$('#datetimepicker1').datetimepicker({
    format : "YYYY-MM-DD"}
    ).on("dp.change", function (e) {
        datetimepicker1 = $('#datetimepicker1').val();
$('[name = new_dateTime]').val(datetimepicker1);
    });

You should try this: 您应该尝试这样:

where #form is the form id like this: 其中#form是这样的表单ID:

<form id="form" method="post">

Here is the jquery code: 这是jQuery代码:

$(document).ready(function() {

   $('#form').submit(function() { 

     e.preventDefault();// Datepicker / Timepicker jQuery Script
     $('#datetimepicker1').datetimepicker({
        format : "YYYY-MM-DD"}
       ).on("dp.change", function (e) {
        datetimepicker1 = $('#datetimepicker1').val();
    });
  });
});

You can also try this : 您也可以尝试以下方法:

<input type="submit" name="submit" id="submit">

Changes in jquery: jQuery中的更改:

$(document).ready(function() {

   $('#submit').click(function() { 

     e.preventDefault();// Datepicker / Timepicker jQuery Script
     $('#datetimepicker1').datetimepicker({
        format : "YYYY-MM-DD"}
       ).on("dp.change", function (e) {
        datetimepicker1 = $('#datetimepicker1').val();
    });
  });
});

Make an AJAX request on dp.change event, It will be something like: 在dp.change事件上发出AJAX请求,它将类似于:

$('#datetimepicker1').datetimepicker({
format : "YYYY-MM-DD"}
).on("dp.change", function (e) {
    datetimepicker1 = $('#datetimepicker1').val();
    $.ajax({
       url: 'AJAX.php', //PHP file to send value
       method: 'GET', //or POST
       data: {date_time: datetimepicker1},
       success: function(response) {
         console.log(response);
       }
    });
});

In AJAX.php file, use $_GET to trive the data and pass to SQL/Database. 在AJAX.php文件中,使用$_GET整理数据并将其传递到SQL / Database。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM