[英]Get value of input form (PHP)
我正在使用公式,必须将日期划分为日,月和年。 我找到了一种使用$ enterdate变量分隔日期的方法。
<form action="/datepage.php" method="get">
<label data-for="date">Enter Date:</label>
<?php $enterdate = $GET["date"]; list ($day,$month,$year) = split('[/.-]', $enterdate); ?>
<input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
<input type="hidden" id="day" name="day" value="<?php echo $day; ?>" />
<input type="hidden" id="month" name="month" value="<?php echo $month; ?>" />
<input type="hidden" id="year" name="year" value="<?php echo $year; ?>" />
</form>
但是我不知道,我如何获得输入字段的值,即用户填写了输入字段(id =“ datefield”)。 例如,用户输入以下日期: 14.01.16
毕竟,我需要URL中的此GET输出:
&date = 14.01.16&day = 14&month = 01&year = 16
有人知道我做错了什么吗?
如果您将formt与method="get"
一起使用,则其所有输入将通过datepage.php
传递到$ _GET数组中,因此您可以像这样访问输入:
echo $_GET['date'];
echo '<br />';
echo $_GET['day'];
echo '<br />';
echo $_GET['month'];
echo '<br />';
echo $_GET['year'];
echo '<br />';
请记住,它们存储在$ _GET数组中,其中键入了输入名称。
您的html表单:
<form action="/datepage.php" method="get">
<label data-for="date">Enter Date:</label>
<?php $enterdate = $GET["date"]; list ($day,$month,$year) = split('[/.-]', $enterdate); ?>
<input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
<input type="hidden" id="day" name="day" value="<?php echo $day; ?>" />
<input type="hidden" id="month" name="month" value="<?php echo $month; ?>" />
<input type="hidden" id="year" name="year" value="<?php echo $year; ?>" />
<input type="submit" value="Submit">
</form>
提交按钮已添加到您的表单
URL中是否有数据要求? 使用GET将在URL中发送数据。
我将以这种方式进行操作-(摘要:使用post操作发布数据,并使用jQuery填写隐藏输入的值)
这应该可以解决问题。 这是在用户选择一个日期之后,这将使用正确的值更新隐藏的表单字段。 这样,当您发布此表单时,服务器将获取值。
您的表单中添加了提交输入:
<form action="/datepage.php" method="POST">
<label data-for="date">Enter Date:</label>
<input type="text" id="datefield" name="date" class="datepicker" value="" autocomplete="off" />
<!-- set from jQuery -->
<input type="hidden" id="day" name="day" value="" />
<input type="hidden" id="month" name="month" value="" />
<input type="hidden" id="year" name="year" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
jQuery,它将在选择日期选择器日期后在表单中放置正确的值。
<script>
$("#datefield").datepicker({
onSelect: function(dateText) {
var tempArray = dateText.spit('-'); // not sure if you are using - or /
var day = $tempArray[0];
var month = $tempArray[1];
var year = $tempArray[2];
// add that to the hidden values
$("#day").attr("value", day); // set the day value
$("#month").attr("value", month); // month
$("#year").attr("value", year); // year
}
});
</script>
而您的服务器端:
// if the form was submitted show the results on the screen.
if( isset($_POST['submit']) ) {
echo $_POST['date']; // fulldate
echo $_POST['day'];
echo $_POST['month'];
echo $_POST['year'];
}
GET和POST之间的区别http://www.w3schools.com/tags/ref_httpmethods.asp
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.