Php~Javascript~HTML Problem

Hello :slight_smile:

i really need help with javascript function and php.

javascript function in calculate.js–
function CalculateScore(){
var total = 0;
var radios = document.getElementsByTagName(‘input’);
var value = 0;
for (i = 0; i < radios.length; i++) {
if (radios[i].type === ‘radio’ && radios[i].checked) {
value = radios[i].value;
total = (total + value * 1);
}
}
rpt=confirm(“Your score is : “+ total +”\n\nReset form ?”);
}

this function works in html file that i am not allowed to change… so i have to use another php file to get the total scores.

when i call this function in php like
echo "";

it just always shows me confirm box with score = 0.

is there any way i can get “total scores” in some php variable(in php file). instead of using confirm box.
i am really stuck…

Thanks!

I’m thinking the javascript fires based on radio checked?
You dont need to echo the just include it in the header of the php file.

As far as getting the score back out of javascript to use in php, the events happen at different times.
Page loads, php does its thing. Then user clicks radio box, it calculates the score and spits it back. Php has already done its dash.

Your best way would be to remake the javascript to update a

tag based on the return value.

Thanks for reply :slight_smile:
yes js function is for radio buttons… all radio buttons are in html file.
can you please give me some example or give me some useful links that can help.

just use document.write() instead of confirm. that should put it anywhere u want.

Or use

function CalculateScore(){ var total = 0; var radios = document.getElementsByTagName('input'); var value = 0; for (i = 0; i < radios.length; i++) { if (radios.type === 'radio' && radios.checked) { value = radios.value; total = (total + value * 1); } } document.getElementById('score').innerHTML = 'Total Score : ' + total; }

Where score is the ID

Thank you so much this code works…
document.getElementById(‘score’).innerHTML = 'Total Score : ’ + total;

last question hopefully :slight_smile: :smiley:
i wrote

some text


in html file(that i am not allowed to change).

but its not working when i put it in php file. why is that?

Have you attached the correct javascript link or included the javascript code in the php file?

There is no difference (in the eyes of html and javascript) between a .html and .php file.

Sponsor our Newsletter | Privacy Policy | Terms of Service