2012-09-20 52 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