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 :)
graziaziAsked:
Who is Participating?
 
enachemcCommented:
use the === operator for comparing variables with types

$trig === 'true' //this means that the two variables are equal and of the same type (String)
$trig === true //will say yes only if $trig is a boolean with the value of true
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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

0
 
graziaziAuthor Commented:
Hi. i tried both suggestions. Unfortunately neither updates the column.

When I echo out the value of $ trig, it's always FALSE
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Lukasz ChmielewskiCommented:
Seems like a missing bracket

if (e.value == pr.value) {
             score +=20;
             trig = true;
             }
             else{
//             alert("Incorrect: Answer = "+e.value);
             trig = false;
                                 }
       }
0
 
Lukasz ChmielewskiCommented:
(in else in javascript) - the alert is the only condition for the else, then ALWAYS trig is set to false.
0
 
graziaziAuthor Commented:
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

0
 
graziaziAuthor Commented:
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
0
 
Lukasz ChmielewskiCommented:
So. What does the function return if one hundred checkboxes are checked and the last one is not checked ?
0
 
graziaziAuthor Commented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.