Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-29-2012, 01:28 PM   PM User | #1
Clearasil
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Clearasil is an unknown quantity at this point
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..
Clearasil is offline   Reply With Quote
Old 10-29-2012, 01:48 PM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,493
Thanks: 44
Thanked 438 Times in 427 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
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).
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
Clearasil (10-31-2012)
Old 10-29-2012, 01:59 PM   PM User | #3
Clearasil
New to the CF scene

 
Join Date: Oct 2012
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Clearasil is an unknown quantity at this point
Ok thanks, I'll try that
Clearasil is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:01 PM.


Advertisement
Log in to turn off these ads.