Results 1 to 10 of 10

Thread: [RESOLVED] Posting back on the same page

  1. #1

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Resolved [RESOLVED] Posting back on the same page

    Hi again,
    I'm wondering if I should be posting pages back on themselves? I ask because I have some instances with forms where I want to check that fields are full and the user is logged in and if they don't meet certain criteria, then they get redirected to another page. But if these conditions aren't met they can still at least view the page.

    Sometimes my conditions are met right away and so the user is redirected to the other page immediately, which is something that I don't want to do.

    So, long question short, is it okay practice to post to an intermediate page and then move right back to the original page? Or is there a preferred method? I found the IsPostBack property very useful in ASP.NET.

    Am I making sense?

  2. #2
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Posting back on the same page

    what do you mean if they don't meet certain criteria? Like if all the fields are not filled in and they are not logged in?

  3. #3

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Posting back on the same page

    Yes. That's what I mean.
    And clearly I have no idea what I'm doing yet with PHP.
    In ASP.NET I would do something like
    vb.net Code:
    1. If Not IsPostBack Then
    2. ' Blah
    3. End If
    to not take action if the user did not click a button or take some action to cause a post back.

    For example, the user views a post, there is a place to leave a comment at the bottom, but they can't do so unless they are logged in and the comment field is full. However, I'd like them to be able to at least view the page that this commentbox is on.

    So now I'm checking that the $_POST comment variable isset and is not empty, but if it is and the user is not logged in then redirect. However, the unset, empty, user-not-logged-in condition is met when you navigate to the page. So you get redirect immediately. Does that make sense because I'm confusing myself now.

  4. #4
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Posting back on the same page

    What I have done in the past on news, comment type situation is When displaying the comments section, I would check to see if the user is logged in. If the user is logged in then I would display the comment box and button to do the post. I then would have it post to a different page, and if post and insert to the db was both successful, redirect back to the page where they commented.

  5. #5
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Posting back on the same page

    Remember that you can put conditional html on a php page.
    ie:
    PHP Code:
    <?php
    if ($isloggedin == True){
        
    ?><Input type="textbox" name="comment"/>
    <?php 
        
    } else {
        
    //do not display the comment box/button
        
    }
    ?>
    The only time you will see the textbox will be if the user is logged in.

    Edit: $isloggedin would need to be set and I only use it for demonstration purposes. You could just check your session variable and make sure it is set.
    Last edited by zalez; Aug 17th, 2007 at 03:48 PM.

  6. #6

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Posting back on the same page

    That's a better idea.

    Regarding the redirect, it is okay to post to an intermediate "processmypage.php" page that is not shown to do some processing and then redirect back to the "page.php" page instead of posting back on "page.php"?

    I didn't know if that was considered okay practice to do so.

  7. #7
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: Posting back on the same page

    I think it is just a personal preference. I feel it is ok but mind you I am not a php guro. I am a php advocate . I use this method and have not had any issues. On the process page I use a refresh header and delay it for 5 seconds and print on the screen that the message was successful and that they are being redirected. Matter of fact, this forum uses this method if you watch close when you post.

  8. #8

    Thread Starter
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Posting back on the same page

    Fair enough. Thanks once again for the suggestions and help.

    Wait 5 minutes and I'll probably have started another thread.

  9. #9
    Hyperactive Member
    Join Date
    Dec 2004
    Posts
    326

    Re: [RESOLVED] Posting back on the same page

    I'm sure I will be here

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: [RESOLVED] Posting back on the same page

    Whether you make POST requests to the same URL as the GET request depends on whether you care about using meaningful URLs.

    My preferred method is to use mod_rewrite to direct all requests to a single PHP script which then loads the appropriate page based upon the URL structure and the type of request.
    For example, a request GET /products/157 might load the script /products/get and pass the parameter id=157. If any changes need to be made to that item then I would make a PUT request to the same URL; my PUT handler script will be run and then make a GET redirect to the same URL again.
    If a new product needs to be added then I would make a POST request to /products/add or similar.

    If you don't want to use mod_rewrite then you would group logically-related actions into single scripts (e.g. product.php) and determine the action based upon request type and querystring parameters.

    There is no 'best' URL structure for any particular case; it is mostly up to you and what you find most logical.

Posting Permissions

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



Click Here to Expand Forum to Full Width