Model-View-Controller: Processing the PHP file

The PHP file

After filling out the form a PHP file is processed. I am not going into detail except that when processing was successful or failed a javascript is executed to load the application again but with the variable formOutcome having a value.

<?php
$ordertype = $_POST['ordertype'];
if (!isset($ordertype))
{
	fail();
}
$name = $_POST['name'];
if (!isset($name))
{
	fail();
}
$email = $_POST['email'];
if (!isset($email))
{
	fail();
}
$from = 'From: flashscript.biz'; 
$to = 'flashs46@flashscript.biz';
$subject = 'Order Inquiry'; 

$body = "From: $name\n E-Mail: $email\n Message:\n $ordertype";

success();
   
function success()
{
	echo '<script type="text/javascript">
        window.location = "http://your_url.com/mvcexample.html?formOutcome=Okay";
      </script>';
}
function fail()
{
	echo '<script type="text/javascript">
        window.location = "http://your_url.com/mvcexample.html?formOutcome=Failed";
      </script>';
}

exit();
?>
	

Originally this variable was undefined. Now I can show the part in the HTML page, where this variable plays a role. If it is not undefined depending on the result, which we obtain by the variable formResult another variable, formMessage, will have a value.

/* 
This part of the Javascript is needed after the php form script was processed. We declare the 
variable 'formResult' and 'formMessage' and set both to 'undefined'. Once 'formResult' is defined
in the new URL after php processing it will have a value. This  has to be done prior to loading
all other scripts.
*/ 
				var formResult;
				formResult = location.search.split('formOutcome=')[1];
				
				var formMessage;
				if(formResult == "Okay")
				{
					formMessage = "Order was successful!";
				}
				if(formResult == "Failed")
				{
					formMessage = "ERROR: Try again!";
				}			
			</script>
			<script type="text/javascript" src="src/loading.js"></script>
		</footer>
	</body>
</html>
	

In the Documentclass there is a piece of code executed when formResult is not undefined. The visibility of vies is changed and an event is dispatched with parameters for formResult and formMessage. There is a listener in the Controller triggering a function.

/* 
formResult has only a value, when the php file was processed. We need to place this 
code outside of the Documentclass, because at one point the views are updated and the 
Documentclass needs to be instantiated for that.
*/

	if(formResult != undefined)
	{	
		this.showView ("resultForm");
		EventBus.dispatch("resultForm", c, formResult, formMessage);
	}
}
	

This gets us to the end of the this tutorial. One last word to the formatting of the views. The position of the canvas for the background is the leading position of the other views. Those are set relative to the canvas. All files can be downloaded here. Comments can be left on my blog.