2012-09-20 53 views
3

我想實現Ajax jQuery表單提交。我粘貼在下面的代碼不起作用。我得到daymonthyear簡單的Ajax表單提交錯誤

jQuery的阿賈克斯

<script> 
$(document).ready(function() { 
    $('#cal_submit').click(function(event) { 
     if ($('select[name="day"]').val() == '') { 
      event.preventDefault(); 
      alert('Please enter a day'); 
     } 
     else { 
      event.preventDefault(); 
      new_day = $('select[name="day"]').val(); 
      new_month = $('select[name="month"]').val(); 
      new_year = $('select[name="year"]').val(); 
      $.get('function.php', { day: new_day, month: new_month, year:new_year}); 
      $('select[name="day"]').val(''); 
      $('select[name="month"]').val(''); 
      $('select[name="year"]').val(''); 
     } 
    }); 
}); 
</script> 

在處理PHP所在的函數未定義的索引錯誤。

function calendar() 
{ 


     $j = mysql_real_escape_string($_GET['day']); 
     $F = ucwords(mysql_real_escape_string($_GET['month'])); 
     $Y = mysql_real_escape_string($_GET['year']); 
     $date =" ".$F." ".$j.", ".$Y." "; 

    $query = "SELECT * 
       FROM calendar 
       WHERE event_day = '".$j."' 
       AND event_month = '".$F."' 
       AND event_year = '".$Y."'" ; 
      $run = mysql_query($query); 
      $norows = mysql_numrows($run); 

      if ($norows < 1){ 
       echo "<div class=\"indexnoresult\">No TAP Event(s) for $date</div>" ; 
      } else { 
      while($row = mysql_fetch_array($run)) { 

        echo "<div class=\"indexnoresult\">Event(s) for $date<br> 
        Name : $row[event_name] <br> 
        Time : $row[event_time] <br> 
        Venue : $row[event_venue] <br> 
        </div> 
       " ; 



      } 

      } ?> 

HTML表單

 <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" > 

       Pick Date To View Events  

      <select name="day"> 
        <option value =""> </option> 
        <option value ="1">1</option> 
        <option value ="2">2</option> 
      </select> 

    Month 
       <select name="month"> 
        <option value =""> </option> 
        <option value ="january">January</option> 
        <option value ="february"> february </option> 
        <option value ="march">March</option> 
       </select> 

    Year: 
       <select name="year"> 
        <option value =""> </option> 
        <option value ="2005">2005</option> 
        <option value ="2006">2006</option>    
       </select> 

    <input type='submit' name='cal_submit' id='cal_submit'> 
    </form> 
    </div> 

請在那裏我是我走錯了。

+1

MySQL有'DATE'類型。 –

+0

'$ .get('function.php',{day:new_day,month:new_month,year:new_year});'new_day,new_month,new_year實際上在代碼中的任何位置分配了一個值嗎? –

+0

什麼是'new_day'' new_month'和'new_year'? – Deepak

回答

0

你需要有相同的變量在你的Ajax對象/數據。

 day = $('select[name="day"]').val(); 
     month = $('select[name="month"]').val(); 
     year = $('select[name="year"]').val(); 
     $.get('function.php', { day: day, month: month, year:year}); 

第一選擇選項(當AJAX調用,未做選擇列表選擇默認值)是空白的,這就是爲什麼錯誤。

此外,表單表示POST方法,而您的ajax是GET。

0

更改以下塊

 day = $('select[name="day"]').val(); 
     month = $('select[name="month"]').val(); 
     year = $('select[name="year"]').val(); 

以下

 new_day = $('select[name="day"]').val(); 
     new_month = $('select[name="month"]').val(); 
     new_year = $('select[name="year"]').val(); 
+0

謝謝。我仍然有錯誤。 – ilp

+0

你從哪裏得到錯誤?它來自PHP嗎? – Deepak

+0

是的。 function.php – ilp