We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

If and else logic do the same thing

graziazi
graziazi asked
on
Medium Priority
344 Views
Last Modified: 2012-05-11
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 :)
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
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

Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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

When I echo out the value of $ trig, it's always FALSE
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
(in else in javascript) - the alert is the only condition for the else, then ALWAYS trig is set to false.

Author

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

Author

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
So. What does the function return if one hundred checkboxes are checked and the last one is not checked ?

Author

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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.