Jump to content

AJAX POLL + PHP Issue


mjurmann

Recommended Posts

Hello there everyone. I need help!

I'm trying to use an AJAX Poll that uses PHP and MySQL to gather the poll info and then shoot back the results.

Problem is, when you use the poll in its own PHP file, it works fine. Example at: http://www.mattjurmann.com/temp/gameargusmedia/poll/ajax-poller.php

However, I am trying to use a PHP include on my main index page of my website located here: http://www.mattjurmann.com/temp/gameargusmedia/index.php

If you scroll down to the bottom of the right column, you can see the poll. Now the information there is included from the same file that the link above works perfectly at. However, if you make a selection on the index page and then click "Vote", you can see that the behavior is not the same. It simply takes you to the top of the page and does nothing.

Does anyone have any idea why this is not working in an include in my index file but working in the file itself?

Here is the code for the file:

[code]<?php

require_once("dbConnect.php");

?>

<HTML>
<HEAD>
<?php
@include($_SERVER['DOCUMENT_ROOT']."/config/metatags.inc");
?>
<title>Ajax poller</title>



<link rel="stylesheet" href="css/ajax-poller.css" type="text/css">
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajax-poller.js"> </script>

</HEAD>
<BODY>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" onSubmit="return false" method="post">


<?php
$pollerId = 1; // Id of poller
?>
<!-- START OF POLLER -->
<div class="poller">

<div class="poller_question" id="poller_question<?php echo $pollerId; ?>">
<?php


// Retreving poll from database
$res = mysql_query("select * from poller where ID='$pollerId'");
if($inf = mysql_fetch_array($res)){
echo "<p class=\"pollerTitle\">".$inf["pollerTitle"]."</p>"; // Output poller title

$resOptions = mysql_query("select * from poller_option where pollerID='$pollerId' order by pollerOrder") or die(mysql_error()); // Find poll options, i.e. radio buttons
while($infOptions = mysql_fetch_array($resOptions)){
if($infOptions["defaultChecked"])$checked=" checked"; else $checked = "";
echo "<p class=\"pollerOption\"><input$checked type=\"radio\" value=\"".$infOptions["ID"]."\" name=\"vote[".$inf["ID"]."]\" id=\"pollerOption".$infOptions["ID"]."\"><label for=\"pollerOption".$infOptions["ID"]."\" id=\"optionLabel".$infOptions["ID"]."\">".$infOptions["optionText"]."</label></p>";

}
}
?>
<br/><a href="#" onClick="castMyVote(<?php echo $pollerId; ?>,document.forms[0])"><img src="images/button_vote.jpg" border="0"></a>
</div>
<div class="poller_waitMessage" id="poller_waitMessage<?php echo $pollerId; ?>">
Getting poll results. Please wait...
</div>
<div class="poller_results" id="poller_results<?php echo $pollerId; ?>">
<!-- This div will be filled from Ajax, so leave it empty --></div>
</div>
<!-- END OF POLLER -->
<script type="text/javascript">
if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<?php echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<?php echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address

}
</script>
</form>

</BODY>
</HTML>[/code]

Thank you in advance.
Link to comment
https://forums.phpfreaks.com/topic/32831-ajax-poll-php-issue/
Share on other sites

You probably having a path problem.  It seems like the page at http://www.mattjurmann.com/temp/gameargusmedia/index.php can not load the js/ajax-poller.js

you need to either place the js file at this location
http://www.mattjurmann.com/temp/gameargusmedia/js/*.js
or modify the .js path inside your index.php
Link to comment
https://forums.phpfreaks.com/topic/32831-ajax-poll-php-issue/#findComment-152913
Share on other sites

I checked the html source code of the page at:
http://www.mattjurmann.com/temp/gameargusmedia/index.php
--there are 2 HTML tags, 2 HEAD tags, etc. in the html source code.  The second HTML tag starts where the poll code starts near the end of the page.  Your polling 'include' file contains an entire html page which is then included in another entire html page creating an invalid html page.
Link to comment
https://forums.phpfreaks.com/topic/32831-ajax-poll-php-issue/#findComment-153018
Share on other sites

[quote author=hvle link=topic=120972.msg496829#msg496829 date=1167920853]
You probably having a path problem.  It seems like the page at http://www.mattjurmann.com/temp/gameargusmedia/index.php can not load the js/ajax-poller.js

you need to either place the js file at this location
http://www.mattjurmann.com/temp/gameargusmedia/js/*.js
or modify the .js path inside your index.php
[/quote]

Well, I cleaned up the HTML and BODY tags from my include file, so that's fine. However, its still anchoring at the top of the index page when I click on the VOTE button instead of loading the JS file. Do I need to have the JS files somewhere in the INCLUDE page itself, or is it fine if I just have them in the index page like I do now? Any other problems anyone can see? Sigh... lol

I forgot to mention that the index.php file and the included file that is ajax-poller.php are located in the same directory. So by referring to the js/ajax-poller.js file, the path should be correct.

Here is the code at the beginning of the index page:

[code]<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>game.argus | media</title>
<link href="css/gameargus_styles.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="css/ajax-poller.css" type="text/css">
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajax-poller.js"> </script>

</head>

<body>
[/code]

And here is the code from the INCLUDE file (basically the same as before except I got rid of the BODY and HTML tags):

[code]<?php

require_once("dbConnect.php");

?>


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" onSubmit="return false" method="post">


<?php
$pollerId = 1; // Id of poller
?>
<!-- START OF POLLER -->
<div class="poller">

<div class="poller_question" id="poller_question<?php echo $pollerId; ?>">
<?php


// Retreving poll from database
$res = mysql_query("select * from poller where ID='$pollerId'");
if($inf = mysql_fetch_array($res)){
echo "<p class=\"pollerTitle\">".$inf["pollerTitle"]."</p>"; // Output poller title

$resOptions = mysql_query("select * from poller_option where pollerID='$pollerId' order by pollerOrder") or die(mysql_error()); // Find poll options, i.e. radio buttons
while($infOptions = mysql_fetch_array($resOptions)){
if($infOptions["defaultChecked"])$checked=" checked"; else $checked = "";
echo "<p class=\"pollerOption\"><input$checked type=\"radio\" value=\"".$infOptions["ID"]."\" name=\"vote[".$inf["ID"]."]\" id=\"pollerOption".$infOptions["ID"]."\"><label for=\"pollerOption".$infOptions["ID"]."\" id=\"optionLabel".$infOptions["ID"]."\">".$infOptions["optionText"]."</label></p>";

}
}
?>
<br/><a href="#" onClick="castMyVote(<?php echo $pollerId; ?>,document.forms[0])"><img src="images/button_vote.jpg" border="0"></a>
</div>
<div class="poller_waitMessage" id="poller_waitMessage<?php echo $pollerId; ?>">
Getting poll results. Please wait...
</div>
<div class="poller_results" id="poller_results<?php echo $pollerId; ?>">
<!-- This div will be filled from Ajax, so leave it empty --></div>
</div>
<!-- END OF POLLER -->
<script type="text/javascript">
if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<?php echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<?php echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address

}
</script>
</form>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/32831-ajax-poll-php-issue/#findComment-153067
Share on other sites

I got it, your passing document.forms[0], in your test poll page, which is the only form. Your index page has 3 forms, therefor you need to pass document.forms[2] as the argument AND copy over the images so they load.

::Edit

Also, the reason why the browser gets taken to the top of the site is because the href is set to #. Just simply take out the href attribute and your good to go.


Do I get paid for this?  :P
Link to comment
https://forums.phpfreaks.com/topic/32831-ajax-poll-php-issue/#findComment-153173
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.