Re: How to keep $_POST value when I have 3 submit options?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Oct 23, 2014, at 3:06 PM, Negin Nickparsa wrote:

Sincerely
Negin Nickparsa

On Thu, Oct 23, 2014 at 11:01 AM, Jeffry Killen <jekillen@xxxxxxxxxxx>
wrote:


On Oct 23, 2014, at 10:10 AM, Negin Nickparsa wrote:

I have items showing up in list view or grid view.
I have 2 buttons List and Grid
and Items will show up in Grid view and List view
also I order them in a select tag by price and other options

when I sort by name I want to remember the state of view
whether it is grid or if it is list

user selects Grid view and then wants to order them by price. but when
they
order by price booom!(submitted) they will go to default view which was
List view.It is not correct so bad.


How does the user designate the view he wants to see?
It appears that there must be a way, via javascript ajax call*
, to tell the server
initially what the preferred view is, so when the page reloads the correct
view
is displayed.

*or get/post sent to the page that is being viewed. I would do this as
<?php
// in the top of the page being viewed.

$_file_self = basename($_SERVER['PHP_SELF']);
$_view = '';
if($_GET['view'])
{
 $_view = $_GET['view'];
}
?>

The

<a href="<?php print $_file_self ?>?view=grid">Grid View</a>
<a href="<?php print $_file_self ?>?view=list">List View</a>

You may also want to save the view preference as a session variable
or have code write a preference file for the user.
Is the user required to have an account? If so, include a variable that
designates the default view preference.

​using get method can be a way to achieve it however I prefer to use post
method.

​can you give me hints about ajax parts? maybe I can do something with that. for session they are so strict not using session for the sake of speed and
security.​

If your not familiar with javascript, doing it with ajax will be a challenge. Ajax is a method of sending ansyncronous calls to the server and receiving a reply. The page does not reload so if the reply contains string content or change of state in the client, you have to handle it with javascript programming. With ajax, also you will need a separate sever side script to handle the request. I have tried to use ajax with self processing forms and pages but haven't been able
to master that.

Using get method I believe is the simplest. If you use post, you will need a form
to submit as I understand it.

<form name="viewPref" name="viewPref" action="<?php print $_file_self? >" method="post">
View Preference:<br>
Grid View <input type="radio" name="pref" id="grid" value="grid"><br>
List View <input type="radio" name="pref" id="list" value="list"><br>
<input type="submit" name="UserPrefs" value="Set Your View Preferences">
</form>

Notice the radio buttons both have the same name. You would look for $_POST['pref'] and it will have the value that the user selected. With the id values set you can use javascript to apply specific actions. I don't advise using text fields in the form because garbage and spam can be submitted without screening. All the elements in the page will have to have DIFFERENT id values applied. Accept in radio button and checkbox groups, all elements will also have to have different names. The name and id value of a single element can be the same (accept in radio and checkbox groups).





I have an event onchange that when I click on selection the form will be
submitted
I want to keep the values so that it can remember whether it should sort
the items in grid view or list view

the live example can be seen here:

http://www.lenmar.com/general-purpose/aa/aa/nickel-metal-
hydride/nickel-metal-hydride-battery

this is the older version which is working with Get method but the test server is with $_POST I am showing the concept to help clarifying the
problem

as far as I attempted, I got the hidden values for $_POST
and then I pass them to the form but it still has problem
here is the sample code:


<form method="post" action="" id="myform">
<div id="gridSort">
<input type="hidden" name="selected_sort" value="<?php echo
!empty($_POST['sort']) ? strip_tags($_POST['sort']) : ''; ?>" />
 <input type="hidden" name="selectionList_view" value="<?php echo
!empty($_POST['Listview']) ? strip_tags($_POST['Listview']) : ''; ? >" />
<input type="hidden" name="selectionGrid_view" value="<?php echo
!empty($_POST['Gridview']) ? strip_tags($_POST['Gridview']) : ''; ? >" />
<span>View Results As:</span> <span>
<input type="submit" class="listButtons" name="Lisview" value="List">
</span> <span>
<input type="submit" class="resultButtons" name="Gridview"
value="Grid"></span>
<select id="sortSelect" class="sortSelect" size="1" name="sort"
onchange="this.form.submit();" >
<option selected>Sort</option>
<option value="Name">Name</option>
<option value="PriceLowToHigh">Price - Low</option>
<option value="PriceHighToLow">Price - High</option>
</select>
</div>
</form>
<script type="text/javascript">
document.getElementById('sortSelect').value ="<?php if(!
$_POST['sort']):?>"Sort"<?php else: echo $_POST['sort']; endif;? >";
</script>
<pre>
<?php print_r($_POST); ?>
</pre>


I select the price high to low or something else I want it to remember
what
I had chosen before that list? or grid?

my question is that is it possible to have just 1 form and submit and keep
them? hidden field didn't work as I tested.

Any help would be appreciated.

Sincerely
Negin Nickparsa





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php






[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux