Javascript 使用jQuery datepicker和PHP时出现未定义变量错误
如何将Javascript 使用jQuery datepicker和PHP时出现未定义变量错误,javascript,php,jquery,html,datepicker,Javascript,Php,Jquery,Html,Datepicker,如何将datepickerdate复制到PHP变量$adateStringValue 当我使用“$adateStringValue=$\u POST['datepicker'];”时,我得到了未定义的变量:adateStringValue error。我如何纠正这个问题 下面是我的代码 要选择日期,请执行以下操作: <script> $(document).ready(function () { $("#datepicker").datepicker({ dateFo
datepicker
date复制到PHP变量$adateStringValue
当我使用“$adateStringValue=$\u POST['datepicker'];
”时,我得到了未定义的变量:adateStringValue error
。我如何纠正这个问题
下面是我的代码
要选择日期,请执行以下操作:
<script>
$(document).ready(function () {
$("#datepicker").datepicker({
dateFormat: "dd-mm-yy"
});
$('form#dateform').submit(function(){
var aselectedDate = $('#datepicker').val();
localStorage.setItem('adate', aselectedDate);
});
});
</script>
$(文档).ready(函数(){
$(“#日期选择器”)。日期选择器({
日期格式:“dd-mm-yy”
});
$('form#dateform')。提交(函数(){
var aselectedDate=$(“#日期选择器”).val();
setItem('adate',aselectedDate);
});
});
abc.php:
<?php
echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\" method=\"POST\"><br><br>
<table>
<tr><td>
<b>Select date<b></td><td>
<input id=\"datepicker\" name=\"datepicker\"value=\"$adateStringValue\"/>
</td></tr>
</table>
?>
$(函数(){
$('#datetimepicker1')。datetimepicker();
});
将您的PHP更改为以下内容,以便在使用之前声明$adateStringValue
。相应地进行调整,将MySQL逻辑添加回:
<?php
// Ternary IF statement to set either the $_POST value or a blank string
$adateStringValue = (isset($_POST['datepicker'])) ? $_POST['datepicker'] : '';
// Just use a blank action attribute to POST to the same file.
// Also added a space between the action/method attributes to output valid (X)HTML
echo '
<form id="dateform" name="dateform" action="" method="POST"><br><br>
<table>
<tr>
<td><b>Select a date <b></td>
<td><input id="datepicker" name="datepicker" size="15" value="' . $adateStringValue . '"/></td>
</tr>
</table>
<br>
<input type="submit" name="submit" value="Submit"/>
</form>
';
// Output the value if we have POST data
if (isset($_POST['submit'])) {
$message = "You have selected this date: $adateStringValue";
echo "<script>alert('$message');</script>";
}
?>
表单方法是post,请尝试$\u post['name']@RayonDabre我尝试过使用post。。它没有解决问题。您的日期选择器上的名称
和值
属性之间缺少空格。
这一行应该是这样的。。您不能让也不能在文件末尾关闭字符串。在“.”之后添加;
。这并不能回答问题
<link href="http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(document).ready(function () {
$("#datepicker").datepicker({
dateFormat: "dd-mm-yy"
});
$('form#dateform').submit(function(){
var aselectedDate = $('#datepicker').val();
localStorage.setItem('adate', aselectedDate);
});
});
</script>
<?php
$con = mysql_connect("localhost","root","" );
if(!$con){
die("Cannot connect:" .mysql_error());
}
mysql_select_db("trainingschedule",$con);
echo "<form id=\"dateform\" name=\"dateform\" action=\"associate.php\"method=\"POST\"><br><br>
<table>
<tr><td>
<b>Select a date <b></td><td><input id=\"datepicker\" name=\"datepicker\" size=\"15\" value=\"$adateStringValue \" /></td></tr>
</table>
<br>
<input type=\"submit\" name=\"submit\" value=\"Submit\"/>;
</form>";
if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_POST['datepicker'];
while($fetchdates = mysql_fetch_array($getDates_query)) {
if ($fetchdates['event_date'] == $adateStringValue) {
$message = "You have selected this date";
echo "<script>alert('$message');</script>";
}
else {
$message1 = "Select another date";
echo "<script>alert('$message1');</script>";
}
}
}
mysql_close($con);
?>
<div id='datetimepicker1' >
<input type='text' class="form-control" name="datatime" data-date-format="MM/DD/YYYY" />
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker1').datetimepicker();
});
</script>
<?php
// Ternary IF statement to set either the $_POST value or a blank string
$adateStringValue = (isset($_POST['datepicker'])) ? $_POST['datepicker'] : '';
// Just use a blank action attribute to POST to the same file.
// Also added a space between the action/method attributes to output valid (X)HTML
echo '
<form id="dateform" name="dateform" action="" method="POST"><br><br>
<table>
<tr>
<td><b>Select a date <b></td>
<td><input id="datepicker" name="datepicker" size="15" value="' . $adateStringValue . '"/></td>
</tr>
</table>
<br>
<input type="submit" name="submit" value="Submit"/>
</form>
';
// Output the value if we have POST data
if (isset($_POST['submit'])) {
$message = "You have selected this date: $adateStringValue";
echo "<script>alert('$message');</script>";
}
?>