Results 1 to 3 of 3

Thread: Quiz with php and without sql

  1. #1
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Quiz with php and without sql

    http://www.w3schools.com/php/php_ajax_poll.asp
    hello guys i say this threat and tried to implement it but it does not seem to show any proper results and does not edit the text file.
    What I am doing wrong?
    Also I would like it not to load the question every time i refresh and the user should be allowed to vote once

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You need some way of storing the data on the server. You don't necessarily need sql, or even any database, but if not, then you need it to store in text files. Either way, you must store the IP or username of the users who have voted, and there is no way around that. You could place a cookie on their computer so they could only vote once per session, but a user who knows a bit about computers could delete it and vote repeatedly easily. It looks like the tutorial uses files, so that's fine, but you'll need to add a few options to make that fully work.

    If you want help with this, you need to post your code in addition to the link the tutorial you are using. There is something wrong with how you used that code, not with the tutorial. It is possible that your server just doesn't work with something, such as with php modifying files, though. But we can't help without information about your site.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Dec 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thank you very much for the reply, I figured out I need a server program and now I use wampserver.I'm a newb so there is no way I could knew about that.
    So anyway i modified the code for a couple of days but with my limited knowledge in php and java script I can't figure out how to prevent users from voting more then once with the ip algorithm.I need an algorithm to check if a given ip have voted and if it did, i need to show up results directly, not showing any messages.
    I am trying with cookies right now, cuz it seems to be easier and i have lost too much time already so i would be happy on any sort of solution that prevents double voting at least on 1-rst level:
    PHP Code:
    <?php
    error_reporting
    (E_ALL E_NOTICE E_DEPRECATED);

    $vote $_REQUEST['vote'];
    //get content of textfile
    $filename "poll_result.txt";
    $content file($filename);

    //put content in array
    $array explode("||"$content[0]);
    $yes $array[0];
    $no $array[1];

    if( isset( 
    $_COOKIE['vote']))
    {
    ?>
    <div id="quiz"><a href="#">Did you know...</a></div>
    <div id="quiz2">
    <h2 >Did you know Ring of Slaying has a right-click option to check kills?</h2>
    <table cellpadding="0px" cellspacing="0px" style="background:#CEB98E;font-weight:normal;" >
    <tr>
    <td>Yes:</td>
    <td>
    <img style="margin:0px;padding:0px;"src="poll.gif"
    width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
    height='20'>
    <?php echo(100*round($yes/($no+$yes),2)); ?>%
    </td>
    </tr>
    <tr>
    <td>No:</td>
    <td>
    <img src="poll.gif"
    width='<?php echo(100*round($no/($no+$yes),2)); ?>'
    height='20'>
    <?php echo(100*round($no/($no+$yes),2)); ?>%
    </td>
    </tr>
    </table> 
    </div>
    <?php

    }
    else{ 

    ?>
    <div id="poll">
    <div id="quiz"><a href="#">Did you know...</a></div>
    <div id="quiz2">
    <h2>Ring of Slaying has a right-click option to check kills?</h2>

    <a name="vote" href="javascript:getVote(0)">Yes</a>

    <a name="vote" href="javascript:getVote(1)">No</a>

    </div>
    </div>
    <?php
    }


    ?>
    javascr. file:
    Code:
    function commonfunca(){
    setcookie(vote);
    getVote(0)
    }
    
    function commonfuncb(){
    setcookie(vote);
    getVote(1)
    }
    
    function setcookie ("vote", "vote", )
    var xmlhttp;
    
    function getVote(int)
    {
    xmlhttp=GetXmlHttpObject();
    if (xmlhttp==null)
      {
      alert ("Browser does not support HTTP Request");
      return;
      }
    var url="poll_vote.php";
    url=url+"?vote="+int;
    url=url+"&sid="+Math.random();
    xmlhttp.onreadystatechange=stateChanged;
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
    function stateChanged()
    {
      if (xmlhttp.readyState==4)
      {
      document.getElementById("poll").innerHTML=xmlhttp.responseText;
      }
    }
    
    function GetXmlHttpObject()
    {
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
      {
      objXMLHttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    return objXMLHttp;
    }
    my idea is to set commonfunca() which start the two functions getvote and setcookie and after that to check with php if the cookie was set and show the results if it is.
    But the problem is that I can't figure out how to set a cookie in javascript
    I would be very happy if somebody can replace the example with real values which I can replece with my own, cuz I don't know if javascript want quotations, or no quotations:
    Code:
    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
    }
    thanks and sorry for the stupid question.
    Last edited by Snookerman; 12-30-2009 at 01:14 PM. Reason: please use [php] [/php] and [CODE] [/CODE] tags

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •