...

View Full Version : Resolved Keeping settings, two forms and two submit buttons



Clearasil
10-29-2012, 02:28 PM
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?



$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
}

tangoforce
10-29-2012, 02:48 PM
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).

Clearasil
10-29-2012, 02:59 PM
Ok thanks, I'll try that :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum