duanqiang9212 2014-01-17 04:19
浏览 41
已采纳

Jquery对话框表单没有正确地将字段内容发布到PHP文件

One of my web pages has a link on it.

<div id="updateDay"><a href="#" id = "updateDiffDay">Update</a> a different day</div>

When the user clicks the updateDiffDay link a jquery modal dialog box opens with a form on it. The form has two fields, one is a datepicker and the other is a text box.

<form id="updateDiffDayForm" method="post" action="updateDiffDay.php">
    <input class = "field" type="text" placeholder= "Select a date" id="datepicker">
    <input type="text" id="diffDayDailyMetric" class="field" placeholder="Enter weight" />
</form>

The dialog box has two buttons on it, Update and Cancel. When the user clicks the Update button the form should post the content the user entered on the form to the updateDiffDay.php file.

The code for the modal dialog box is as follows:

<script>
$(function() {
$( "#datepicker" ).datepicker({
  dateFormat: 'yy-mm-dd',
  changeMonth: true,
  changeYear: true,
yearRange: "-90:+0"
  });

$( "#dialog" ).dialog({
  autoOpen: false,
  width: 350,
  height: 250,
  modal: true,
  dialogClass: 'ui-dialog',
  draggable: false,
  resizable: false,
  buttons: {
        Update: function () {
    $('#updateDiffDayForm').submit();
        },
        Cancel: function () {
            $(this).dialog("close");
        }
    },

  show: {
    duration: 500
  },
  hide: {
    duration: 500
  }
});

$( "#updateDiffDay" ).click(function() {
  $( "#dialog" ).dialog( "open" );
});

});

The code for updateDiffDay.php is as follows:

<?php
$date = $_POST['datepicker'];
$metric = $_POST['diffDayDailyMetric'];
echo $date;
echo $metric;

Everything seems to work up until the fields in the form are posted to the PHP file. The problem is when the user clicks the Update button the content of the input boxes on the form (i.e., the date the user picks in the date picker and the value entered in the diffDayDailyMetric input box) are ignored and the default date for the datepicker, 1970-01-01, is posted to datepicker and nothing is posted to diffDayDailyMetric.

I've tried a lot and searched the forums but haven't been able to make this work. Any help would be greatly appreciated. This is the first time I have ever used jquery like this. Help is greatly appreciated. Thanks

  • 写回答

1条回答 默认 最新

  • donqh00404 2014-01-17 04:27
    关注

    In your form:

    <form id="updateDiffDayForm" method="post" action="updateDiffDay.php">
        <input class = "field" type="text" placeholder= "Select a date" id="datepicker">
        <input type="text" id="diffDayDailyMetric" class="field" placeholder="Enter weight" />
    </form>
    

    You're not using a name attribute. Update it with:

    <form id="updateDiffDayForm" method="post" action="updateDiffDay.php">
        <input class="field" type="text" placeholder="Select a date" id="datepicker" name="datepicker">
        <input type="text" id="diffDayDailyMetric" class="field" placeholder="Enter weight" name="diffDayDailyMetric" />
    </form>
    

    And that should do the trick.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果