Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree3Likes

Thread: Best way to transfer control from page-to-page

  1. #1
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Best way to transfer control from page-to-page

    I am new to web development and am not confident in the best way to change control from page-to-page.

    Before going onward, let me be transparent and say that I am actually programming in another language (i.e. PHP), but I respect Java programmers, and since my question(s) are more about web programming in general, I am hoping you guys will be willing to help me out?!)

    Here is my challenge...

    I have a web form and when I submit it, I need to display a non-data-validation message. On success, maybe, "Your account was created." And on failure, maybe, "Your account could not be created. Contact the administrator."


    My 1st attempt at things went like this...
    Java Code:
    Opening HTML
    -------------------------
    If Form Submitted...
    	Check Data
    	Process Form
    	Display Success Msg and echo Nested HTML to close out Page
    	(or Display Failure Msg and echo Nested HTML to close out Page
    -------------------------
    HTML Form
    Closing HTML

    So I had an HTML page, an HTML form, and HTML to display on Success and another set of HTML to display on Failure?!


    In my 2nd attempt at things went like this...
    Java Code:
    If Form Submitted...
    	Check Data
    	Process Form
    	Assign "Outcome Code" to variable and store in Session
    	Redirect to "Outcome Page" and display message based on "Outcome Code" found in Session
    -------------------------
    Opening HTML
    HTML Form
    Closing HTML
    This second approach works better in that it eliminates issues with the user submitting the Form and then clicking the Back button and then the Forward button.

    It also is slightly better in that it eliminates my "nested HTML" which was needed to create different ending HTML to close things out.


    If someone could help me understand what is a better way to do what I need to do, that would be great!

    Specifically...

    1.) I would like to separate the Presentation from the Logic more
    2.) Make each file shorter and not have one file act as 3 pages
    3.) Ensure that if a user hits Back and then Forward it won't break or double-submit things...



    I know I am not a Java programmer, but any help would be *greatly* appreciated!!!

    Thanks,


    Debbie

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Best way to transfer control from page-to-page

    Are you asking a Java-related question or a Javascript-related question?

  3. #3
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by Fubarable View Post
    Are you asking a Java-related question or a Javascript-related question?
    I am asking a web-based programming/design question, but there wasn't a specific forum for that so I asked here.

    My question should be fairly platform independent since it is about separating the HTML from the business logic and about submitting a form onto itself versus using redirects to handle what happens after a web form is submitted.

    (Of course telling me how to write the world's greatest EJB won't help me, but I think my questions would apply whether I was doing this in PHP or .Net or java or whatever.)

    HTH,


    Debbie

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page

    Web frameworks like Grails, Rails, Spring MVC, etc... have already done this for you. Enterprise apps tend to rely on Spring/Hibernate and more recently on the more dynamic Grails (which is also based on Spring/Hibernate). All MVC frameworks handle the separation of control, and provide facilities for user feedback, roll back, etc...

    You could roll your own if it is just for the practice, but these are proven and scalable technologies that make development much simpler and easier than attempting to reinvent the wheel! Good luck :)
    Fubarable likes this.

  5. #5
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by DoubleDee View Post
    I am asking a web-based programming/design question, but there wasn't a specific forum for that so I asked here.

    My question should be fairly platform independent since it is about separating the HTML from the business logic and about submitting a form onto itself versus using redirects to handle what happens after a web form is submitted.

    (Of course telling me how to write the world's greatest EJB won't help me, but I think my questions would apply whether I was doing this in PHP or .Net or java or whatever.)

    HTH,


    Debbie
    Was hoping to have someone answer questions and help me understand things so I can do this myself.

    Any takers?


    Debbie

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page

    Ok, so if this is for your reference, I have to ask what platform you intend to do this on? If java, then I assume you're using servlets in an enterprise container? If thats the case, then I'll go with what I said before and suggest you look at Spring MVC. The Model View Controller design pattern specifies a separation of data (model), display of data (view) and control over said data (controller).

    But Spring MVC is a huge pain in the ass and I really wouldn't do it this way. It involves learning about enterprise containers, servlets, java beans, xml, dependency injection, DAO objects, application servers, etc....

    Java is NOT a dynamic language like php, python, perl, ruby, groovy, lisp, clojure, etc... You cannot just mix in java code with html and have it do anything unless using JSP or the like on an application server.

    This is the entire reason Grails was invented. Use a dynamic language (groovy) to help model, control, and display (mvc) data from a database to the user via controllers and views. This is the next step in web application programming beyond a simple php/html page with static lookups and display code.

    If you want to have this control over modular code with clean, clear separation of control, then you need to learn one of the web frameworks I mentioned. Even .net (blehhh) has MVC stuff these days for web apps. If you are unfamiliar with MVC in general, read up on 'design patterns', or check out this wiki: http://en.wikipedia.org/wiki/Model–view–controller

  7. #7
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by quad64bit View Post
    Ok, so if this is for your reference, I have to ask what platform you intend to do this on?
    I stated that in my opening post...


    Before going onward, let me be transparent and say that I am actually programming in another language (i.e. PHP), but I respect Java programmers, and since my question(s) are more about web programming in general, I am hoping you guys will be willing to help me out?!)

    Debbie

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page


  9. #9
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by quad64bit View Post
    I know how to Google...

    Anyone else care to help me figure some things out?


    Debbie

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page

    I don't understand what You're asking. You said:

    1.) I would like to separate the Presentation from the Logic more
    2.) Make each file shorter and not have one file act as 3 pages
    3.) Ensure that if a user hits Back and then Forward it won't break or double-submit things...
    and you stated that you program in PHP. I described what is done in the java world to handle these problems. This is a java forum. Are you asking java developers to provide php how-to information? If so, you might have better luck on a php forum. I'm trying to be helpful here, but you aren't giving me much to go on. I explained how the MVC design patterns address your issues and gave you links to numerous MVC packages for php... What exactly are you looking for then?
    Fubarable likes this.

  11. #11
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by quad64bit View Post
    I don't understand what You're asking. You said:

    and you stated that you program in PHP. I described what is done in the java world to handle these problems. This is a java forum. Are you asking java developers to provide php how-to information? If so, you might have better luck on a php forum. I'm trying to be helpful here, but you aren't giving me much to go on. I explained how the MVC design patterns address your issues and gave you links to numerous MVC packages for php... What exactly are you looking for then?
    Let's start over...

    I am trying to better understand web-programming and *hoping* that the answers I need are independent of the language I am using which is PHP.

    I have a "Create an Account" page that I am trying to refactor. The way the script works is that initially an HTML form is displayed (via a PHP page). When the user submits the Form, I look at the $_POST variable and then run my PHP script to handle the form. If the account creation was successful (i.e. record was inserted into table) then I display a "Success" message. If the insert failed, I display a "Contact your administrator message". Currently the initial Page/Form and then the Success OR Failure messages are all being shown via ONE PHP PAGE. To make this work I need to have HTML echoed in the Success and Fail branches so my page closes out because if my code dropped down to the bootm in order to finish out the page I would end up displaying the form again.

    Needless to say this is extremely hard to program and maintain.

    My second attempt at my "Create an Account" entailed having PHP at the top of the script and HTML at the bottom. First pass the PHP was skipped and the HTML form displays. When the form is submitted, the page is submitted back to itself and my PHP handles the form. This time, though, I just assign a value to an "Outcome Code" - in a session - depending if the account creating "succeeded" or "failed". Then I used a redirect to an "Outcome Page" which reads the "outcome variable" in the session and displays a message accordingly.

    This approach was a slight improvement in that I had one script to display the form and one script to display the "outcome".

    The redirect also prevented double-submissions.

    But I still feel like I am programming like a BASIC programmer and not the way a java developer would?!

    Now, I am not ready to learn OOP or MVC, but I think my questions can be answered here - by Java programmers - by just helping me better understand how you pass data between pages and whether you let scripts submit back to themselves and make a one-in-all-script, or if you break things up like I tried to do in my second attempt.

    Follow me?

    I think my questions are more how to program for the web and less about a particular language.

    Thanks,



    Debbie

  12. #12
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page

    Alright, that is something to go on. One thing you can do is to break up your scripts into separate files. What you put into what file really depends on context. Perhaps all login methods go in one file. Perhaps all database queries go in another. Then from your primary HTML, you can reference scripts as needed and not have to have all that nonsense in the HTML (well, less of it anyway). Generally a failed login redirects back to the original login page, and if you want to be nice, send the username back again after the failed attempt, so the user doesn't have to retype it. Most frameworks also make use of a 'flash' variable. This is a one-time use variable used on error (or success) to display a message to the user. Your login script might set the value of this after a failed or successful attempt so it might be displayed by the login page.

    Usually the way it works is this, you type in a form. The form submits fields to php script. Script does what it needs to do. It also sets flash variable. It either redirects back to login on failed attempt, or redirects to some other page on successful attempt. The login page should be built to always look for the flash - if it's there, it can display it with special styling, if it isn't, it ignores it. Same for the login field - if a login exists in memory, set the login field to that (since that means we've been directed back from a previous failed attempt) and if it doesn't exist, we just don't fill anything out.

    You will also need to lock down your other pages somehow, so a person can't just bypass the login by typing in a URL explicitly. You could do this with a session variable, but you'll need to check it on every page. You could write a script that does this, and reference it in every page. The login should also expire after a certain time.

    I know I'm beating you to death with this, but seriously, time spent looking at an mvc framework is not time wasted, nor is it a cop-out. Everything you are trying to do is handled with controllers that have 'actions' associated with them, and all of that is navigable with very simple RESTful services, CRUD, etc... These frameworks also have security available via plugins. If you want this code to be maintainable and expandable, you need to come up with a common system for things you do i.e. login, navigate to secure pages, logout, look up user-specific records. The concept is called convention over configuration - meaning, you always do something a certain way, and therefor you always know how to do something. From-scratch web systems don't take advantage of this - for example, you wouldn't know how to log into (programmatically) a system I invented and I couldn't log into yours unless we first shared an API or something. With REST and CRUD, it is the same everywhere. Frameworks enable simple REST and CRUD interfaces so that you don't have to write an entire framework stack yourself.

    I'm not picking on you, sorry to beat a dead horse - but consider reading up on some of these a little bit; even if you don't decide to use them, they might lend insight on the proper way to do it yourself in your own app!

    Hope that helps a bit!
    Fubarable likes this.

  13. #13
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    quad64bit,

    Can I post my PHP here?

    Even if you don't know the specifics, it would help for you - and others - to see my programming style/approach.

    Thanks,


    Debbie

  14. #14
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Quote Originally Posted by quad64bit View Post
    Alright, that is something to go on. One thing you can do is to break up your scripts into separate files.
    Which I tried.


    What you put into what file really depends on context. Perhaps all login methods go in one file. Perhaps all database queries go in another. Then from your primary HTML, you can reference scripts as needed and not have to have all that nonsense in the HTML (well, less of it anyway).
    Does my HTML file reference the PHP scripts, or do I have a Base PHP Script that includes HTML Templates as needed?


    Generally a failed login redirects back to the original login page, and if you want to be nice, send the username back again after the failed attempt, so the user doesn't have to retype it. Most frameworks also make use of a 'flash' variable. This is a one-time use variable used on error (or success) to display a message to the user. Your login script might set the value of this after a failed or successful attempt so it might be displayed by the login page.

    Usually the way it works is this, you type in a form. The form submits fields to php script. Script does what it needs to do. It also sets flash variable. It either redirects back to login on failed attempt, or redirects to some other page on successful attempt.
    I have the form reload on itself for data entry errors. But if the INSERT fails, I redirect to a new page just like when it succeeds.


    The login page should be built to always look for the flash - if it's there, it can display it with special styling, if it isn't, it ignores it. Same for the login field - if a login exists in memory, set the login field to that (since that means we've been directed back from a previous failed attempt) and if it doesn't exist, we just don't fill anything out.
    Not understanding that one. (Guess we can come back to that later?!)


    You will also need to lock down your other pages somehow, so a person can't just bypass the login by typing in a URL explicitly. You could do this with a session variable, but you'll need to check it on every page. You could write a script that does this, and reference it in every page. The login should also expire after a certain time.
    Guess I'll need help with that one.


    I know I'm beating you to death with this, but seriously, time spent looking at an mvc framework is not time wasted, nor is it a cop-out. Everything you are trying to do is handled with controllers that have 'actions' associated with them, and all of that is navigable with very simple RESTful services, CRUD, etc... These frameworks also have security available via plugins. If you want this code to be maintainable and expandable, you need to come up with a common system for things you do i.e. login, navigate to secure pages, logout, look up user-specific records. The concept is called convention over configuration - meaning, you always do something a certain way, and therefor you always know how to do something. From-scratch web systems don't take advantage of this - for example, you wouldn't know how to log into (programmatically) a system I invented and I couldn't log into yours unless we first shared an API or something. With REST and CRUD, it is the same everywhere. Frameworks enable simple REST and CRUD interfaces so that you don't have to write an entire framework stack yourself.

    I'm not picking on you, sorry to beat a dead horse - but consider reading up on some of these a little bit; even if you don't decide to use them, they might lend insight on the proper way to do it yourself in your own app!

    Hope that helps a bit!
    I don't disagree with you, HOWEVER, I will never get my website tweaked and up and running in the time I need to doing that.

    Help me fix what I have so it is decent, and then after my site is up I can look at Code Ignitor for "Release #3"...

    I will post my code next...



    Debbie

  15. #15
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    quad64bit,

    Here my code from this Fall. (It is actually my "Add a Comment" form, but the approach is the same as I need for "Add a Customer".)

    The code works just fine, but I still think I could write better code and so that is why I am back.

    You may not know PHP, but it is so well documented it would be hard not to understand programatically what I am doing!!

    PHP Code:
    <?php
    	// Initialize a session.
    	session_start();
    
    	// Access Constants.
    	require_once('config/config.inc.php');
    
    	// Initialize Variables.
    	$_SESSION['resultsCode'] = '';
    
    
    	// **************************
    	// Check for Primary Keys.	*
    	// **************************
    
    	// If either articleID or memberID do not exist for whatever reason, then we
    	// cannot insert a record, and therefore the form should never be displayed.
    	if (empty($_SESSION['articleID'])	|| empty($_SESSION['memberID'])){
    		// Key(s) Missing.
    		$_SESSION['resultsCode'] = '61';
    
    		// Redirect to Processing Page.
    		header("Location: " . WEB_ROOT . "process_comment.php");
    
    		// End script.
    		exit();
    	}
    
    
    	// ************************
    	// Check for Form Values.	*
    	// ************************
    
    	// If either pageTitle or memberFirstName do not exist for whatever reason,
    	// then either there were problems setting the $_SESSION, or the user may have
    	// arrived at this page directly which indicates a larger problem.  In either
    	// case, the form should not be displayed.
    	if (empty($_SESSION['pageTitle'])	|| empty($_SESSION['memberFirstName'])){
    		// Form Values Missing.
    		$_SESSION['resultsCode'] = '62';
    
    		// Redirect to Processing Page.
    		header("Location: " . WEB_ROOT . "process_comment.php");
    
    		// End script.
    		exit();
    	}else{
    		// Set Local Variables.
    		$pageTitle = $_SESSION['pageTitle'];
    		$memberFirstName = $_SESSION['memberFirstName'];
    	}
    
    
    	// *************************************************************
    	// HANDLE FORM.																								 *
    	// *************************************************************
    	if ($_SERVER['REQUEST_METHOD']=='POST'){
    		// Form was Submitted (Post).
    
    		// Initialize Errors Array.
    		$errors = array();
    
    		// Trim all form data.
    		$trimmed = array_map('trim', $_POST);
    
    
    		// ************************
    		// Validate Form Data.		*
    		// ************************
    		// Check Comment.
    		if (empty($trimmed['body'])){
    			$errors['body'] = 'Please enter a Comment.';
    		}
    
    		// **************************
    		// Attempt to Add Comment.	*
    		// **************************
    		if (empty($errors)){
    			// Form data clean.
    
    			// Set Comment Variables.
    			$articleID = $_SESSION['articleID'];
    			$memberID = $_SESSION['memberID'];
    			$body = $trimmed['body'];
    			$status = 'Pending';
    
    
    			// Connect to the database.
    			require_once('private/mysqli_connect.php');
    
    			// Build query.
    			$q = "INSERT INTO comment(article_id, member_id, body, status, created_on)
    							VALUES(?, ?, ?, ?, NOW())";
    
    			// Prepare statement.
    			$stmt = mysqli_prepare($dbc, $q);
    
    			// Bind variable.
    			mysqli_stmt_bind_param($stmt, 'iiss', $articleID, $memberID, $body, $status);
    
    			// Execute query.
    			mysqli_stmt_execute($stmt);
    
    			// Verify Insert.
    			if (mysqli_stmt_affected_rows($stmt)==1){
    				// Insert Succeeded.
    				$_SESSION['resultsCode'] = '75';
    			}else{
    				// Insert Failed.
    				$_SESSION['resultsCode'] = '63';
    			}// End of VERIFY INSERT.
    
    			// Close prepared statement.
    			mysqli_stmt_close($stmt);
    
    			// Close the connection.
    			mysqli_close($dbc);
    
    
    			// Redirect to Display Outcome.
    			header("Location: " . WEB_ROOT . "process_comment.php");
    
    			// End script.
    			exit();
    
    		}else{
    			// Form data has errors.			(** NEW **)
    			// Drop-through to Form to display errors.
    
    		}// End of CHECK FOR ERRORS.
    
    	}else{
    		// Form was NOT Submitted (Get).
    		// Drop-through to display Form.
    
    	}// End of HANDLE FORM.
    
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    	<!-- HTML Metadata -->
    	<title>Comment on an Article</title>
    	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    	<meta name="description"
    				content="" />
    	<meta name="keywords"
    				content="" />
    
    	<!-- Page Stylesheets -->
    	<link type="text/css" rel="stylesheet" href="css/main.css" />
    	<link type="text/css" rel="stylesheet" href="css/dropdown.css" />
    	<link type="text/css" rel="stylesheet" href="css/components.css" />
    	<link type="text/css" rel="stylesheet" href="css/add_comment.css" />
    </head>
    
    <body>
    	<div id="wrapper" class="clearfix">
    		<div id="inner">
    			<!-- Include BODY HEADER -->
    			<?php require_once(ROOT . 'components/body_header.inc.php');	?>
    
    			<!-- MIDDLE COLUMN -->
    			<div id="middle_2col">
    				<div class="box_Content">
    					<div id="box_Comments">
    						<h3 class="header2_h3">What Do You Think?</h3>
    
    						<form id="addComment" action="" method="post">
    							<!-- ADD A COMMENT FORM -->
    							<fieldset>
    								<ol>
    									<!-- Set Form Token -->
    <!--
    									<li>
    										<input type="hidden" name="form_token" value="<?php echo (isset($_SESSION['form_token']) ? $_SESSION['form_token'] : ''); ?>" />
    									</li>
    -->
    									<!-- Article Title -->
    									<li>
    										<p class="fauxLabel">Article Title:</p>
    										<p class="fauxInput"><?php echo '"' . htmlspecialchars($pageTitle, ENT_QUOTES) . '"'; ?></p>
    									</li>
    
    									<!-- First Name -->
    									<li>
    										<p class="fauxLabel">First Name:</p>
    										<p class="fauxInput"><?php echo htmlspecialchars($memberFirstName, ENT_QUOTES); ?></p>
    									</li>
    
    									<!-- Comment -->
    									<li>
    										<label for="body">Comment:</label>
    										<textarea id="body" name="body" cols="50" rows="15"><?php if (isset($body)){echo htmlspecialchars($body, ENT_QUOTES);} ?></textarea>
    <!--
    										<textarea id="body" name="body" cols="50" rows="15"><?php //echo $body; ?></textarea>
    										<textarea id="body" name="body" cols="50" rows="15"><?php //echo htmlspecialchars($body, ENT_QUOTES); ?></textarea>
    -->
    										<?php
    											if (!empty($errors['body'])){
    												echo '<span class="error">' . $errors['body'] . '</span>';
    											}
    										?>
    									</li>
    
    									<!-- Submit Form -->
    									<li>
    										<input type="submit" name="addComment" class="button" value="Add a Comment"/>
    									</li>
    								</ol>
    							</fieldset>
    						</form>
    					</div><!-- End of BOX COMMENTS -->
    				</div><!-- End of #BOX_CONTENT -->
    			</div><!-- End of #MIDDLE -->
    
    			<!-- RIGHT COLUMN -->
    			<div id="right">
    				<!-- Insert STICKY -->
    				<?php	require_once('components/content_sticky.inc.php');	?>
    
    			</div><!-- End of #RIGHT -->
    
    		</div><!-- End of #INNER -->
    	</div><!-- End of #WRAPPER -->
    
    	<!-- Include BODY FOOTER -->
    	<?php	require_once(ROOT . 'components/body_footer.inc.php');	?>
    
    </body>
    </html><? //Build Date: 2011-09-06 6:46pm ?>
    Based on the understanding that I need to take baby-steps and try and improve what I have before I go make wholesale changes, what do you think about this code?

    Thanks,


    Debbie

  16. #16
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Best way to transfer control from page-to-page

    Does my HTML file reference the PHP scripts, or do I have a Base PHP Script that includes HTML Templates as needed?
    Both kind of. I'm not sure the default PHP setup will pay attention to .html files, or only .php files. Either way, you can have a .php file that is 99% html or 100% PHP. Doesn't really matter, since PHP is executed and transformed server side, the only thing that makes it to the user is the HTML in the file, and the html generated by php statements.

    But if the INSERT fails, I redirect to a new page just like when it succeeds.
    You could do that I guess, but why? If you want to give the user the opportunity to try again, they wouldn't be trying again from somewhere else right? (if I am understanding the context of the situation - I try to log in, it fails, tells me it failed, I try again after modifying my name or pass)

    Not understanding that one. (Guess we can come back to that later?!)
    Flash is just the name they tend to call the error variable - because it 'flashes' up to the user. An example would be when you try to log in and the password field turns red and a little message 'flashes' up saying invalid password. It's simpler than it sounds.

    what do you think about this code?
    I really don't know anything about php, I've only used it once or twice before. When I used it, I defiantly put all that stuff in your php block in separate files. You can also break your PHP files up into functions - that will give you much cleaner code and allow easier reuse.

    My suggestion for you at this point is to do what I do, and do what I tell others in the same spot. Make a new project, a very very small one that does something really simple... like print a helloWorld!. Use that to figure out how to get separate files working, functions, etc... If it works on a mini example, it'll work in the larger case too. Experimenting with a large code body like that can be really frustrating. There is a lot more room for side effects with lots of code - a simple example is easier to follow.

  17. #17
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    I'll see what I can come up with.

    Thanks,


    Debbie

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Best way to transfer control from page-to-page

    There should be some decent PHP tutorials out there for this sort of thing, surely?
    I know I've got a couple of APress books at home that cover how to modularise your PHP web app in the way that you are trying to do, so it's not unheard of.

    It just seems odd to be asking this here...

  19. #19
    AlanMcBee is offline Member
    Join Date
    Feb 2012
    Location
    near Sacramento CA
    Posts
    1
    Rep Power
    0

    Default Re: Best way to transfer control from page-to-page

    Debbie,

    I'm not a Java programmer, nor a PHP programmer. (I specialize in MS technologies.) I think the Java guys are getting distracted by the absence of Java knowledge needed to solve this issue for you. ;-)

    The question you're asking is addressed by a couple of Design Patterns that were identified and documented by Martin Fowler in his book Patterns of Enterprise Application Architecture (Addison-Wesley Professional, 2002).

    I can't tell from your profile and your question whether you are new to Patterns. Briefly, they are recommended language/platform-independent ways of solving common problems. It will take a while to learn the common ones and know when and where to use them -- particularly when they don't include examples in your platform or language.

    The two that came to my mind are the Front Controller and the Application Controller patterns.

    Right away, I'll say that based on my limited knowledge of PHP, you will probably not want the Front Controller pattern. A defining characteristic of Front Controller is that all incoming web requests, regardless of the source page, will be routed to one code path that will decide what to do with it. This doesn't mean that you use just one URL for all pages; it means that you configure your framework and code to intercept all URL requests and send them to your Front Controller class first. In PHP, I think this means you need to have a common include file that you always call. This can be inefficient and difficult to enforce.

    The Application Controller is more like a state machine. As each page processes its requests (whether initial view, or handling the posted data and the results you get from processing it), it gets a Command class object (Command is another pattern, although a very simple one, that is described in the book on Design Patterns, often called the Gang-of-Four Patterns book (see Wikipedia)). The page then executes the Command, which is a signal to the Application Controller to change the state. In more practical terms, that means transferring control to another page. How you do it is up to you, but all the logic for it will be in one place and isolated from the logic for the page itself. You can use Session values, or use a Redirect (HTTP 302), or emit javascript -- it doesn't matter. Whatever meets your requirements (sounded like you had some regarding the Back button and navigation history). I will recommend that you strive to rely on just one method. Also, bear in mind that you can use more than Application Controller, but each one should be scoped to handle an entire logical grouping of pages.

    I want to dispel a few points of this thread that may have been confusing. You do not need a framework for this pattern. Spring, Grails, etc, are MVC frameworks that have pretty much nothing to do with your problem. They're great frameworks which are not at all useful for PHP. Similarly, your problem has nothing to do with CRUD, REST, or even access control. I'm not sure why those suggestions were made, but they're distracting.

    Hope that helps,
    ->Alan

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: Best way to transfer control from page-to-page

    It's hardly surprising people were looking at this from a Java perspective, as this is a Java forum.
    As you note, PHP has it's own quirks that make certain patterns less attractive, so asking at a PHP forum would have made more sense.

    In any case (bar the first post where quad64bit, not unreasonably, thought this was about Java), the rest is fairly reasonable. Directing someone who is asking about separating display from business logic to the relevant MVC frameworks for their language makes perfect sense. And those frameworks will be using (as Struts and Spring do) patterns relevant to the language.

    Oh, and this thread is 2 months old...:)

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 11-24-2011, 01:18 PM
  2. Replies: 0
    Last Post: 08-23-2011, 12:38 PM
  3. Go back to previous page using session in JSP page
    By gopikarikati009 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-23-2011, 10:30 AM
  4. How to Export web page(jsp page) to excel using jsp or servlets or javascript
    By techsing14 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 05-25-2011, 11:08 AM
  5. setting the view to a jsp page from a self refeshing page
    By deepal_205 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-15-2008, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •