php: transfer a variable from one page to another & setup cookie

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cristibodi
    New Member
    • May 2010
    • 2

    php: transfer a variable from one page to another & setup cookie

    I am trying to set-up polls for almost all the pages.

    After you submit the response I want to get back to the original page and instead of the form to show the poll results.

    Also, I want to set-up a cookie to make sure that the same person does not vote twice.

    There are 3 pages involved and one script file:
    • main_page.php
    • poll_vote.php
    • poll_results.ph p


    I want all the polls to use the same poll_vote.php and return "OK"/data has been saved to each page that requested poll_vote.php.

    I need help because I can not make it work properly.

    Here are the codes:

    main_page.php
    Code:
    <?php
    session_start();
    
    $PollIDini = 5;
    $pollIDvoted = "votedPollID" . $PollIDini;
    
    $cookie = "CookiePoll" . $PollIDini;
    $voted = "VotedPoll" . $PollIDini;
    $expirein=time()+1200000;
    
    if(isset($_SESSION[$pollIDvoted]))
    {
    	if($_SESSION[$pollIDvoted]==100)
    	{
    	setcookie($cookie, $voted, $expirein);
    	session_destroy();
    	}
    }
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    
    <body>
    
    <div id="SurveyFR">
    
    <p class="polltitle">Rate this calculator</p>
    <form name="survey" id="survey" method="post" enctype="multipart/form-data" accept-charset="UTF-8" action="../php_files/poll_vote.php?IDpoll=<?php echo $PollIDini ?>&IDorigpage=<?php echo $activePageURL ?>">
    <ul>
    <li><input type="radio" name="Survey" id="Survey" value = "1" /> Very helpful</li>
    <li><input type="radio" name="Survey" id="Survey" value = "2" /> Helpful</li>
    <li><input type="radio" name="Survey" id="Survey" value = "3" /> Somehow helpful</li>
    <li><input type="radio" name="Survey" id="Survey" value = "4" /> Not at all</li>
    <li><input type="radio" name="Survey" id="Survey" value = "5" /> Not what I need</li>
    <li class="tocenter">
    <input name="submit" id="submit" type="submit" class="btn" style="width:69px; height:29px; background-image:url(../images/tools/vote.jpg); background-repeat:no-repeat; border:none" value="" />
    <input name="Results" id="Results" type="button" class="btn" style="width:69px; height:29px; background-image:url(../images/tools/results.jpg); background-repeat:no-repeat; border:none" value="" onclick="ShowPollResults ()" />
    </li>
    </ul>
    </form>
    
    </div>
    
    	<div id="SurveyResults" style="display:none">
    	
    	<?php
    	include ("../php_files/poll_results.php");
    	?> 
    	
    	</div>
    
    <?php
    if ($_SESSION[$pollIDvoted]==100)
    	{
    echo "<script type='text/javascript'>ShowPollResults ()</script>";
    	}
    ?> 
    
    </body>
    </html>
    poll_vote.php
    Code:
    <?php
    
    $pollID = $_GET['IDpoll'];
    $pollIDvoted = "votedPollID" . $pollID;
    
    if(isset($_SESSION[$pollIDvoted]))
    {
    $_SESSION[$pollIDvoted]=200;
    }
    else
    {
    $_SESSION[$pollIDvoted]=100;
    }
    
    header('Location: ' . $_GET['IDorigpage'] . '');
    
    $host="localhost"; // Host name 
    $usernamec="user"; // Mysql username 
    $passwordc="password"; // Mysql password 
    $db_name = "polls"; // Database name 
    $tbl_name = "votes"; // Table name 
    
    $pollOption = $_POST['Survey'];
    
    $conpoll = mysql_connect($host, $usernamec, $passwordc);
    
    if (!$conpoll)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db($db_name, $conpoll)or die("cannot select DB");
    
    if ($pollOption!=0)
    {
    	$sqlpoll="INSERT INTO votes (Poll_ID, Poll_Option)
    	VALUES ('$pollID','$pollOption')";
    	
    	if (!mysql_query($sqlpoll,$conpoll))
    	  {
    	  die('Error: ' . mysql_error());
    	  }
    }
    	
    mysql_close($conpoll)
    
    ?>

    external script
    Code:
    function ShowPollResults () { 
    		
    	var element1 = document.getElementById('SurveyFR');
    	var element2 = document.getElementById('SurveyResults');
    	
    	element2.style.display = "block";
    	element1.style.display = "none";
    
    }
    poll_results.ph p just takes dat from MySql DB and show the results. It works fine.

    What should I do/change in poll_vote.php and main_page.php?
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    #2
    this code below is what i use to set the returnURL of my original page

    Code:
    $returnURL .= 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    ... on your main page with the form for starters you should include the pollID within a hidden field... it will pass to the next page as $_POST rather than $_GET, also you should include the return url in the form as a hidden field so it passes that to the next page too. you can then use this url and add any vars and value to it to get back to the page you want it to... this will then lead onto displaying what you want it to.

    you could set a certain var that is sent back to the original page when you record if a person has voted. and on the main page if it returns and they have it will display the results or whatever you wish

    hope this is helpful

    Comment

    Working...