2012-10-15 42 views
0

我試圖從Ajax發佈到PHP文件來構建民意測驗系統,但我無法發送任何請求,因爲我看不到單選按鈕...... Cookie檢查有什麼問題......我只想檢查用戶IP,如果他已經投消失單選按鈕,如果不是他顯示單選按鈕爲什麼我檢查cookie時看不到單選按鈕?

的index.html

<script type="text/javascript" > 
     $(function(){ 
      var loader=$('#loader'); 
      var pollcontainer=$('#pollcontainer'); 
      loader.fadeIn(); 
      //Load the poll form 
      $.get('poll.php', '', function(data, status){ 
       pollcontainer.html(data); 
       animateResults(pollcontainer); 
       pollcontainer.find('#viewresult').click(function(){ 
        //if user wants to see result 
        loader.fadeIn(); 
        $.get('poll.php', 'result=1', function(data,status){ 
         pollcontainer.fadeOut(1000, function(){ 
          $(this).html(data); 
          animateResults(this); 
         }); 
         loader.fadeOut(); 
        }); 
        //prevent default behavior 
        return false; 
       }).end() 
       .find('#pollform').submit(function(){ 
        var selected_val=$(this).find('input[name=poll]:checked').val(); 
        if(selected_val!=''){ 
         //post data only if a value is selected 
         loader.fadeIn(); 
         $.post('poll.php', $(this).serialize(), function(data, status){ 
          $('#formcontainer').fadeOut(100, function(){ 
           $(this).html(data); 
           animateResults(this); 
           loader.fadeOut(); 
          }); 
         }); 
        } 
        //prevent form default behavior 
        return false; 
       }); 
       loader.fadeOut(); 
      }); 

      function animateResults(data){ 
       $(data).find('.bar').hide().end().fadeIn('slow', function(){ 
        $(this).find('.bar').each(function(){ 
         var bar_width=$(this).css('width'); 
         $(this).css('width', '0').animate({ width: bar_width }, 1000); 
        }); 
       }); 
      } 

     }); 
    </script> 

poll.php

if (!isset($_POST['poll']) || !isset($_POST['pollid'])) { 
    $query = mysql_query("SELECT id, ques FROM questions ORDER BY id DESC LIMIT 1"); 
    while ($row = mysql_fetch_assoc($query)) { 
     //display question 
     echo "<p class=\"pollques\" >" . $row['ques'] . "</p>"; 
     $poll_id = $row['id']; 
    } 
    if (isset($_GET["result"]) == 1 || isset($_COOKIE["voted" . $poll_id]) == 'yes') { 
     //if already voted or asked for result 
     showresults($poll_id); 
     exit; 
    } else { 
     //display options with radio buttons 
     $query = mysql_query("SELECT id, value FROM options WHERE ques_id=$poll_id"); 
     if (mysql_num_rows($query)) { 
      echo '<div id="formcontainer" ><form method="post" id="pollform" action="' . $_SERVER['PHP_SELF'] . '" >'; 
      echo '<input type="hidden" name="pollid" value="' . $poll_id . '" />'; 
      while ($row = mysql_fetch_assoc($query)) { 
       echo '<p><input type="radio" name="poll" value="' . $row['id'] . '" id="option-' . $row['id'] . '" /> 
       <label for="option-' . $row['id'] . '" >' . $row['value'] . '</label></p>'; 
      } 
      echo '<p><input type="submit" value="Submit" /></p></form>'; 
      echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?result=1" id="viewresult">View result</a></p></div>'; 
     } 
    } 
} else { 
    // i cant see this radio 
    if (isset($_COOKIE["voted" . $_POST['pollid']]) != 'yes') { 

     //Check if selected option value is there in database? 
     $query = mysql_query("SELECT * FROM options WHERE id='" . intval($_POST["poll"]) . "'"); 
     if (mysql_num_rows($query)) { 
      $query = "INSERT INTO votes(option_id, voted_on, ip) VALUES('" . $_POST["poll"] . "', '" . date('Y-m-d H:i:s') . "', '" . $_SERVER['REMOTE_ADDR'] . "')"; 
      if (mysql_query($query)) { 
       //Vote added to database 
       setcookie("voted" . $_POST['pollid'], 'yes', time() + 86400 * 300); 
      } 
      else 
       echo "There was some error processing the query: " . mysql_error(); 
     } 
    } 
    showresults(intval($_POST['pollid'])); 
} 

回答

0

isset($_COOKIE["voted" . $poll_id]) == 'yes' isset返回true或false,所以此條件始終爲false。 與此相同if (isset($_COOKIE["voted" . $_POST['pollid']]) != 'yes')