Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Jednostavna Ajax anketa
Author Message
stefendoza Offline
Redoviti posjetitelj
**

Posts: 81
Joined: Apr 2010
Reputation: 1
Post: #1
Jednostavna Ajax anketa
Ima neko ideju kako da rjesim ovaj problem da korisnik moze da glasa samo jednom, ali moram ovako preko txt fajlova. Ja sam uspio da upisem u fajl IP adresu korisnika, uporedim da li je ta ip adresa vec postoji u fajlu i da ispisem poruku da je korisnik vec glasao. Problem se javlja da kako da korisniku ostampam samo rezultat ako je glasao a ako nije da mu stampam formu da glasa.Zna neko kako ovo da rjesim. Hvala unaprijed


poll_vote.php
Code:
<?php
$vote = $_REQUEST['vote'];

//Hvatanje IP adresa  i upis u datoteku
if (!empty($_SERVER['HTTP_CLIENT_IP']))  
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
$imefajla="voters.txt";
$fp= fopen($imefajla,"w");
fputs($fp, $ip."\n");



//get content
$filename = "poll_result.txt";
$content = file($filename);

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

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);

?>



<h2>Result:</h2>
<table>
<tr>
<td>Yes:</td>
<td>
<img 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>

anketa.php
Code:
<html>
<head>
<script type="text/javascript" src="poll.js"></script>
</head>
<body>

<?php

//Hvatanje IP adrese
     if (!empty($_SERVER['HTTP_CLIENT_IP']))  
        {
          $IP=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))  
          $IP=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          $IP=$_SERVER['REMOTE_ADDR'];
        }

        $fp = fopen("voters.txt",'r');
        
        while (!feof($fp))
        {
                        //OVAJ DEO NE RADI,TJ TREBA OBEZBEDITI KADA JE OVAJ USLOV ISPUNJEN DA SE STAMPA KOMENTAR 'Vec ste glasali' I REZULTAT ANKETE U SUPROTNOM DA SE STAMPA ANKETA ZA GLASANJE
            $ip=fgets($fp);
            if($IP=$ip)
            {
                echo'Vec ste glasali <p>';
                
            }
            if($IP!=$ip)
            {
            echo'';
            }
        
        }
        

?>

<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
Yes:
<input type="radio" name="vote" value="0" onclick="getVote(this.value)" />
<br />No:
<input type="radio" name="vote" value="1" onclick="getVote(this.value)" />
</form>
</div>

</body>
</html>
(This post was last modified: 09-12-2010 05:16 PM by stefendoza.)
09-12-2010 05:15 PM
Find all posts by this user Quote this message in a reply
zero Offline
Forumaš
***

Posts: 461
Joined: Jan 2008
Reputation: 10
Post: #2
RE: Jednostavna Ajax anketa
Ne znam zasto kazes da si je ti napisao, kada sam ja prije par godina koristio tu istu skriptu.

Brzo. Jeftino. Kvalitetno. Izaberi dva!
09-12-2010 07:44 PM
Find all posts by this user Quote this message in a reply
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #3
RE: Jednostavna Ajax anketa
PHP Code:
if($IP=$ip

bi trebalo biti

PHP Code:
if($IP==$ip

ostalo nisam ni gledao inače = je operator pridruživanja, a == operator usporedbe

"When all else fails, read the manual."
(This post was last modified: 09-12-2010 08:24 PM by danielvast.)
09-12-2010 08:22 PM
Find all posts by this user Quote this message in a reply
stefendoza Offline
Redoviti posjetitelj
**

Posts: 81
Joined: Apr 2010
Reputation: 1
Post: #4
RE: Jednostavna Ajax anketa
zero Wrote:Ne znam zasto kazes da si je ti napisao, kada sam ja prije par godina koristio tu istu skriptu.

Kao prvo nisam nigdje rekao da sam je ja napravio sto ce reci da LAZES.
Kao drugo upisivanje IP adrese u txt i fajl,prolazak kroz fajl i uporedjivanje IP adresa sam ja licno dopisao tako da ako cemo da cepidlacimo, istu skriptu opet nisi mogao da koristis i ispade da opet LAZES. Jeli tako?
Kao trece ova skripta ima da se skine sa jedno 15-ak sajtova koliko sam skontao i sta sa tim???

#danielvast, tacno je da je i tu greska ali to opet nece rjesiti problem.
10-12-2010 02:10 AM
Find all posts by this user Quote this message in a reply
kecko Offline
Forumaš
***

Posts: 645
Joined: Nov 2009
Reputation: 20
Post: #5
RE: Jednostavna Ajax anketa
Ovo bi moglo bit zanimljivo :)))
10-12-2010 08:44 AM
Find all posts by this user Quote this message in a reply
stefendoza Offline
Redoviti posjetitelj
**

Posts: 81
Joined: Apr 2010
Reputation: 1
Post: #6
RE: Jednostavna Ajax anketa
Kako kome, meni ne :P
11-12-2010 02:19 AM
Find all posts by this user Quote this message in a reply
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #7
RE: Jednostavna Ajax anketa
A da nam das sta pise u datoteci <<voters.txt>> ili da ispises varijablu $ip i visdis sta ti ispisuje, malo jeste tesko raditi debugging u PHP-u ali moras se nekeko snalaziti.

"When all else fails, read the manual."
(This post was last modified: 11-12-2010 02:00 PM by danielvast.)
11-12-2010 02:00 PM
Find all posts by this user Quote this message in a reply
stefendoza Offline
Redoviti posjetitelj
**

Posts: 81
Joined: Apr 2010
Reputation: 1
Post: #8
RE: Jednostavna Ajax anketa
Pa znas sta u voters se upise Ip adresa posjetioca stranice. Posto radim u lokalu upise se 127.0.0.1. Nije to problem to radi oko upisa u fajl i ovaj dio za uporedjivanje ip adresa, ali meni treba sad kad ja ustanovim da ako je neko glasao sa 127.0.0.1 i ako opet otvori anketu sa te adrese da mu ispise poruku da je on vec glasao i da prikaze samo rezultat. Znaci gledaj ovde:

Code:
$ip=fgets($fp);
            if($IP==$ip)
            {
                echo'Vec ste glasali <p>';
                //OVDE TREBA OBEZBJEDITI STAMPAJNE REZULTATA
                
            }
            if($IP!=$ip)
            {
            //OVDE TREBA PONOVO STAMPATI ANKETU DA KORISNIK GLASA
            }
11-12-2010 02:13 PM
Find all posts by this user Quote this message in a reply
danielvast Offline
Forumaš
***

Posts: 432
Joined: Dec 2009
Reputation: 22
Post: #9
RE: Jednostavna Ajax anketa
Pa onda u cemu je problem jel radi if sto bi trebao raditi?

Edit mislim da bi to trebalo izgledati ovako::

PHP Code:
<html>
<
head>
<
script type="text/javascript" src="poll.js"></script>
</head>
<body>

<?php

//Hvatanje IP adrese
     
if (!empty($_SERVER['HTTP_CLIENT_IP']))   
        {
          
$IP=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty(
$_SERVER['HTTP_X_FORWARDED_FOR']))  
          
$IP=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
          
$IP=$_SERVER['REMOTE_ADDR'];
        }

        
$fp fopen("voters.txt",'r');
        
$ips = array();
        while (!
feof($fp))
        {
        
//OVAJ DEO NE RADI,TJ TREBA OBEZBEDITI KADA JE OVAJ USLOV ISPUNJEN DA SE STAMPA KOMENTAR 'Vec ste glasali' I REZULTAT ANKETE U SUPROTNOM DA SE STAMPA ANKETA ZA GLASANJE 
            
$ip=fgets($fp);
            
array_push($ips,$ip);
        }
        if(
in_array($IP$ips))
        {
             echo 
'Vec ste glasali!';
        }
        else
        {
            echo 
'<div id="poll">
                     <h3>Do you like PHP and AJAX so far?</h3>
                     <form>
                      Yes:
                     <input type="radio" name="vote" value="0" onclick="getVote(this.value)" />
                     <br />No:
                     <input type="radio" name="vote" value="1" onclick="getVote(this.value)" />
                     </form>
                     </div>'
;
        }
?>
</body>
</html> 

Ako ima gresaka popravi ovo sam ovdje napisao.

"When all else fails, read the manual."
(This post was last modified: 11-12-2010 03:00 PM by danielvast.)
11-12-2010 02:19 PM
Find all posts by this user Quote this message in a reply
stefendoza Offline
Redoviti posjetitelj
**

Posts: 81
Joined: Apr 2010
Reputation: 1
Post: #10
RE: Jednostavna Ajax anketa
Sta ti radi ova in_array() ?
11-12-2010 10:31 PM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)