【问题标题】:Undefined variable error when using jQuery datepicker and PHP使用 jQuery datepicker 和 PHP 时出现未定义变量错误
【发布时间】:2015-10-23 11:41:48
【问题描述】:

如何将 datepicker 日期复制到 PHP 变量 $adateStringValue

当我使用“$adateStringValue = $_POST['datepicker'];”时,我得到Undefined variable: 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>

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>
?>

我需要用数据库中的一列检查这个选定的日期(该列有字符串格式的日期)。

if(isset($_POST['submit']))
{
$sql1="SELECT event_date from events_schedule";
$getDates_query= mysql_query($sql1,$con);
$adateStringValue = $_GET['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>";
       }
     }
  }

完整代码如下:

<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 &nbsp;&nbsp;<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);
?>

【问题讨论】:

  • 表单方法是post,试试$_POST['name']
  • @RayonDabre 我尝试使用 POST .. 它没有解决问题。
  • 您在日期选择器的 namevalue 属性之间缺少一个空格。
  • &lt;input id=\"datepicker\" name=\"datepicker\" value=\"$adateStringValue\"/&gt; 这行应该是这样的..你不能在 echo 里面有&lt;?php。您需要使用 IDE
  • 您也没有在文件末尾关闭您的字符串。在`'之后添加";

标签: javascript php jquery html datepicker


【解决方案1】:

将您的 PHP 更改为以下内容,以便在使用它之前声明 $adateStringValuebefore。相应地调整以添加您的 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 &nbsp;&nbsp;<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>";
}

?>

【讨论】:

    【解决方案2】:
    <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>
    

    【讨论】:

    • 这不能回答问题
    猜你喜欢
    • 2021-06-03
    • 2015-07-05
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多