Link to home
Start Free TrialLog in
Avatar of graziazi
graziaziFlag for Afghanistan

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
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]	 
       }

Open in new window


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'");

Open in new window


$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 :)
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you could simplify your code like this:

$value = $trig ? $val1 : $val2;
mysql_query("UPDATE user SET total = $scoretown + $value WHERE username = '$user' ");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of enachemc
enachemc
Flag of Afghanistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of graziazi

ASKER

Hi. i tried both suggestions. Unfortunately neither updates the column.

When I echo out the value of $ trig, it's always FALSE
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(in else in javascript) - the alert is the only condition for the else, then ALWAYS trig is set to false.
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;
		 
       }
      
    }

}
}

Open in new window

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 ?
Ah it works. I implemented your code roads and the this part of your code enachemc
$trig === 'true' //

Open in new window


Also thanks angellll, I understand your logic