I have drop down in pop up window where users can choose their country. I am using hidden field to submit choice with php and when I hit "submit" button I get the country which was chosen. Information about country is shown in the header so it should be on every page but when I start to browse between pages the country value dissapeares. How can I keep it on every page?
<div class="field">
<div class="input-box">
<?php $_countries = Mage::getResourceModel('directory/country_collection')->loadData()->toOptionArray(false); ?>
<?php if (count($_countries) > 0): ?>
<select name="country" id="country" onchange="print(this.value)">
<option value=""> </option>
<?php foreach($_countries as $_country): ?>
<?php if(!in_array($_country['value'], $arrNO)):?>
<option value="<?php echo $_country['value'] ?>" >
<?php echo $_country['label'] ?>
</option>
<?php endif;?>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
<form action="" method="post">
<input id="choice" type="hidden" name="fname" value=""/>
<input type="submit" value="OK"/>
</form>
<div id="usWarning"><p><span class="red">NOTE</span>: If you live in <span class="red">Arizona</span>, <span class="red">Iowa</span>, <span class="red">Maryland</span>, <span class="red">Oklahoma</span>, <span class="red">South Dakota</span>, <span class="red">Vermont</span>, <span class="red">Washington</span> or <span class="red">Wisconsin</span>, we are unfortunately not allowed to sell tobacco to you. Its forbidden with online sales of tobacco in these states.</p></div>
</div>
"Value" for the hidden input field assigned with the help of JS:
function print(value) {
document.getElementById("choice").value=value;
}
Thank you for your help.
To preserve a form value across requests as a hidden field simply means echoing it back into the form when rendering it.
<input id="choice" type="hidden" name="fname" value="<?php echo htmlspecialchars ($_POST ['fname']); ?>" />
Alternatively you can use sessions or cookies to propagate the value to other pages.
in action page
$_SESSION['country'] = $_POST['country']
in form
<option value="<?php echo $_country['value'] ?>" <?php if(isset($_SESSION['country']) && $_SESSION['country']===$_country['value']) echo "selected"; ?>>
echo the session value(if set) to the hidden field too
You could make a session variable, to make the value persist across pages.
$_SESSION['countryname'] = "countryname";
Sessions
Edit:
As you are using Sessions for a different purpose, set a cookie, separately, subject to its own expiry. using setcookie.
Related
I created a form, where my inputs are created by a foreach loop with data coming from a mysql database.
When the form is submitted with method post, I want to be able to receive all the info through $_POST, but currently I'm only receiving the info from the last created input in the foreach loop.
The variable $fishes comes from mysql database and contains 8 rows, so the foreach loops 8 times.
My goal is to insert this data in a database table, so I can recall it.
When i run my XDebug after pressing submit, my $_POST contains action: insertFish and quantity: (value from input of last iteration in foreach).
I would also like to be able to see the label with the corresponding quantity. I thought by adding a label would work but it didn't.
My form:
<form class="fishadd__form" action="index.php?page=fishtanks&id= <?php echo $_SESSION['tank_id']; ?>" method="post">
<input type="hidden" name="action" value="insertFish">
<div class="fishadd__box--wrapper">
<?php foreach($fishes as $fish): ?>
<div class="fishadd__box">
<h2 class="fish__name"><?php echo $fish ['name']?></h2>
<img class="fish__picture" src="../src/assets/img/goldfish.svg" alt="Goldfish" width="125">
<div class="item__counter">
<div class="fishadd__bottom">
<button class="minus__button">-</button>
<label for="quantity" name="<?php echo $fish['name']?>">
<input class="fishadd__input" type="number" name="quantity" min="0" value="0">
</label>
<button class="plus__button">+</button>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<button type="submit" class="add__fish" value="submit">add to tank</button>
</form>
Thanks to the comments, unique names for the input fields was the fix. I solved it with an array that is looped inside the foreach:
<input class="fishadd__input" type="number" name="<?php echo $fish['name']?>" min="0" value="0">
The scenario is - I have a form with multiple input fields and text areas and whenever I post this form all these values will get posted. However, I also have a dropdown that I create from my database. This dropdown value shows up properly when I get the values. I have a second submit button, which should take this selected value and post to other page.
I have tried the $_POST='name of select option' but it did not help.
I have a onlick for directing the form to other page for updating the db.
I'm fairly new to php, so it could be the use of _POST that could be incorrect.
<form name="f1" class="formoid-solid-blue" method="GET">
<div class="title">
<h2></h2>
<h2>Tracking & Receiving</h2>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="small" type="text" name="store" placeholder="Store #"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="medium" type="text" name="userid" placeholder="UserId"/>
<span class="icon-place"></span>
</div>
</div>
<div class="element-input">
<label class="title"></label>
<div class="item-cont">
<input class="large" type="text" name="order" placeholder="Order Number"/>
<span class="icon-place"></span>
</div>
</div>
<div class="submit">
<input type="submit" name="Send" value="Send"/>
</div>
<div class="element-separator">
<hr>
<h3 class="section-break-title">Tracking Numbers</h3>
</div>
<div class="element-multiple">
<label class="title"></label>
<div class="item-cont">
<div class="large">
<select data-no-selected="Nothing selected" name="updTR" multiple="multiple">
<option name="op" value="<?php require 'connection.php'; ?>"> //getting value form db
<?php
echo $trackID; //DB value
$trackID = $_GET['updTR']; //getting the variable from the form
?></option>
</select>
<span class="icon-place"></span>
</div>
</div>
</div>
<div class="submit">
<input type="submit" onclick="f1.action='UpdateTR.php'; return true;" name="UpdateTR" value`enter code here`="Submit"/>
</div>
</form>
Well, looking at the form, you said you are using POST, and tested with other POST related method, but your form is using GET, as seen on your code above.
Since you are new to PHP, as an example, if you are using post, and a variable is waiting on the other page to collect this information from your form then you do it like this:
This is the form field example:
<input type="text" name="XYZ">
Then on the page that will collect this info, it would be
$someVariable = $_POST[ 'XYZ' ];
now, if you want to use GET, then its the same thing but you use this
$someVariable = $_GET[ 'XYZ' ];
hope this clears the confusion.
-- EDIT 2 --
Ok after reading your comment, and since i haven't seen how you are iterating through your DB for the options that go in that "option" list/menu, I'm going to say you cant put "connection" as the value on this part:
<option name="op" value="<?php require 'connection.php'; ?>">
because assuming that "connection.php" is connecting to the DB, then that wont help you, that goes elsewhere. Instead, once you've made that connection(elsewhere, preferably above it in the header somewhere), you then have to have code that loops through the DB, and "WHILE" its looping, spit out results into that options field. Let me give you an example with PSEUDO code.
If a normal select/option code looks like this
<select>
<option> option 1 </option>
<option> option 2 </option>
</select>
etc, then you need php that loops through your DB results, and puts the results into the "options" in that select,
here is some pseudo code:
<select>
<?php
while($row = your fetch code here){
echo "<option>your line items here</option>";
}
?>
</select>
OR.....
if "$row" has a specific value you want to use from the database that you visually want to add in that list, then you could do similar to above but with something like:
<select>
<?php
while($row = your fetch code here){
echo "<option>your line items here "' . $row['some value from DB here'] . '"</option>";
}
?>
</select>
etc.
Essentially, you want your "while loop" to iterate through your database and WHILE its doing it, to input its data into your options html.
ALSO, if you wanted a particular value from your DB, put into a GET variable, to use for the processing page (as i mentioned all the way above), then again, similarly, you can do something like this:
<?php
while($row = your fetch code here){
echo "<a href='linkHere.php?yourVariableToTrackWithGetHere='".$row['yourDBresutlToTrack']."'> Your text here </a>"; }
?>
This way, when you click the link, the database value gets added to that variable that you can call later in the processing page.
Kinda long winded but, this should set you 100% straight.
Hope this helps.
I ve read a few question on the site similar to this but couldn't really take anything from them. I have a form that gets submitted, if there are errors, I notify the user of the errors above the form and display the form below that for them to correct it. This is all good and dandy except that they have to re enter all of their information again. I want the information to stay, and them to only have to fix / reenter the field that the mistake was in.
Here is my handling file:
<?php
include_once("Header.php");
if(!empty($_POST['formsubmit'])){require_once ("Form_Handle.php");}
include("Form.php");
include_once("Footer.php");
?>
is there something that i have to do when I include the form.php after I handle it?
I felt the answers provided are somewhat vague and also incorrect in the sense they tell you to set the contents of the fields inside the value attribute based on whether or not values exist for those fields.
Here's why. When you put if statement logic inside the value attribute, it either sets the value attribute to 'YOURVALUE' or '""'. <- That is the problem. The value of the field gets set to empty string when $_POST["field_name"] is empty. If you had form validation and were hoping to throw an 'empty field' error, this would pass your validation logic, which would be completely incorrect (Your form field will appear empty, but there would be an empty string inside it).
Instead, just echo the variable without any checks. If it's not set, nothing will happen. If it is, you will be able to retain the value. For example,
<input type="text" name="email" value="<?php echo $_POST["email"]; ?>" >
Here's another example of code where I forgo the whole above situation and only echo the value attribute if the variables are not empty.
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name" <?php if (!empty($_POST['name'])) {echo "value=\"" . $_POST["name"] . "\"";} ?> >
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" <?php if (!empty($_POST['email'])) {echo "value=\"" . $_POST["email"] . "\"";} ?> >
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Website: <input type="text" name="website" <?php if (!empty($_POST['website'])) {echo "value=\"" . $_POST["website"] . "\"";} ?> >
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
Comment: <textarea name="comment" <?php if (!empty($_POST['comment'])) {echo "value=\"" . $_POST["comment"] . "\"";} ?> rows="5" cols="40"></textarea>
<br><br>
Gender:
<input type="radio" name="gender" value="female" <?php if ($_POST['gender']=="female") {echo "checked";} ?> >Female
<input type="radio" name="gender" value="male" <?php if ($_POST['gender']=="male") {echo "checked";} ?> >Male
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
In relation to the actual question that the user asked, you would need to add similar code into your form.php file.
You just need to write some logic in your input field value for keeping entered value. e.g
<input type="text" name="login" value="<?php if(isset($_POST['login'])){ echo $_POST['login'];}?>">
In your Form.php you should check the presence of various $_POST vars which you use and put their contents as value of input fields
In your form assign default values.
For example:
<?php
$answer = (isset($_POST['answer'])) ? $_POST['answer'] : '';
?>
And the form field:
<input type="text" name="answer" value="<?php print $answer;?>"/>
Why not store the posted data in a session variable.
Then when you redirect the user back to the form page, you can check to see if the form values are in the session variable, if so fill in the contents as the value in the input field.
In my Codeigniter website I've made a searchform. When I search the page is redirected to a 'searchresults' page.
I always get a lot results and I want to minimize that. I would like to add searchfilters to the 'searchresults' page, so I can search on specific categories/keywords.
I've been searching for this the whole day.
What I've tried is adding a dropdown and a input field to the searchresults page. The dropdown is populated with the categories found with the normal searchform. This works, but I can't filter and limit the searchresutls when I submit the 'second searchform'.
My form on the 'searchresults' page looks like this:
<script type="text/javascript">
$(document).ready(function(){
$(".zoekfilters").hide();
$(".filters").show();
$('.filters').click(function(){
$(".zoekfilters").slideToggle();
});
});
</script>
<div class="zoekfilters">
<br/>
<form action="home/get_filtered_vac" method="post">
<p class="field">
<label class="field">Categorie:</label>
<select name="catselect" id="selection" value="Categorie">
<option selected disabled value="">Selecteer een categorie</option>
<?php foreach($all as $dat ){ ?>
<option value="<?php echo $dat['categorie']; ?>"><?php echo $dat['categorie']; ?></option>
<?php } ?>
</select>
</p>
<br/>
<p class="field">
<label class="field">Tags:</label>
<input name="tag" type="search" placeholder="Tags">
</p>
<br/>
<p class="field">
<label class="field">Aantal uren:</label>
<?php foreach($all as $dat){ ?>
<?php echo $dat['uren']; nbs(2); ?>
<input type="checkbox" value="<?php echo $dat['uren']; ?>">
<?php } ?>
</p>
<br/>
<p class="field">
<input type="submit" value="Filter resultaten">
</p>
</form>
You can try it yourself, so the idea is clear:
Go to: http://kees.een-site-bouwen.nl/vacatures
leave the big searchbox empty and add '9101' to the small searchbox, Now wait a second an then submit the form. You will be redirected to http://kees.een-site-bouwen.nl/vacatureresults
Click on 'Zoekfilters' to open the second searchfrom on the 'searchresults' page.
If people need my model functions I will post them.
Hopefully I'm making this more difficault then it has to be. Here is what I'm trying to do. I have a form that does a POST and returns data. I have a second form that then asks the user a yes/no question based on data from the first form. Is it possible to capture the POST data from the first form submission and pass it along with the second form POST?
Here is my scenario
if ($_POST['button_1']) {
$params = $_POST;
print_r($_POST);
// process form data
}
if ($_POST['button_2']) {
// Retain the POST data from the first submission
$new_params = $params . $_POST;
print_r($new_params);
// process form data and do some additional stuff
}
<form id="form_1" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_1" id="button_1"/>
</form>
<form id="form_2" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
...
<input type="submit" value="Button" name="button_2" id="button_2"/>
</form>
Is there a way to do this easily or am I over complicating this?
You can either use a hidden field in your second form, or sessions. Start reading here:
http://www.php.net/manual/en/book.session.php
Yes, the standard way is to use type = "hidden" fields to pass this context data onto the second form. There are lots of worked examples to see how to do this. View the source of this HTML page or any other with forms on them and search for "hidden" to see how the applications do this.
There are a few ways to "fake" this.
Have the first form submit to itself and just load the $_REQUEST variables and use them to populate the second form with the appropriate data/options.
have the 2nd form loaded via ajax once the first is submitted and use javascript to grab the current form variables and provide them to the ajax function.
Which method would you prefer?
UPDATE:
This is long, but a working example
<!DOCTYPE html>
<html>
<body>
<form name="first" method="post">
<input type="hidden" name="action" value="firstformdone">
<div style="width:100px;float:left;">
Age:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="age">
</div>
<div style="clear:both;"></div>
<div style="width:100px;float:left;">
Name:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<input type="text" name="name">
</div>
<div style="clear:both;"></div>
<br>
<?PHP
if (!$_REQUEST['action'] == "firstformdone") {
?>
<input type="submit" value="contine">
<?PHP
}
?>
</form>
<?PHP
if ($_REQUEST['action'] == "firstformdone") {
?>
<form name="second" action="something_else.php" method="post">
<input type="hidden" name="age" value="<?PHP echo $_REQUEST['age']; ?>">
<input type="hidden" name="name" value="<?PHP echo $_REQUEST['name']; ?>">
<div style="width:150px;float:left;">
Preferred games:
</div>
<div style="width:200px;float:left;margin-left:20px;">
<select name="games">
<option value="">Select games</option>
<?PHP
if ($_REQUEST['age'] <= 10) {
?>
<option value="tlddlywinks">Tiddly Winks</option>
<option value="Jacks">Jacks</option>
<option value="Go-Fish">Go-Fish</option>
<option value="Hid-And-Go-Seek">Hid-And-Go-Seek</option>
<?PHP
} else {
?>
<option value="Halo">Halo</option>
<option value="StarWars">The Old Republic</option>
<option value="LaserTag">Laser Tag</option>
<option value="spin-the-bottle">spin-the-bottle</option>
<?PHP
}
?>
</select>
</div>
<div style="clear:both;"></div>
<br>
<input type="submit" value="Next!">
</form>
<?PHP
}
?>
</body>
</html>
You need to package the data from the first form up for resubmission. You can use a hidden fields for that job:
foreach ($_POST as $key => $value) {
print "<input type='hidden' name='".htmlspecialchars($key, ENT_QUOTES, "UTF-8")."' value='".htmlspecialchars($value, ENT_QUOTES, "UTF-8")."'>";
}