Preserving previous while adding new one on a form - php

I have this form that comes with some parameters from another page. I want to preserve those values and add a sortby parameter, but every time I hit submit all the parameters disappear, but the new sortby parameter.
How can I preserve the parameters from the previous page and add or change just the orderby parameter.
<form name="formSearch" action="<?php echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']; ?>" method="GET">
<select name="order_by" id="order_by">
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 1) { echo "selected"; } ?> value="1">Ultima Modificacion (Reciente)</option>
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 2) { echo "selected"; } ?> value="2">Ultima Modificacion (Viejo)</option>
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 3) { echo "selected"; } ?> value="3">Precio (Mayor to Menor)</option>
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 4) { echo "selected"; } ?> value="4">Precio (Menor to Mayor)</option>
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 5) { echo "selected"; } ?> value="5">Marca/Modelo (A to Z)</option>
<option <?php if( isset($_REQUEST['order_by']) && $_REQUEST['order_by'] == 6) { echo "selected"; } ?> value="6">Marca/Modelo (Z to A)</option>
</select>
<input name="submit" type="submit" />
</form>

Add
<INPUT type='hidden' name='OPT1' VALUE='<?php if( isset($_REQUEST['OPT1'])) { echo $_REQUEST['OPT1']; } else { echo "" } ?>
since you need to pass them as hidden input fields.
OPT1 is the name of the parameter to preserve - add as many of these as you have parameters
If you wish for the fields/values to actually show, then :
Drop type='hidden' to display them and be editable
Make them disabled input fields to display them and not be editable.
However those 2 options need to be done cleanly, e.g. if the old value came from radio button, you need to display and pre-populate same radio button setup, etc...

It seem that is the way "form" work if you use the method "GET", So, if I need to preserve the values and need to use "GET" I would need to add a bunch of hidden fields like it was suggested. But I was trying to avoid that. If someone knows a better way, please let me know, if not this would be a solution:
" method="GET">
$v){
echo ''."\n";
}
?>
Now, If you don't need to use "GET" in the result form, just change the method to "POST" and it will work fine.
Note if you are coming from another form, that form needs to be "GET" for this to work.
Search Page -> Use Get
Result Page w/ Sorting form -> Use Post

The simplest way to persist the data would be to make sure the fields responsible for populating the values in question are present on the current page. If they're not supposed to be visible to the user anymore on the page in question, you can set them to <input type="hidden" name=fieldname" value="value_set_on_previous_submit"/> . Or if they should still show, just ensure that their values are set to the submitted ones.

Related

Posting corresponding datas on clicking submit button

for($i=0;$i<2;$i++)
{
?>
<tr>
<td>
<select name="sel_<php echo $i; ?>" id="sel_<php echo $i; ?>">
<option value="1">A</option>
<option value="2">B</option>
</select>
</td>
<td>
<input type="submit" name="sub_<?php echo $i; ?>" value="submit" />
</td>
</tr>
<?php
}
?>
Now I have 2 select boxes sel_1 and sel_2, each of them have corresponding submit button sub_1 and sub_2. I want to post the corresponding data when a submit button is pressed. For example: when I press sub_1 I need to post the value of sel_1.
So, if press on the submit button in a specific row, I need to post the value of the select box in that row.
How can I do this?
It doesn't matter that both select are sent. Just work with the one select you need.
if (isset($_POST['sub_0'])) {
$data = $_POST['sel_0'];
} elseif (isset($_POST['sub_1'])) {
$data = $_POST['sel_1'];
}
In $data will be value of the first, or the second one, select.
IF you need to send just one selectbox, you need more forms (each form will contain select and submit).
since you have not mentioned the form method i am using $_REQUEST here,
if (isset($_REQUEST['sub_0'])) {
// use the value of $_REQUEST['sel_0'];
} elseif (isset($_REQUEST['sub_1'])) {
// use the value of $_REQUEST['sel_1'];
}
Note: you can replace $_REQUEST with $_POST or $_GET as per you form method type or else you can use $_REQUEST itself if you wish to do so

make an option from a selection tag do action on the whole site PHP

iamiI have a selection tag and and options that can do task in the page , but the problem is that it just does that in just the index page and not and the others... this is my code:
region.php
(isset($_POST["company"])) ? $company = $_POST["company"] : $company=1;
if(isset($_POST['selectedRegion'])){
$region = explode( "|" ,$_POST['selectedRegion']) ;
$_SESSION['regCode'] = $region[0];
$_SESSION['selection'] = $region[1];
$regionCode = $_SESSION['regCode'];
}else if(isset($_SESSION['regCode'])){
$region[1] = $_SESSION['selection'];
$regionCode = $_SESSION['regCode'] ;
}else{
$regionCode = 'DEF';
$_SESSION['regCode'] = $regionCode;
}
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<select id="selectedRegion" name="selectedRegion" size="1" autocomplete="off">
<option style="border-bottom: 1px dashed #000" value="<?php if (isset($_SESSION['regCode']) and $_SESSION['regCode'] != 'DEF'){ echo "".$_POST['selectedRegion']."";}?>" selected="selected"><?php if (isset($_SESSION['regCode']) and $_SESSION['regCode'] != "DEF"){ echo "".$region[1]."";}else{echo "--------";} ?></option>
<option value="ny">New York</option>
<option value="miami">Miami</option>
</select>
<input type="submit" value="Enter Region" />
</form>
process.php
<?php if(!isset($_SESSION)){ session_start();}
if(isset($_POST['selectedRegion'])){
$region = $_SESSION['selectedRegion']=$_POST['selectedRegion'];
if($region == "ny"){
echo "new york selected";
}
if($region == "miami"){
echo "miami selected";
}
?>
The index.php and all the other pages of the site have the region.php and the process.php included, but my problem is that any time that i do a selection it just does the action on the page that is in... and if i want the action to be made in another page i would have to press the option again. is there anyway that i can do the action of my selection work on page change and all the pages of my site? thanks
The solution you are looking for consists of these steps:
you evaluate the selection you receive at session start from the client, according to what the user has chosen. So just what you have so far.
you store that selection in a session variable. See the php documentation for the basic use of a session variable. It just boils down to:
session_start();
$_SESSION['region'] = $_POST['selectedRegion'];
you create a switch statement based on that session variable in all scripts that should react on that user choice:
session_start();
switch ($_SESSION['region']) {
case 'ny':
// do something
break;
case 'miami':
// do something
break;
default:
// do something
}
With this the user has to make his choice only once inside a single session (returning requests). You can offer that selection on the entry page and/or on all pages, just as you like. You can enhance that by storing that inside a persistent user profile, if you have an account management.
In addition some more information, as a reaction to your additional information as you provided by editing your question.
Your general approach is probably into the right direction, great. There are a few issues you have to correct though. I can only hint to them, since you offer only snippets of your scripts.
In 'process.php' you have this line:
$region = $_SESSION['selectedRegion']=$_POST['selectedRegion'];
This is obviously invalid syntax, you cannot have two assignments inside a single statement. At least that does not do what you want it to.
not sure if this conditional really belongs there:
if (isset($_POST['selectedRegion']))
It is fine if 'process.php' is the script meant to SET the session variable. In all other scripts obviously $_POST['selectedRegion'] is not set, since the user did not make a choice / was not offered a choice. So there you cannot test or use that variable, you just use the session variable directly.
there are a few other things, but as said you are on the right track.
This approach does work, it is the "normal" approach to this. So don't get frustrated, you will sort this out. In general: your code is a little confusing, too many separate variables where it is unclear what purpose they save. Why the distinction between region, regionCode and selectedRegion. Why isn't a single variable sufficient? Try to simplify your code, this will help to get this to work.
Value of $_POST['selectedRegion'] will not be available always. You should store this value in session like $_SESSION['selectedReg']=$_POST['selectedRegion']; during the form submission.
<form id="regionSelect" action="<?php $_SERVER['PHP_SELF'] ?>" method="post" onchange="this.form.submit();" >
<option style="border-bottom: 1px dashed #000" value="<?php if (isset($_SESSION['regCode']) and $_SESSION['regCode'] != 'DEF'){ echo $_SESSION['selectedRegion'];}?>" ><?php if (isset($_SESSION['regCode']) and $_SESSION['regCode'] != "DEF"){ echo $_SESSION['selectedRegion'];}else{echo "--------";} ?></option>
<option value="ny">New York</option>
<option value="miami">Miami</option>
</select>
<input type="submit" value="Enter Region" />
</form>

Remembering Form Checkbox State With Initial Default Value

I've got a form with a checkbox. When a user first visits the page, I want the checkbox to be 'checked'. However, if they uncheck the box and then submit the page, I want it to remain unchecked (and to remain checked if they submit the page with it checked).
To determine when it has been checked and the form submitted, I'm currently doing:
<input type='checkbox' class='seeAll' name='seeAll' value='true' <?php if ($_POST['seeAll'] == 'true') echo checked; ?>>
This works great for leaving the box checked when needed, however, how would I go about ensuring it stays unchecked if they submit it that way, while also being checked if they revisit the page (such as by re-entering the URL)?
Thanks!
I don't know why it took me so long to come up with this answer, but after struggling with this, I realized I could just check the value of the checkbox via the $_POST, as I was doing before and could check if the user arrived at the page by some means other than the submit button by doing this:
<?php if(($_POST['seeAll'] == 'true') || !isset($_POST['submit'])) echo checked; ?>
If the user submitted the form, than isset($_POST['submit']) will be true and so if that's the case and $_POST['seeAll'] is empty, them obviously the user submitted an unchecked box. If isset($_POST['submit']) is false, then the user arrived on the page without submitting the form and I should check the checkbox as the 'default'.
So then my whole <input> tag looks like this:
<input type='checkbox' class='seeAll' name='seeAll' value='true' <?php if(($_POST['seeAll'] == 'true') || !isset($_POST['submit'])) echo checked; ?>>
Works just as I need!
NOTE:: This is differs from OP's question because it will remember the value of the checkbox even if the user goes away from the page (say, to www.facebook.com) and then back to the page. The OP wanted it to only remember the value of the checkbox when the page was posted to.
If you want a non permanent method you can use $_SESSION :
<?php
if (!isset($_SESSION)) {
session_start();
}
if ($_POST['seeAll'] == 'true' || !isset($_SESSION['seeAll'])) {
$_SESSION['seeAll'] = true;
} else {
$_SESSION['seeAll'] = false;
}
?>
<form method="POST">
<input type='checkbox' class='seeAll' name='seeAll' value='true'
<?php if ($_SESSION['seeAll']) echo 'checked'; ?>
/>
<input type='submit'/>
</form>
see: http://php.net/manual/en/session.examples.basic.php
One solution is to use both $_SESSION[""] and $_POST[""].
(See explanation and code below)
EXPLANATION
If $_SESSION["loadcount"] is not set, set it to 0.
With each if (isset($_POST["submit"], increase $_SESSION["load count"] by +1.
In the 'checked' option of the input type="checkbox", echo PHP. If if ($_SESSION["loadcount"] == 0) echo 'checked'. (Sets the checkbox to an initially checked state.)
Else, if (isset($_POST["agree"] echo 'checked', to remember the state checked by the user. (Remember the checked state set by user.
CODE
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php // session_firstload.php
// Check if $_SESSION is not set, set $_SESSION loadcount to 0
if (!isset($_SESSION["loadcount"]))
$_SESSION["loadcount"] = 0;
// When the user submits using POST Method
if (isset($_POST["submit"]))
{
// Increase $_SESSION["loadcount"] by +1
$_SESSION["loadcount"] = $_SESSION["loadcount"] + 1;
}
// Echoing for Debugging / Understanding
echo $_SESSION["loadcount"];
?>
<form action="session_firstload.php" method="post">
<input id="agree" type="checkbox" name="agree"
<?php
// If Check the Checkbox is $_SESSION["loadcount"] == 0
// Else 'remember' with (isset($_POST["agree"]
if ($_SESSION["loadcount"] == 0)
echo "checked";
else
{
if (isset($_POST["agree"]))
echo "checked";
}
?>
>I Agree<br>
<input type="submit" name="submit">
</form>
</body>
</html>

how to select option to submit different php page

drop down select "sales" go to sales.php, other options selected go to addFund.php...
<form name='searform' method='post' action='<?php echo $home;?>addFund.php'>
Search : <input type='text' id='sear' name='sear'> by
<select id='psel' name='psel' onchange='change()'>
<option value='email'>Email</option>
<option value='name'>Username</option>
<option value='domain'>Domain name</option>
<option value='sales'>Sales</option>
</select>
<input type='submit' name='sub' id='sub' value='Go' onclick='gopage()'>
<input type='submit' name='dir' id='dir' value='Direct'>
</form>
How to submit different pages
You could use some Javascript nastiness to achieve this with the change() function, but the usual way to do this is to route all requests through a controller and include() the appropriate page. For example, point your form to action.php, and in that file, do this:
action.php
<?php
if (isset($_POST['psel']) && $_POST['psel'] == 'sales') {
include 'sales.php';
} else {
include 'addFund.php';
}
...or you could just put roughly that code into addFund.php, since you only seem to have one other script that you would want to send requests to.
You could do this with javascript:
function change(el) {
if(el.value === 'sales') {
el.form.action = 'sales.php';
} else {
el.form.action = 'addFund.php';
}
}
Change the onchange to onchange="change(this)". A better way would be to check the variable on serverside and include the right file.
Change your select to this:
<select name="vote" onChange="document.forms['searForm'].submit()">
And make your form action go to something like pageChange.php where it returns a different page depending on the $_POST value.

having PHP/javascript create a website based on options picked

I want the user to be able to pick two different variables from a website (from a drop down menu) and hit a button to bring them to a page where files are to download based on the variables picked.
I have the html ready to go.. and i have both menus in an array in php.. i was wondering how to pass both variables through to another site and then have unique content depending on which ones picked..
how do i get php to make it's own site?
Try something like this:
page1.php
<?php
$ar = array('foo', 'bar');
?>
<form action="page2.php" method="post">
<select name="choice">
<?php foreach($ar as $value) { ?>
<option value="<?php echo $value ?>"><?php echo $value ?></option>
<?php } ?>
</select>
<input type="submit" value="go to page2">
</form>
page2.php
<?php
$choice = $_POST['choice']; // sanitize as needed
if( $choice === 'foo' ) {
// do foo choice
}
else if( $choice === 'bar' ) {
// do bar choice
}
?>
Query strings are also ok, just change the form method to GET, and $_POST to $_GET.
have you tried using query variables? og perhaps posting the info to the page?
I would suggest using the query like: http://www.yourdomain.com/speciallayoutpage.php?layout=2&colortheme=1

Categories