我有一个这样的表格:
<form method="post" action="functions/updateWeek.php">
<label for="dateStart">View From</label>
<input type="date" name="dateStart" required>
<label for="dateEnd">Until</label>
<input type="date" name="dateEnd" required>
<input type="submit" id="submitWeek" value="Go"/>
</form>
还有一些像这样的javascript:
$(document).ready(function() {
//if submit button is clicked
$('#submitWeek').click(function () {
//Get the data from the field
var dateStart = $('date[name=dateStart]');
var dateEnd = $('date[name=dateEnd]');
//organize the data properly
var data = 'dateStart=' + dateStart.val() + '&dateEnd=' + dateEnd.val();
//start the ajax
$.ajax({
//this is the php file that processes the data and send mail
url: "functions/updateWeek.php",
//GET method is used
type: "POST",
//pass the data
data: data,
//Do not cache the page
cache: false,
success: function() {
alert("done");
}
});
//cancel the submit button default behaviours
return false;
});
});
发送到这个php文件:
//Start sessions
session_start();
//include config file
include_once 'config.php';
include_once 'accountFunctions.php';
//get start date from form
$dateStart = $_POST['dateStart'];
//get end date from form
$dateEnd = $_POST['dateEnd'];
//collect the start week
$startWeek = getStartWeek($dateStart);
//collect the end week
$endWeek = getEndWeek($dateEnd);
我没有包含剩余的功能。
我遇到的问题是在检查时(因为查询不起作用),我的post方法中的dateStart和dateEnd都显示为“未定义”。
我对于为什么不是最模糊的。我错过了一些明显的东西吗我怀疑它必须是我的jquery选择器的一个问题,但如果是这样,它会是什么?
答案 0 :(得分:5)
你必须使用像这样的选择器,
var dateStart = $('input[name="dateStart"]');
var dateEnd = $('input[name="dateEnd"]');
答案 1 :(得分:4)
在jQuery中,输入名称的选择器就是这样(在你的情况下):
var dateStart = $('input[name="dateStart"]');
var dateEnd = $('input[name="dateEnd"]');
这也适用于元素中的任何属性:
答案 2 :(得分:0)
试试此代码
var dateStart = $('input[name="dateStart"]').val();
var dateEnd = $('input[name="dateEnd"]').val();
var data = 'dateStart=' + dateStart+ '&dateEnd=' + dateEnd;