Condition Always Returns The Same

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1055 Views - Last Post: 10 September 2011 - 02:38 PM Rate Topic: ***** 1 Votes

#1 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Condition Always Returns The Same

Posted 09 September 2011 - 12:59 PM

I dont really understand whats occuring here. i am checking to see the status of one variable. the entire script works without a hitch if all i do is print, but on the false condition i want it to throw the exception. but for some reason it always throws the exception if the variable is true or false
<html>
	<body>
		<?php require_once('Errors/Exceptions/JavascriptNotSupported.php'); ?>
		<?php
			$js = array_key_exists('js', $_GET) ? $_GET['js'] : '';
			if ($js == '')
			{
			   $js = 'off';
			?>
	    <form name="postJs" action="<?php echo $PHP_SELF ?>" method="get">
	    <input type="hidden" name="js" value="on">
	    </form>
	    <script type="text/javascript"><!--
	      document.postJs.submit()
	    //-->
	    </script>
			<?php
			}
			
			try
			{ 
				if($js="off")
				{
					throw new JavascriptNotSupported('No JS'); 
				}
				else
				{
					echo 'Javascript is enabled';
				}
			} 
			catch(Exception $e) 
			{ 
				echo $e; 
			}
			
			?>
	</body>
</html>


This post has been edited by Crazy_Learner: 10 September 2011 - 02:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Condition Always Returns The Same

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Condition Always Returns The Same

Posted 09 September 2011 - 01:01 PM

Yes, you're not checking the equality of $js to be "off" by doing this: if($js="off")

Since you're using a single =, you're just checking that you can assign "off" to $js, which you obviously can.

You should be using if ($js == "off").

EDIT: Ha, Mac, I beat you! AND I WROTE MORE :ph34r:'d

This post has been edited by RudiVisser: 09 September 2011 - 01:04 PM

Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: Condition Always Returns The Same

Posted 09 September 2011 - 01:02 PM

The = operator is for assignment, and the == operator is for comparisons. Hence, "off" is being assigned to $js, and $js is not evaluating to false.
if($js="off")


Was This Post Helpful? 1
  • +
  • -

#4 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 09 September 2011 - 01:06 PM

well that did it. i cant believe i did not notice that. such a novice mistake, makes me feel kind of worthless. oh well. thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Condition Always Returns The Same

Posted 10 September 2011 - 12:31 AM

thatís why you write such statements the other way round:
if ("value" == $var)

in case you write = instead of ==, you get an assignment error.
Was This Post Helpful? 0
  • +
  • -

#6 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 10 September 2011 - 01:39 AM

View PostDormilich, on 10 September 2011 - 01:31 AM, said:

thatís why you write such statements the other way round:
if ("value" == $var)

in case you write = instead of ==, you get an assignment error.


its a good idea, but i dont think that method would work for me. would just cuase me confusion when reading it.
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3554
  • View blog
  • Posts: 10,335
  • Joined: 08-June 10

Re: Condition Always Returns The Same

Posted 10 September 2011 - 01:47 AM

afaik, thatís the only way to prevent this kind of errors, so choose your poison.
Was This Post Helpful? 0
  • +
  • -

#8 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 10 September 2011 - 01:47 AM

The Issue
The issue is when i load a page a form automatically submits using Javascript and dependent on the result certain action is suppose to occur. To accomplish this i use an error handling system. However, it always executes the first exception even if the condition is false on load. But if i load the page with the variables being defined by me it works correctly and as expected.

Attempted Solutions
1) Removed general catch(Exception $e) block and added more specific catches
-- RESULT : No difference

2) Changed order of if statements checking the Get Variables
-- RESULT : No difference

3) Removed the thrown action to ensure if statements where being evaluated correctly
-- RESULT: They executed correctly, add exception to value that should return as 'on'; exception still occurs

4) Moved Thrown exceptions to separate functions.
-- RESULT: No difference

Validation Code:
<html>
	<body>
		<script type="text/javascript" src="../Detect/FlashDetection.js"></script>
		<?php require_once('Errors/Exceptions/JavascriptNotSupported.php'); ?>
		<?php require_once('Errors/Exceptions/FlashUpdateRequired.php'); ?>
		<?php
			$js = array_key_exists('js', $_GET) ? $_GET['js'] : '';
			$flash = array_key_exists('flash', $_GET) ? $_GET['flash'] : false;
			if ($js == '')
			{
			   $js = 'off';
			   $flash = false;
			?>
	    <form name="postJs" action="" method="get">
	    <input type="hidden" name="js" id="js" value="on">
   	    <input type="hidden" name="flash" id="flash" value="true">
	    </form>
	    <script type="text/javascript"><!--
	    	var flashTest = MeetsRequirement(10);
	    	document.getElementById('flash').value = flashTest;
	      	document.postJs.submit();
	    //-->
	    </script>
			<?php
			}
			
			try
			{ 
				echo $js ;
				echo '<br>' . $flash;
				
				if($flash == 'false' || $flash == false)
				{
					echo '<br> this executed flash';
					throw new FlashUpdateRequired('No Flash'); 
				}
				if($js=="off")
				{
					echo '<br> this executed js';
					throw new JavascriptNotSupported('No JS'); 
				}
				
			} 
			catch(JavascriptNotSupported $e) 
			{ 
				echo 'Executed Javascript Exception Handler: ' . '<br>' . $e;
			}
			
			catch(FlashUpdateRequired $e)
			{
				echo 'Executed Flash Exception Handler: ' . '<br>' . $e;
			}
			
			?>
	</body>
</html>



Exception For JS
	class JavascriptNotSupported extends Exception
	{
		/**
		 *
		 *
		 *
		 */
		public function __construct($message, $code = 0, Exception $previous = null) 
		{
			$this->Redirect();	      
    	}
    	
    	/**
    	 *
    	 *
    	 *
    	 */
    	public function __destruct()
    	{
    		 
    	}
    	
    	/**
    	 *
    	 *
    	 *
    	 */
   		private function Redirect()
    	{
    		header('HTTP/1.1 415 Unsupported Media Type');
    		header('Location: Errors/NoJavascript.html');
    	}
	}




Flash Exception
class FlashUpdateRequired extends Exception
	{
		/**
		 *
		 *
		 *
		 */
		public function __construct($message, $code = 0, Exception $previous = null) 
		{
			$this->Redirect();	 
    	}
    	
    	/**
    	 *
    	 *
    	 *
    	 */
    	public function __destruct()
    	{
    		 
    	}
    	
    	/**
    	 *
    	 *
    	 *
    	 */
    	private function Redirect()
    	{
    		header('HTTP/1.1 415 Unsupported Media Type');
    		header('Location: Errors/FlashVersionObsolete.html');
    	}
	}



This post has been edited by Crazy_Learner: 10 September 2011 - 03:36 AM

Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,549
  • Joined: 23-August 08

Re: Condition Always Returns The Same

Posted 10 September 2011 - 05:17 AM

Merged duplicate topics. When you already have a topic open on a problem, don't go and open a new one!
Was This Post Helpful? 0
  • +
  • -

#10 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,356
  • Joined: 15-February 11

Re: Condition Always Returns The Same

Posted 10 September 2011 - 05:28 AM

Only one exception can be thrown per try block and only one catch will execute. Whichever one throws an exception first gets caught.

While debugging you should always comment out redirects so you can properly see what's happening. Dump $flash and $js and post whatever you see on the screen.
Was This Post Helpful? 0
  • +
  • -

#11 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 10 September 2011 - 08:17 AM

View Postcodeprada, on 10 September 2011 - 06:28 AM, said:

Only one exception can be thrown per try block and only one catch will execute. Whichever one throws an exception first gets caught.

While debugging you should always comment out redirects so you can properly see what's happening. Dump $flash and $js and post whatever you see on the screen.


I tried making two separate try and catch statements and removing the redirect statements and everything comes out as expected. the js variable passes and the flash variable pass the check. when i add the redirection back in, the second exception will always throw, even when the condition is not met.

Modified Portion
<?php
			}

			try
			{ 
				var_dump($js, $flash);
				if($js=="off")
				{
					echo '<br> this executed js';
					throw new JavascriptNotSupported('No JS'); 
				}				
			} 
			catch(JavascriptNotSupported $e) 
			{ 
				echo $e;
			}
			
			try
			{
				if($flash == 'false' || $flash == false)
				{
					echo '<br> this executed flash';
					throw new FlashUpdateRequired('No Flash'); 
				}

			}
			catch(FlashUpdateRequired $e)
			{
				echo $e;				
			}
			?>



With No redirect in the exception classes
result is:

javascript:string(2) "on"
flash:string(4) "true"

with redirect re enabled, i am shown the redirected flash page

upon further review i found that what ever $js is set to prior to the form code is what value it keeps, but i dont understand why as the value should be retrieved from the $_GET array which is correct

This post has been edited by Crazy_Learner: 10 September 2011 - 11:09 AM

Was This Post Helpful? 0
  • +
  • -

#12 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,597
  • Joined: 08-August 08

Re: Condition Always Returns The Same

Posted 10 September 2011 - 11:08 AM

Your first post has (not including blank lines and breaks into/out of PHP) only 32 lines. This is a classic case showing how important it is to separate your languages.

If you can't read your own code when it's only 32 lines then can can't expect to complete any projects. You should immediately stop thinking about how to get your code to work and start thinking about how to organize it in a way that you can read it. It wouldn't hurt to start thinking about functions too.
Was This Post Helpful? 0
  • +
  • -

#13 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 10 September 2011 - 11:14 AM

Firstly, I usually do try to do that, however i cant really separate the form from this php and javascript in this case because its required to be on the same page. i have tried splitting them already and did not get any of the results expected. with the current layout the results being returned are correct, but the exception is thrown anyway.

Secondly, i can read my code and the majority of my site has separated all the languages used. Also please refer to the post with my code and you will see under the attempted solutions that i did put them in functions, and even tried a class shortly after

Not trying to be rude, but its not a matter of if i can read it, its a matter of its evaluating wrong which i know becuase it executes perfectly without the exceptions being thrown. Ie i replace the areas where the exception would be thrown with an echo command and it returns everything correctly.
Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3034
  • Posts: 10,597
  • Joined: 08-August 08

Re: Condition Always Returns The Same

Posted 10 September 2011 - 11:25 AM

  • If you could read your code, you'd be able to edit/debug it and wouldn't need help.
  • It is always possible to separate languages. See below.

Your code from your first post, separated:
<?php 
require_once('Errors/Exceptions/JavascriptNotSupported.php');
$js = array_key_exists('js', $_GET) ? $_GET['js'] : '';
echo "<html><body>";
if ($js == '') {
 $js = 'off';
	echo file_get_contents("myform.html");
}
try { 
	if($js="off") {
		throw new JavascriptNotSupported('No JS'); 
	} else {
		echo 'Javascript is enabled';
	}
} 
catch(Exception $e) {
	echo $e; 
}
echo "</body></html>";
?>


myform.html
<form name="postJs" action="<?php echo $PHP_SELF ?>" method="get">
<input type="hidden" name="js" value="on">
</form>
<script type="text/javascript"><!--
	document.postJs.submit()
//-->
</script>


Ooops! I forgot to fixt the action in the form, but hopefully you can see that's easy enough. I don't have time to go further now...

This post has been edited by CTphpnwb: 10 September 2011 - 11:26 AM

Was This Post Helpful? 0
  • +
  • -

#15 Crazy_Learner  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 145
  • Joined: 25-July 09

Re: Condition Always Returns The Same

Posted 10 September 2011 - 12:34 PM

Alright, here is the way you wanted it with all the corrections i have made thus far. It does not place any of the variables in the get Array and thus all validation never occurs because it never equals false due to the fact the variable is not there in the first place.

Again, not trying to be rude. i just hate repeating steps i have tried before and the only way i got the variables to be placed in the get array is by placing it in the same file. Also in case you dont know how this is working it is being implemented on another page, and as soon as its included it performs the checks using this script page. thus its suppose to use the same page for the get location

AutoSubmitForm.html
<html>

	<head>
	
		<title>Javascript Checking</title>
		
		<!-- Scripts -->
		<script type="text/javascript" src="../Detect/FlashDetection.js"></script>
		
	</head>
	
	<body>
		
		<form name="postJs" action="ValidateAbility.php" method="get">
		    <input type="hidden" name="javascript" id="javascript" value="false">
	   	    <input type="hidden" name="flash" id="flash" value="false">
	    </form>

		<!-- Submit The Form -->
	    <script type="text/javascript"><!--
	    	var flashTest = MeetsRequirement(10);
	    	document.getElementById('flash').value = flashTest;
	      	document.postJs.submit();
	    //-->
	    </script>
		
	</body>

</html>



ValidateAbility.php
<html>
	<head>
		<title>Validation Of Capabilities</title>
	</head>
	
	<body>
		
		<?php
			require_once('Errors/Exceptions/JavascriptNotSupported.php');
			require_once('Errors/Exceptions/FlashUpdateRequired.php');
			
			$js = array_key_exists('javascript', $_GET) ? $_GET['javascript'] : '';
			$flash = array_key_exists('flash', $_GET) ? $_GET['flash'] : '';
			
			echo 'Javascript: ' . $js . '<br>Flash: ' . $flash;
			
			$check = new CheckStandards($js,$flash);
			
			class CheckStandards
			{
				private $javascript;
				private $flash;
			
				/**
				 *
				 *
				 *
				 */
				public function __construct($javascript, $flash)
				{
					if(empty($javascript)) { $javascript = ''; }
					if(empty($flash)) { $flash = ''; }
					
					$this->javascript = $javascript;
					$this->flash = $flash;
					
					$this->javascript = $this->PerformJavascriptCheck();
					$this->flash = $this->PerformFlashCheck();
					echo '<br>Completed';
				}
				
				/**
				 *
				 *
				 *
				 */
				public function __destruct()
				{
				
				}
				
				/**
				 *
				 *
				 *
				 */
				private function PerformJavascriptCheck()
				{
					if($this->javascript == '')
					{
						$this->javascript = 'false';
						echo file_get_contents('AutoSubmitForm.html');
					}
					else
					{
						try
						{
							if($this->javascript == 'false')
							{
								throw new JavascriptNotSupported($this->javascript);
							}
						}
						catch(JavascriptNotSupported $jsLoadFail)
						{
							echo $jsLoadFail;
						}
					}
					
					return $this->javascript;
				}
				
				/**
				 *
				 *
				 *
				 */
				private function PerformFlashCheck()
				{
					try
					{
						if($this->flash == 'false')
						{
							throw new FlashUpdateRequired($this->flash);
						}
					}
					catch(FlashUpdateRequired $flashVersionBad)
					{
						echo $flashVersionBad;
					}
					
					return $this->flash;
				}

			}
			?>
	</body>
	
</html>


This post has been edited by Crazy_Learner: 10 September 2011 - 12:37 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2