2015-10-23 72 views
2

如何將datepicker日期複製到PHP變量$adateStringValue使用jQuery datepicker和PHP時未定義的變量錯誤

當我使用「$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); 
?> 
+1

形式方法後,嘗試$ _ POST [「名」] – Rayon

+0

@RayonDabre我嘗試使用POST。它並沒有解決問題。 – newbie

+0

您在datepicker上的'name'和'value'屬性之間缺少一個空格。 –

回答

0
<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> 
+2

這並不回答 – Terminus

+1

誰在地球上_upvoted_這? –

1

更改你的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 &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>"; 
} 

?>