I have a script that takes ratings (1-5) for links. It works fine in the same browser, but I want to submit the rating to a small popup window. After searching low and high I found a JavaScript to submit the variables to the popup, and when I first coded it - it worked. I added some PHP and it died.
I started with this in the head:
<script language="JavaScript">
<!--
buttonClicked=false;
function myOpenWindow() {
myWindowHandle = window.open('about:blank','myWindowName','width=200,height=150');
}
//-->
</script>
and this in the body of my page:
<form method="POST" name="myForm" action="/test/rating2.php" target="myWindowName" onSubmit="if (!buttonClicked) return false">
<font face=Arial size=2><b>
Vote for this Site!
</b></font><br>
<select name="rating" size="1">
<option value="5" selected>5 - Superb
<option value="4">4 - Tasty
<option value="3">3 - Average
<option value="2">2 - Bland
<option value="1">1 - Rotten
</select>
<input type="hidden" name="item_id" value="anigif_1">
<input type="button" value="Vote" onClick="myOpenWindow(); buttonClicked=true; setTimeout('document.myForm.submit()',500)">
</form>
here is the code for the popup that intercepted the form submit:
<?
if ($REQUEST_METHOD=="POST") {
$usr = "user";
$pwd = "password";
$db = "database";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$SQL = " INSERT INTO ratings ";
$SQL = $SQL . " (id, item, value) VALUES ";
$SQL = $SQL . " (NULL,'$item_id','$rating') ";
$result = mysql_db_query($db,"$SQL",$cid);
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
mysql_close($cid);
}
?>
<?
if ($rating) {
if (!is_numeric($rating)) {print ("<font face=Arial size=2>Error in Vote</font>\n"); die();}
else
echo ("<font face=Arial size=2><b>You successfully Voted!</b></font><br>\n");
}
$usr = "user";
$pwd = "password";
$db = "database";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$rating_item = "anigif_1";
$SQL = " SELECT * FROM ratings WHERE item = \"$rating_item\"";
$result = mysql_db_query($db,"$SQL",$cid);
$num_rows = mysql_num_rows( $result );
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
if ($num_rows==0) { echo ("<font face=Arial size=1>No Votes yet</font><br>"); }
else {
print "<font face=Arial size=1>$num_rows Votes, </font>";
$SQL2 = "SELECT AVG(value) AS avgRating FROM ratings WHERE item = \"$rating_item\"";
$result2 = mysql_db_query($db,"$SQL2",$cid);
if (!$result2) { echo("ERROR: " . mysql_error() . "\n$SQL2\n"); }
print "<font face=Arial size=1>Rating: " . mysql_result($result2,0) . "</font>";
}
mysql_close($cid);
?>
<br><br>
<form>
<input type="button" value="Close Window" onClick="window.close()">
</form>
I gotta say, it worked like a charm - "at first". Then I decided I wanted to print the number of votes and average on the main page too, so I coded this for the body portion of the main page:
<form method="POST" name="myForm" action="/test/rating2.php" target="myWindowName" onSubmit="if (!buttonClicked) return false">
<font face=Arial size=2><b>
Vote for this Site!
</b></font><br>
<select name="rating" size="1">
<option value="5" selected>5 - Superb
<option value="4">4 - Tasty
<option value="3">3 - Average
<option value="2">2 - Bland
<option value="1">1 - Rotten
</select>
<input type="hidden" name="item_id" value="anigif_eclipse">
<input type="button" value="Vote" onClick="myOpenWindow(); buttonClicked=true; setTimeout('document.myForm.submit()',500)">
<br>
<?
$usr = "user";
$pwd = "password";
$db = "database";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$rating_item = "anigif_eclipse";
$SQL = " SELECT * FROM ratings WHERE item = \"$rating_item\"";
$result = mysql_db_query($db,"$SQL",$cid);
$num_rows = mysql_num_rows( $result );
if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n"); }
if ($num_rows==0) { echo ("<font face=Arial size=1>No Votes yet</font><br>"); }
else {
print "<font face=Arial size=1>$num_rows Votes, </font>";
$SQL2 = "SELECT AVG(value) AS avgRating FROM ratings WHERE item = \"$rating_item\"";
$result2 = mysql_db_query($db,"$SQL2",$cid);
if (!$result2) { echo("ERROR: " . mysql_error() . "\n$SQL2\n"); }
print "<font face=Arial size=1>Rating: " . mysql_result($result2,0) . "</font>";
}
mysql_close($cid);
?>
</form>
As soon as I added this PHP (either inside OR outside of the </form> tag) I got a Javascript error "Error: object does not support this property or method".
I removed all of the PHP code from the page, and I still get the friggin' error (yes, I checked the cache and source, the updated page was what I was looking at).
Why did the PHP screw up the form submit to the new window? Is there a JavaScript error I don't see? I was working?? ANY help here would be great.
Also, I need to add in code to save the user's IP and eliminate double voting. Any takers for a little help there?