Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8711495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:53:33+00:00 2026-06-13T04:53:33+00:00

why i see this error message ( ! ) Notice: Undefined index: poll in

  • 0

why i see this error message “( ! ) Notice: Undefined index: poll in D:\wamp\www\poll\poll.php on line 6” in the ajax when i try to poll .. i have two files

1- index.html contains get request

2- poll.php file to select request and display result

index.html

<!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"  dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>AJAX Poll Using jQuery and PHP</title>

<link rel="stylesheet" type="text/css" href="styles.css" />
<script type="text/javascript" src="js/jquery-1.3.2.js" ></script>
<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>
</head>
<body>
    <div id="container" >
        <h1>User Poll</h1>
        <div id="pollcontainer" >
        </div>
        <p id="loader" >Loading...</p>
    </div>
</body>
</html>

poll.php

  <?php
    //Update database information according to your server settings
    $conn=mysql_connect('localhost', 'root', '') or die("Can't connect to mysql host");
    //Select the database to use
    mysql_select_db('ahmed') or die("Can't connect to DB");
    if(!$_POST['poll'] || !$_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($_GET["result"]==1 || $_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{
        if($_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']));
    }
    function showresults($poll_id){
        global $conn;
        $query=mysql_query("SELECT COUNT(*) as totalvotes FROM votes WHERE option_id IN(SELECT id FROM options WHERE ques_id='$poll_id')");
        while($row=mysql_fetch_assoc($query))
            $total=$row['totalvotes'];
        $query=mysql_query("SELECT options.id, options.value, COUNT(*) as votes FROM votes, options WHERE votes.option_id=options.id AND votes.option_id IN(SELECT id FROM options WHERE ques_id='$poll_id') GROUP BY votes.option_id");
        while($row=mysql_fetch_assoc($query)){
            $percent=round(($row['votes']*100)/$total);
            echo '<div class="option" ><p>'.$row['value'].' (<em>'.$percent.'%, '.$row['votes'].' votes</em>)</p>';
            echo '<div class="bar ';
            if($_POST['poll']==$row['id']) echo ' yourvote';
            echo '" style="width: '.$percent.'%; " ></div></div>';
        }
        echo '<p>Total Votes: '.$total.'</p>';
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:53:35+00:00Added an answer on June 13, 2026 at 4:53 am

    Change line 6 of poll.php from:

    if(!$_POST['poll'] || !$_POST['pollid']){
    

    to:

    if(!isset($_POST['poll']) || !isset($_POST['pollid'])){
    

    As written, it assumes the existence of $_POST['poll'] and is conditioned on its value being null, false or 0. In the case where the value simply does not exist, the error that you are seeing is the result. The solution is to confirm that the variable has been set at all using isset().

    Also, change line 15 from:

    if($_GET["result"]==1 || $_COOKIE["voted".$poll_id]=='yes'){
    

    to

    if((isset($_GET["result"]) && $_GET["result"] == 1) || (isset($_COOKIE["voted"  .$poll_id]) && $_COOKIE["voted" . $poll_id] == 'yes')){
    

    Similarly, as written, it assumes the existence of $_GET['result'] and is conditioned on its value being 1. In the case where the value simply does not exist, the error that you are seeing is the result. The solution is again to first confirm that the variable has been set at all using isset() and then check its value.


    See: http://php.net/manual/en/function.isset.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see a number of other people asking about this error message in other
When I check to see if my error message is working correctly, I notice
After about 90 seconds I see this error in my apache error log. I'm
In my unhandled exception logging I see this error sporadically through the day on
I include this library: #include <QtCopyDialog> When i compile, i see this error QtCopyDialog:
From times to times, while debugging an Application, I see this error on Xcode:
see this fiddle . This upon running gives an error in console. I'm currently
See also SO 1664677 I get this error 2: Syntax error: ( unexpected when
I see that many people get this error, but their situations all appear a
Please see this example: if (strlen($_SESSION['userDetails']['THISNAME']) == 0) { $_SESSION['userDetails'][''.$THISNAME.'Error'] = autofocus; include $docRoot/html/forms/reg/user_info.html.php;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.