SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    So Simple, but yet so hard.. what am I missing?

    Hey,

    I have a form, lets call it "book.php". The action of this form is to go to a page called "summary.php". This works fine, and the summary outputs a summary of the users submission, etc...

    Now what I want to do, is have 2 buttons. One of them submits the final data, the other cancels and leaves the page. I cannot find a good way to do this, if I make the input button type "submit" it will post the page again thus destroying my current variables.

    What I ideally want is just a button, if a user clicks the button I run the MySQL to commit the inputs, if the other button is pressed a user is redirected elsewhere. However, I cant find a way, using PHP, to determine if a normal "button" has been pressed.

    for example, here is my portion of codes that matters.
    PHP Code:
    <form method="post">
    <p><input type="submit" value="Yes, Submit!" name="submitfinal"><input type="reset" value="No, Start Over" name="resetfinal"></p>
    </form>
    <?php
            
    if (true == isset($_POST['submitfinal'])) {
            
    $result2 mysql_query($sql2); 
        
              if ((!
    $result2) or (mysql_affected_rows() < 1)) {
                echo 
    '<br><p>There was a mysql error or something went wrong, now rolling back...';
                echo 
    '<br><p>Please contact the administrator if this problem persists';
                if (
    mysql_error()) { echo '<br><p>Error performing query: ' mysql_error() . '</p>'; }
                
    mysql_query("ROLLBACK"); die;  }
              
                  else { echo 
    'Expense Submitted!'mysql_query("COMMIT"); } 
            }
            
              if (
    true == isset($_POST['resetfinal'])) {
                echo 
    'Cancelling request...';
                
    mysql_query("ROLLBACK"); }
        }
       
    ?>
    This doesnt work as intended, because when a users clicks submit, it esentially posts to the page again and wipes all my vars out...?

  2. #2
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    your code can check to see what was pressed by giving each button a different name. Then all you so is check if the post variable exists.

    What I generally do is write the contents of all post values back to the page as hidden inputs.

    So when you post from step 1 to step 2, the values posted from step 1 will be put into hidden input fields so when you post from step 2 to step 3 it will carry the values.


  3. #3
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see what your saying, but I dont want to have to post, or reload the page.

    For example, when a user submits the initial form they are taken to the summary. When they are taken to this page, the MySQL queries run, I am using transactions so the queries are run but not commited.

    If a user on this summary page clicks a button (or something), I want to be able to "commit" these queries, if not I can rollback and leave the page.

    However, doing a new post will lose all of this data, and hidden inputs wont really do what I want in this scenario? So I really have no idea how to acheive this.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdob View Post
    I see what your saying, but I dont want to have to post, or reload the page.

    For example, when a user submits the initial form they are taken to the summary. When they are taken to this page, the MySQL queries run, I am using transactions so the queries are run but not commited.

    If a user on this summary page clicks a button (or something), I want to be able to "commit" these queries, if not I can rollback and leave the page.

    However, doing a new post will lose all of this data, and hidden inputs wont really do what I want in this scenario? So I really have no idea how to acheive this.
    I really hate to bump my thread, but does anyone have any help?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I have read this 3 times now and still don't understand what you are trying to do.

    I mean this:
    PHP Code:
    <input type="reset" value="No, Start Over" name="resetfinal"
    Is not going to send any signal to your backend script, thats a basic html reset button that just wipes out any new input into your form. I dont think I ever used it, I just put on a cancel button that sends users away.

    So trying to snag it with this:
    PHP Code:
    if (true == isset($_POST['resetfinal'])) 
    Shows you probably havent understood how forms react with PHP.

    Try and describe your dilemma a little differently.

  6. #6
    SitePoint Enthusiast
    Join Date
    Feb 2008
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tdob View Post
    I see what your saying, but I dont want to have to post, or reload the page.

    For example, when a user submits the initial form they are taken to the summary. When they are taken to this page, the MySQL queries run, I am using transactions so the queries are run but not commited.
    It seems that exactly what you want to do is not possible with pure PHP.

    Remember, all php code is executed before the page is sent to the user. So when your summary page is loaded, PHP processes the SQL query, outputs the button, and then checks if the button was pressed - but this is all before the output goes to the user.

    There is no way to ask for user input and process it on the same page without re-loading.

    One thing you could do is re-design the app. The summary page doesn't actually process the query - it reprints the information for the user to confirm. Then, if the user confirms it the information is passed to a final processing page (through hidden elements or session info) to be processed. If there are no errors, commit, otherwise roll back.

    If you want to capture user input (i.e. the button) without re-loading, you can use AJAX to call a separate PHP script. However, I'm not sure if you can start a transaction in one script and commit it in another (not that familiar with transactions).

    From the bit I have read about transactions, though, it seems that getting a confirmation from the user isn't what they're intended for. They're supposed to be used to check for errors and protect the integrity of the database.

    I don't see how starting a transaction allows the user to better confirm what is being entered than just displaying the information before the transaction is started.

    Good luck,
    - Walkere

  7. #7
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    I have read this 3 times now and still don't understand what you are trying to do.

    I mean this:
    PHP Code:
    <input type="reset" value="No, Start Over" name="resetfinal"
    Is not going to send any signal to your backend script, thats a basic html reset button that just wipes out any new input into your form. I dont think I ever used it, I just put on a cancel button that sends users away.

    So trying to snag it with this:
    PHP Code:
    if (true == isset($_POST['resetfinal'])) 
    Shows you probably havent understood how forms react with PHP.

    Try and describe your dilemma a little differently.
    Thanks for your reply. I understand that wont do anything, its just a part of code I havent go to yet. Its just there as a placeholder for now.

    As Walkere outlined, I have probably coded myself into a corner anyway, and am thinking about doing things the wrong way. However, I will try to describe it to see if anyone has any ideas.

    I have a form that allows users to enter expenses. The can dynamically add a new row to the form, so the submission of the form can contain a lot of data depending on the expenses they have.

    That form directs them to a summary.php which formats things nicer, does some calculations, allows the user to print the summary. I also want to be able to make the user click Yes, this is correct before actually submitting the MySQL.

    The dillemma is, to do this, I need a submit button, which ultimately posts to itself, wiping out all the data. I suppose the only way around this is after the inital post setting a variable like $_SESSION['input']=$_POST? I guess I was trying not to use sessions, as I am using enough of them as this entire system is integrated with active directory in a corporate enviroment, but it seems like the only logical answer.

  8. #8
    SitePoint Evangelist Dave Morton's Avatar
    Join Date
    Sep 2003
    Location
    Carson City, NV
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've written what sounds like exactly what you're trying to do, but since my page isn't for the web, but part of an internal application, I do the vast majority of the calculations/verification client side through JavaScript. I don't suggest such a method for the general public (too many folks have JS disabled), but if this is something where you have more control over which browser is used to view the page/whether JS is enabled, etc, it may be of some help to you, and you can probably get past this problem. I'm more than willing to post code, if necessary, and post a link to an example page, if you like. Let me know (don't want to waste the space if I'm coming from the wrong direction, after all)
    Making a difference, one little psychotic episode at a time
    Geek Cave Creations
    Beta testers needed for pChat
    Dave's Gallery

  9. #9
    SitePoint Addict
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    373
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dave Morton View Post
    I've written what sounds like exactly what you're trying to do, but since my page isn't for the web, but part of an internal application, I do the vast majority of the calculations/verification client side through JavaScript. I don't suggest such a method for the general public (too many folks have JS disabled), but if this is something where you have more control over which browser is used to view the page/whether JS is enabled, etc, it may be of some help to you, and you can probably get past this problem. I'm more than willing to post code, if necessary, and post a link to an example page, if you like. Let me know (don't want to waste the space if I'm coming from the wrong direction, after all)
    Hey, yeah that would be great. This is an internal application, won't be accessible from the internet.

    Feel free to pm me or post it, thanks for the help!


Bookmarks

Posting Permissions

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