poutyboi
poutyboi

Reputation: 149

ajax php javascript php page open, parameters not being passed

I am having trouble pass some values to a php page, don't know whats wrong, just know the error occurs at xmlhttp.open. tried lots of stuff, cant figure it out.

heres the code: html:

<html>
<head>
<script>
function getVote(i, j) {

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    } else {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            document.getElementById("poll").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","poll_vote.php?vote1="+i+"&vote2="+j,true);
    xmlhttp.send();
    //document.write(i + ' ' + j);
}
</script>
</head>
<body>
<div id="poll">
<h3>Are you a skier, or do you get stout on the board mon? Either way, lets see if shred...</h3>
<form>
What do you do?<br>
Ski:
<input type="radio" name="vote1" id="vote1" value="0" >
<br>Snowboard:
<input type="radio" name="vote1" id="vote1" value="1">
<br>Dream of getting rad:
<input type="radio" name="vote1" id="vote1" value="2">
<br>
Do you watch other people hit the jump first?<br>
Yes:
<input type="radio" name="vote2" id="vote2" value="0" onclick="var tmpora = document.getElementById('vote1').value; getVote(tmpora, this.value);">
<br>No:
<input type="radio" name="vote2" id="vote2" value="1" onclick="var tmpora = document.getElementById('vote1').value; getVote(tmpora, this.value);">
<br>
</form>
</div>
</body>
</html>

and php:

<?php
$vote1 = $_REQUEST['vote1'];
$vote2 = $_REQUEST['vote2'];
$filename = "poll_result.txt";
$content = file($filename);    
$array = explode("||", $content[0]);
$ski = $array[0];
$board = $array[1];
$no = $array[2];
$radski = $array[3];
$radboard = $array[4];
if ($vote1 == 0) {
    $ski = $ski + 1;
} else if ($vote1 == 1) {
    echo "howdyyy";
    $board = $board + 1;
} else if ($vote1 == 2) {
    $no = $no + 1;
}
if ($vote2 == 0) {
    if ($ski == 1) {
        $radski = $radski + 1;
    } else if ($board == 1) {
        $radboard = $radboard + 1;
    } else if ($no == 1) {
    }
    $no = $no + 1;
} else if ($vote2 == 1) {
}
$insertvote = $ski."||".$board"||".$no"||".$radski"||".$radboard;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h2>Result:</h2>
<table>
<tr>
<td>Skiers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($ski/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($ski/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Snowboarders:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($board/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($board/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Dreamers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($no/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($no/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Rad Skiers:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($radski/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($radski/($ski+$board+$no),2)); ?>%
</td>
</tr>
<tr>
<td>Rad Snowboarders:</td>
<td>
<img src="poll.gif"
width='<?php echo(100*round($radboard/($ski+$board+$no),2)); ?>'
height='20'>
<?php echo(100*round($radboard/($ski+$board+$no),2)); ?>%
</td>
</tr>
</table>

and txt file like:

0||0||0||0||0

this is like a sample on w3schools but i am new and still learning and curious why my code isn't working now.

thanks to all who help!!

enjoy

Upvotes: 0

Views: 76

Answers (1)

Sandeep Nayak
Sandeep Nayak

Reputation: 4757

ok, got the bug.

Replace your code in line 30 in PHP file with this:

$insertvote = $ski."||".$board."||".$no."||".$radski."||".$radboard;

You had missed out the .(Append) symbol.

This the output I got. Ignore the Network error( I dont have the images)

enter image description here

Hope it helps!.

Upvotes: 2

Related Questions