2011-03-25 103 views
1

我也是mootools和web開發的新手。我已經閱讀了這個非常酷的blog ,我想擴展代碼來連接數據庫以更新php文件的評級。但不幸的是我的代碼不工作意味着數據庫沒有更新。有人可以解釋我爲什麼。非常感謝......mootools單選按鈕問題

下面的代碼

star.html

<html> 

<script src="mootools-1.3.js"></script> 
<script src="lorenzos-MooStarRating-422072a/Source/moostarrating.js"></script> 
<script> 

    // Configure the image paths 
    var MooStarRatingImages = { 
     defaultImageFolder: 'lorenzos-MooStarRating-422072a/Graphics/', 
     defaultImageEmpty: 'star_empty.png', 
     defaultImageFull: 'star_full.png', 
     defaultImageHover: "star_boxed_hover.png" 
    }; 

    // Post iD 
    var postId = 10; 

    // When the DOM is ready.... 
    window.addEvent("domready",function() { 

     // Create our instance 
     // Advanced options 
     var advancedRating = new MooStarRating({ 
      form: 'ratingsForm', 
      radios: 'rating', 
      half: false, 
      //imageEmpty: 'star_boxed_empty.png', 
      //imageFull: 'star_boxed_full.png', 
      //imageHover: "star_boxed_hover.png", 
      width: 17, 
      tip: 'Rate <i>[VALUE]/7.0</i>', 
      tipTarget: $('htmlTip'), 
      tipTargetType: 'html', 
      click: function(value) { 
       // Send ajax request to server 
       new Request.send({ 
        url: "rateSave.php", 
        data: {'rating': value} 
       }); 
      } 
     }); 



    }); 

</script> 

<form name="ratingsForm"> 
<label>Select The Number of Stars</label> 
<input type="radio" name="rating" value="1.0" checked="checked"> 

<input type="radio" name="rating" value="2.0"> 

<input type="radio" name="rating" value="3.0"> 

<input type="radio" name="rating" value="4.0"> 

<input type="radio" name="rating" value="5.0"> 

<input type="radio" name="rating" value="6.0"> 

<input type="radio" name="rating" value="7.0"> 

<!--<input type="radio" name="rating" value="7.5"> 
<input type="radio" name="rating" value="8.0"> 
<input type="radio" name="rating" value="8.5"> 
<input type="radio" name="rating" value="9.0"> 
<input type="radio" name="rating" value="9.5"> 
<input type="radio" name="rating" value="10.0">--> 



<span id="htmlTip"></span> 
</form> 

</html> 

rateSave.php

<?php 

$con = mysql_connect("localhost","root",""); 
if (!$con){ 
    die('Could not connect: ' . mysql_error()); 
} 
mysql_select_db("rating", $con); 

$starCount =$_POST['rating']; 

$result=mysql_query("INSERT INTO star VALUES('hotel','$starCount')"); 



mysql_close($con); 

?> 
+0

您提到數據庫沒有更新,但是是否有任何錯誤消息? – JackWilson 2011-03-25 08:07:17

+0

不,沒有錯誤信息顯示...我無法弄清楚爲什麼會發生這種情況:-( – 2011-03-25 08:18:28

+1

您是否檢查過使用POST實際提交的值是否包含您期望的數據? – ChrisR 2011-03-25 08:25:45

回答

6

嗨Pavithra Gunasekara,錯誤的是「什麼',這裏:

click: function(value) { 
// Send ajax request to server ... 
} 

,而不是「點擊」,則回調函數的名稱是的onClick即

onClick: function(value) { 
// Send ajax request to server ... 
} 

關於「點擊」,你可以做這樣即

advancedRating.addEvent('click', function(){ new Request.send({/* ... */}) }); 

正與例如「 onClick'代替新實例定義中的'click':http://jsfiddle.net/steweb/LDw4y/

+0

這是不對的,它是'點擊':https://github.com/lorenzos/MoStarRating/wiki/docs – 2011-03-25 08:53:47

+1

這是不正確的人:) ..如果你想要使用點擊裏面的新實例定義,你必須使用'onClick'..試試吧;) 如果你想添加點擊一次你有實例,你必須使用addEvent('click',foo) – stecb 2011-03-25 08:56:24

+0

然後解釋爲什麼這個工作,這是完全一樣的,這也發生在gibe與文檔:http://davidwalsh.name/dw-content/mootools-star-rating.php – 2011-03-25 08:58:04