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.
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">
I am very new to PHP and I am trying to display the value of a selected radio button in a string. But all I get returned is "on" instead of the value associated with the selection. Any help would be great!
<form action="Handle_form_PHP.php" method="post">
<div class= "row">
<div class ="col-25">
<label for="student">Student</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value ="student">
</div>
</div>
<div class= "row">
<div class ="col-25">
<label for="student">Employee</label>
</div>
<div class ="col-75">
<input type="radio" name="student" value = "employee">
</div>
</div>
<div class="row">
<input type="submit" value="Submit">
</div>
</form>
This is the PHP I am using to print:
// Create a shorthand for the form data:
$fname = $_REQUEST['firstname'];
$lname = $_REQUEST['lastname'];
$address = $_REQUEST['address'];
$state1 = $_REQUEST['state1'];
$state2 = $_REQUEST['state2'];
$student = $_POST['student'];
// Print the submitted information:
echo "<p>Thank you, <b> $student </b>, <b>$fname $lname</b>, for the filling out our survey:<br>
<p>We will Send you your report at: <i>$address</i>, <i>$state1</i><i>$state2</i>.</p>\n";
If your $_POST['student'] superglobal variable was holding a value of 'on', then the issue has nothing to do with method case-sensitivity.
The default value for a radio button is 'on' unless you provide a value attribute inside the radio button tag.
Let me prove it to you. Go to [cringe] https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_form_checkbox then paste the following snippet in the left-side box, then Run, then Submit.
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" method="get">
<input type="radio" name="student" checked> Student<br>
<input type="submit" value="Submit">
</form>
</body>
</html>
This is submitting with a method of get (notice the lowercase get is inconsequential. post will behave the same way). See that without a value in the student radio input, the submitted value is on.
If you write value="Red42" inside the radio input, you will no longer receive on; the submitted value is Red42.
use POST not post when saying method:
<form action="Handle_form_PHP.php" method="POST">
not
<form action="Handle_form_PHP.php" method="post">
For the record, the book was incorrect and had "post"
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.
I am trying to dynamically add fields with jquery for a project. My problem is that when I send the form to the processing script and do a print_r() on the $_POST['item'] it only lists the last array...
Here is my code:
<div class="itemAdd row">
<div class="span4">
<p>Item Number: <br /><input class="span4" type="text" name="item[itemNumber]" /></p>
<p>Status: <br />
<select name="item[status]" class="span4">
<option value="enabled" selected="selected">Enabled</option>
<option value="disabled">Disabled</option>
<option value="damaged">Damaged</option>
</select>
</p>
</div>
<div class="span4">
<p>Item Notes:<br /><textarea class="span4" style="height:100px;" name="item[notes]"></textarea></p>
</div>
<div class="clearfix"></div>
<hr />
</div>
This button adds new fields:
<i class="icon-plus icon-white"></i> Add Item
Here is the jquery to add new fields:
<script>
//Add Items
$('a#addItem').on('click', function(event) {
event.preventDefault();
$('#productItems').append('<div class="itemAdd row"><div class="span4"><p>Item Number: <br /><input class="span4" type="text" name="item[itemNumber]" /></p><p>Status: <br /><select name="item[status]" class="span4"><option value="enabled" selected="selected">Enabled</option><option value="disabled">Disabled</option><option value="damaged">Damaged</option></select></p></div><div class="span4"><p>Item Notes:<br /><textarea class="span4" style="height:100px;" name="item[notes]"></textarea></p></div><div class="clearfix"></div><hr /></div>');
});
You are using arrays in your html, but the index is a string:
item[itemNumber]
So every item will overwrite the previous one.
You could change it to:
itemNumber[]
and
itemNote[]
or, if you have a numeric ID, you could use that.
You're forcing array keys in your form fields, et.c.:
... name="item[itemNumber]" ...
itemNumber isn't a JS var, it's literal html/attribute text in the html blob you're appending. Perhaps you meant something more like
... name="item[' + itemNumber + ']" ...
instead. Even just item[] would allow PHP to assign array keys for you automatically. But as it stands now, PHP will create an array for you, but since all your fields are using the SAME key names, you'll be continually overwriting values and end up with only the last field name/key-pair in the form.
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.