2011-11-16 49 views
0

我有以下设置为我的ajax调用,但它不工作时,我点击一个明星(这是一个星级评分),虽然如果我访问ajax.php直接插入,但ajax调用没有发生。试图使ajax调用工作与PHP和MySQL

在PHP文件的HTML

echo '<table> 
    <tr> 
     <td style="padding:10px;"> 
     <input type="hidden" name="userID" value="'.$user_id.'"> 
     <span style="font-size: 20px; vertical-align:top;">Comments</span> 
     </td> 
     <td style="padding:10px;"> 
     <textarea name="comments" cols="60" rows="2"></textarea> 
     </td> 
     <td> 
     <div> 
      <input name="star1000" value "1" type="radio" class="star"/> 
      <input name="star1000" value="2" type="radio" class="star"/> 
      <input name="star1000" value="3" type="radio" class="star"/> 
      <input name="star1000" value="4" type="radio" class="star"/> 
      <input name="star1000" value="5" type="radio" class="star"/> 
      </div> 
     </td> 
    <tr> 
    </table>'; 

JS - 用于发送的值

<script> 
    $('.star').rating({ 
     callback: function(value, link) { 
      var name = $(this).attr('name'); 
      var userID = $(this).closest('td').find('input[name="userID"]').val(); 
      var comments = $(this).closest('td').find('textarea[name="comments"]').val(); 
      $.ajax({ 
      url: "http://localhost/mywebsite/ajax.php", 
      type: "POST", 
      data: { 
       name: name, 
       value: value, 
       userID: userID, 
       comments: comments 
      }, 
      cache: false, 
      success: function(response) { 
       try { 
       console.log(response); 
       } catch (err) { 
       alert(response); 
       } 
      } 
      }); 
     } 
     }); 
    </script> 

位于http://localhost/mywebsite/ajax.php

<?php 

     extract($_POST); 

    $rate_val = $_POST['value']; 
    $user_id = $_POST['userID']; 
    $comments = $_POST['comments']; 

    $insert_q = "INSERT INTO ratings (rate_comments, rate_num, option_id, rate_date,user_id) 
       values ('$comments','$rate_val','1',now(),'$user_id')"; 

    include 'opendbconn.php'; 

       if(!($result = mysql_query($insert_q, $database))) 
       { 
        print("Could not execute query!<br/>"); 
        die(mysql_error()."</div> 
       </div>    
           </body> 
           </html>"); 
       } 

    include 'closedbcon.php'; 
?> 
+1

什么事件您呼叫的评价功能 –

+0

当我在“明星”点击它没有被调用?这里它带着你的类 – user710502

+1

你检查过萤火虫吗?它显示任何错误? –

回答

0

我的ajax.php你必须绑定事件点击你的函数.rating(),这样javascript知道它必须在.rating()时执行函数点击“.star”。

你可以做这样的:

$(document).ready(function(){ 
      //other javascript code... 

      $('.star').bind('click',function(){ 
         //your rating function 
       }); 
     });