将变量从javascript传递到php文件,以便在

时间:2016-09-05 18:48:14

标签: javascript php mysql

我正在尝试向我的网络应用程序添加新功能。我希望用户能够选择实际工作的日期和时间。为此,我创建了一个日历,我可以在其中选择可用的日期和时间。在日历中,他们可以选择不同的小时间隔。直到这里一切都很好。现在我需要在数据库中添加该信息。为此我明白我必须将值传递给.php文件,以便我可以将它们保存在数据库中。问题是如何将该值传递给php文件。之后如何将它们添加到数据库中我猜它不会成为问题。但是,如何将选定的值发送到php文件,以便保存该信息?

任何帮助将非常感谢。 谢谢。

JS

function isSlotSelected($slot) { return $slot.is('[data-selected]'); 
function isSlotSelecting($slot) { return $slot.is('[data-selecting]'); }

/**
 * Get the selected time slots given a starting and a ending slot
 * @private
 * @returns {Array} An array of selected time slots
 */
function getSelection(plugin, $a, $b) {
var $slots, small, large, temp;
if (!$a.hasClass('time-slot') || !$b.hasClass('time-slot') ||
    ($a.data('day') != $b.data('day'))) { return []; }
$slots = plugin.$el.find('.time-slot[data-day="' + $a.data('day') + '"]');
small = $slots.index($a); large = $slots.index($b);
if (small > large) { temp = small; small = large; large = temp; }
return $slots.slice(small, large + 1);
}

DayScheduleSelector.prototype.attachEvents = function () {
var plugin = this
  , options = this.options
  , $slots;

this.$el.on('click', '.time-slot', function () {
  var day = $(this).data('day');
  if (!plugin.isSelecting()) {  // if we are not in selecting mode
    if (isSlotSelected($(this))) { plugin.deselect($(this)); }
    else {  // then start selecting
      plugin.$selectingStart = $(this);
      $(this).attr('data-selecting', 'selecting');
      plugin.$el.find('.time-slot').attr('data-disabled', 'disabled');
      plugin.$el.find('.time-slot[data-day="' + day + '"]').removeAttr('data-disabled');
    }
  } else {  // if we are in selecting mode
    if (day == plugin.$selectingStart.data('day')) {  // if clicking on the same day column
      // then end of selection
      plugin.$el.find('.time-slot[data-day="' + day + '"]').filter('[data-selecting]')
        .attr('data-selected', 'selected').removeAttr('data-selecting');
      plugin.$el.find('.time-slot').removeAttr('data-disabled');
      plugin.$el.trigger('selected.artsy.dayScheduleSelector', [getSelection(plugin, plugin.$selectingStart, $(this))]);
      plugin.$selectingStart = null;
    }
  }
  });

this.$el.on('mouseover', '.time-slot', function () {
  var $slots, day, start, end, temp;
  if (plugin.isSelecting()) {  // if we are in selecting mode
    day = plugin.$selectingStart.data('day');
    $slots = plugin.$el.find('.time-slot[data-day="' + day + '"]');
    $slots.filter('[data-selecting]').removeAttr('data-selecting');
    start = $slots.index(plugin.$selectingStart);
    end = $slots.index(this);
    if (end < 0) return;  // not hovering on the same column
    if (start > end) { temp = start; start = end; end = temp; }
    $slots.slice(start, end + 1).attr('data-selecting', 'selecting');
  }
});
};

HTML

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="style.css">
<link href='http://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
<style>
body { font-family:'roboto'; background-color:#ECF0F1; }
</style>
</head>
<body>
<h1 style="margin:150px auto 30px auto;"></h1>
<div id="day-schedule"></div>

<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="../src/index.js"></script>
<script>
(function ($) {
  $("#day-schedule").dayScheduleSelector({
  });
  $("#day-schedule").on('selected.artsy.dayScheduleSelector', function (e, selected) {
    console.log(selected);
  })
})($);
</script>
</body>
</html>

所做的更改

作为建议我试图使用Ajax函数,我告诉我要传递哪些参数,以及将要接收它们的文件。但没有任何事情发生......

以下是更改。谢谢!

    this.$el.on('mouseover', '.time-slot', function () {
    var $slots, day, start, end, temp;
    if (plugin.isSelecting()) {  // if we are in selecting mode
      day = plugin.$selectingStart.data('day');
      $slots = plugin.$el.find('.time-slot[data-day="' + day + '"]');
      $slots.filter('[data-selecting]').removeAttr('data-selecting');
      start = $slots.index(plugin.$selectingStart);
      end = $slots.index(this);
    if (end < 0) return;  // not hovering on the same column
    if (start > end) { temp = start; start = end; end = temp; }
      $slots.slice(start, end + 1).attr('data-selecting', 'selecting');
    }
    console.log(day);
    $.ajax({
     url:   "/Member/test.php",
     dataType:"json",
     type:  "POST",
    data:  {
     weekDay: 'day',
     start: 'start',
     end:   'end'
  }
})
});

2 个答案:

答案 0 :(得分:1)

使用Ajax或者您可以使用<form action=your_php_file.php>包装输入并将其发布/获取到php脚本

答案 1 :(得分:1)

使用AJAX调用发布到您的php脚本,然后在您的php脚本中通过$ _POST数组访问参数,即:

发布到php脚本的代码(在JS文件中插入代码):

$.ajax({
  url:   "/subdirectory/model.php",
  type:  "POST",
  data:  {
    day: "friday",
    start: "07:00:00",
    end:   "16:00:00"
  }
})
.done function(data)({
  console.log(data)
})
.fail function()({
  console.log("Parameters failed to be sent to php!")
})

访问/htdoc/subdirectory/model.php中从JS传递的参数的代码:

<?php
  var_dump($_POST);
?>

执行JS应该会导致三个示例参数显示在浏览器的日志中。对于db,而不是vardumping $ _POST,您将向php脚本添加代码,将代码写入数据库。