graziazi
asked on
If and else logic do the same thing
Hi. I am trying to update a table in PHP on the server side if it meets a condition. The column I am updating is a 'players total score'.
So on the client side I have a javascript var trig. See the code below
client side
So I pass the value of trig over to the server. Belowis logic which I want to update the table appropriately.
server side
$val1 = 20
$val2 = 0
The problem I have is, both conditions update the column by the same value i.e. 20
ps. please go easy on me with my logic, I'm not a natural programmer :)
So on the client side I have a javascript var trig. See the code below
client side
if (e.value == pr.value) {
alert("Correct");//radioButtons[x].id);
score +=20;
[b]trig = true;[/b]
}
else
alert("Incorrect: Answer = "+e.value);
[b]trig = false;[/b]
}
So I pass the value of trig over to the server. Belowis logic which I want to update the table appropriately.
server side
if ($trig == true)
{
mysql_query("UPDATE user SET total = '$scoretown' + '$val1' WHERE username = '$user'");
}
else if($trig == 'false')
mysql_query("UPDATE user SET total = '$scoretown' + '$val2' WHERE username = '$user'");
$val1 = 20
$val2 = 0
The problem I have is, both conditions update the column by the same value i.e. 20
ps. please go easy on me with my logic, I'm not a natural programmer :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi. i tried both suggestions. Unfortunately neither updates the column.
When I echo out the value of $ trig, it's always FALSE
When I echo out the value of $ trig, it's always FALSE
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
(in else in javascript) - the alert is the only condition for the else, then ALWAYS trig is set to false.
ASKER
Here's the whole function ........
function getCheckedRadio() {
var radioButtons = document.getElementsByName("img");
for (var x = 0; x < radioButtons.length; x ++) {
if (radioButtons[x].checked) {
var d = radioButtons[x].id;
var e = document.getElementById(d);
// u = document.getElementById('pr');
f = e.value;
i = pr.value;
if (e.value == pr.value) {
alert("Correct");//radioButtons[x].id);
score +=20;
trig = true;
}
else {
//alert("Incorrect: Answer = "+e.value);
trig = false;
}
}
}
}
ASKER
Ok, roads, I tried your suggestion and I get TRUE and FALSE where appropriate, but the table is not updating. enachemc: I also implemented your logic
So. What does the function return if one hundred checkboxes are checked and the last one is not checked ?
ASKER
Ah it works. I implemented your code roads and the this part of your code enachemc
Also thanks angellll, I understand your logic
$trig === 'true' //
Also thanks angellll, I understand your logic
Open in new window