Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Keeping settings, two forms and two submit buttons

    Hey, I've just started with PHP a couple of months ago. At the moment I'm working on a guestbook, just to try things out and to get a better understanding of OOP.

    So far I have added the ability to add messages and sort them (Ascending or Descending). This is working fine.

    Now I want to set the order to ascending, and add a message after that. If ascending is selected I want the new message at the bottom and not have it go back to the default ascending setting with the message at the top. How do I prevent that setting from changing after I submit a new message?

    PHP Code:
    $desc '';
    $asc '';

    if (isset(
    $_POST['selectorder'])) //Selection menu button is pressed
    {
        if (
    $_POST['sortmethod'] == "Ascending"//Ascending is selected
        
    {
            
    $asc 'selected="selected"';
            
    $desc '';
        }
        else if (
    $_POST['sortmethod'] == "Descending"//Descending is selected
        
    {
            
    $asc '';
            
    $desc 'selected="selected"';
        }
    }

    include(
    'class.users.php');
    include(
    'class.Database.php');
    include(
    'class.staticmethods.php');
    $user = new Users();

    echo 
    '<form method="post">
    Select sort method:
    <select name = "sortmethod">
    <option value = "Descending" ' 
    $desc '>Descending</option>
    <option value = "Ascending" '
    $asc '>Ascending</option>
    </select>
    <input type="submit" name="selectorder" value="Submit" />
    </form>'
    ;    

    if (isset(
    $_POST['submit'])) //new message
    {
        
    $user->addmessage($_POST['username'], $_POST['email'], $_POST['website'], 
                        
    $_POST['message']);
    }

    if (isset(
    $_POST['selectorder'])) //Selection menu button is pressed
    {
        if (
    $_POST['sortmethod'] == "Ascending")
        {
            
    $user->viewguestbook('asc'); //Display the guestbook, order is ascending
        
    }
        else if (
    $_POST['sortmethod'] == "Descending")
        {
            
    $user->viewguestbook('desc'); //Display the guestbook, order is descending
        
    }
    }
    else 
    //No buttons have been pressed
    {
        
    $user->viewguestbook('desc'); //Display the guestbook, order is descending

    Last edited by Clearasil; 10-31-2012 at 11:23 AM.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    I've always used a session to remember the settings instead - something like:

    $_SESSION['order'] = 'asc'; //Can be 'desc' instead

    Then I just put $_SESSION['order'] into my sql (without the single quotes if its inside the double quoted sql string).
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    Clearasil (10-31-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok thanks, I'll try that


  •  

    Posting Permissions

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