How can I display this value in the loop ?
echo '<input type="hidden" name="eg_payamt_'.$i.'" id="amount_post_'.$i.'" value="">';
this code is being passed into next code by POST
On the next page,
Could it be like this ?
foreach($_POST["eg_payamt_"] as $key => $payamt)
{
echo "eg_payamt_$key => $payamt\n <br>";
}
I don't see any results,
Do you guys have any ideas ??
You have to change the name of the input element -- all input elements like this will have the same name:
<input type="hidden" name="eg_payamt[]" value="whatever" />
And then when you access $_POST['eg_payamt'] it will already be an array, so your code will work almost as-is (you need to lose a couple of underscores).
Related
I'm looping through the $_POST array (generated form elements) and I need to get the keys and values of the next elements in my foreach loop.
Edit: The name of the elements for the answers (i.e canbeanynameABC and canbeanynameXYZ) are always unknown.
Here is an example array:
Array (
[inputid] => 87 [inputoutputtype] => radio [canbeanynameABC] => radio answer 2
[inputid] => 88 [inputoutputtype] => radio [canbeanynameXYZ] => radio answer 4 )
My code here
foreach ($_POST as $key => $value) {
switch ($key) {
case "inputid":
echo "<br/>Value of input_id : " . $value;
next($_POST);
echo "<br/>Value of inputoutputtype : " . $value;
next($_POST);
echo "<br/>Value of answer : " . $value;
break;
}
}
I thought that by doing next($_POST), the pointer would now be positioned on the next key/value.
Apparently it doesn't work, i'm getting the following displays:
Value of input_id : 87
Value of input_output_type : 87
Value of answer : 87
Any help would be greatly appreciated. Thanks
Edit: It was suggested that I use arrays to organize my form elements/values. I still can't figure out how to use arrays so that each answer (value) returned from the form, I actually get three values (answer, inputid, inputoutputtype)
Edit: #CBroe I spent the afternoon trying to figure out how to use arrays in $_POST. The form can contain any number of elements, and these elements can be text, radio, select/option, checkbox (can have more than one value returned). Each "value" I get from this form must be associated with an "inputid" and an "inputoutputtype". These form and elements are generated using php, so i'm trying to build a php form handler that will read any numbers of elements and types. The generator is creating unique names for each element so that values don't get overwritten. I'm trying to figure out how to integrate arrays into the generator, but not sure if I'll be able to assign them a row number (i.e [0], [1]..).. maybe I'm just not seeing how arrays would work for my situation.
Update: Ok, now i'm trying to modify my php form handler to deal with elements that could have any names. I need to be able to read id, type and value (3 different values) from element filled out in the form.
I'm playing around with the form (even though its entirely generated), but not sure what to name my elements
<input type='hidden' name='inputradio[inputid]' value='1'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 1'>
<input type='hidden' name='inputradio[inputid]' value='2'>
<input type='hidden' name='inputradio[inputoutputtype]' value='radio'>
<input type='radio' name='inputradio[output]' value='Radio answer 2'>
<input type='hidden' name='inputtext1[inputid]' value='3'>
<input type='hidden' name='inputtext1[inputoutputtype]' value='text'>
<input type='text' name='inputtext1[output]' value='Text answer 1'>
<input type='hidden' name='inputtext2[inputid]' value='4'>
<input type='hidden' name='inputtext2[inputoutputtype]' value='text'>
<input type='text' name='inputtext2[output]' value='Text answer 2'>
Should I change the title of this post if the direction changed a but?
Thanks
C
There are only two properties to an entry in $_POST, the key and the value. I don't know where you got your example array, but it doesn't work like that. So if I had a radio button group with all sharing the same name, I would get the name->value pair.
If I created the following form and I submitted it with value a selected in the radio button group:
<form method="POST" >
<input type="text" name="textbox" value="test"><br/>
a.<input type="radio" name="rd" value="a"><br/>
b.<input type="radio" name="rd" value="b"><br/>
c.<input type="radio" name="rd" value="c"><br/>
<input type="submit" value="submit">
</form>
my $_POST would contain the following values:
Array
(
[textbox] => test
[rd] => a
)
if I tried to step through my array, I would do so as follows:
for ($_POST as $key->$value) {
echo $key . " = " . $value . "\n";
}
Which would give me the following output:
textbox = test
rd = a
Can you tell me what problem you are trying to solve, since it seems you are approaching this from the point of view of some other language or framework, or possibly you are looking to work with some other array in PHP that is not $_POST?
I'm trying to get the value from two input fields and pass them as the name in a form.
In my code, I am hardcoding the value of the price range in for testing purposes.
echo 'PRICE RANGE:';
echo 'Low: <input type="text" name="t[pr_100000]" value="" maxlength="25" /> High: <input type="text" name="t[ph_10000000]" value="" maxlength="25" />';
echo 'STATUS:';
$termsStatus = get_terms( 'Status', array(
'hide_empty' => 0
) );
echo '<ul>';
foreach ($termsStatus as $term_st) {
$termsStatus = $term_st->name . 'PropertyFilter';
echo '<li><label><input type="checkbox" name="t[st_' . $term_st->name . ']" value="st_">' .$term_st->name. '</label></li>';
}
echo '</ul>';
Here is the code on another page that the search parameters are sent to:
// GETS THE VARIABLE FROM THE SEARCH WIDGET
$array_terms_test = array_keys( $_GET['t'] );
Any suggetions? Thanks in advance!
Judging by your comment, it seems like you are having trouble passing the input fields to the second snippit of code (via a submit button or javascript). You need to surround the input fields with a form tag and submit the form. If you want to use $_GET simply assign action='get' on the form. If for some reason this doesn't work for you, you can use javascript to pull the values from the fields and create a url var which you use to redirect (window.location = url).
Sorry if I didn't understand your question.
This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}
I am currently trying to design a for loop to iterate through any 'checked' check boxes, and from that, use the value within the 'value' slot to run some queries. I am unfortunately struggling with this as the list of checkboxes are not pre-defined, they are dynamic from the database pending the users previous selection.
The loop actually works to present the items to be checked:
?>
<input type="checkbox" name="option[]" value="$listing_id">
<font size="+1" color="green"><?php echo"$list_name"; ?>:</font><br />
<?php
The listing ID within the value is what I need to work with in a mysql query before I run an update query. The for loop that's meant to work is:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
...
}
The update query will work within this as its simply copied from somewhere else, I just need the 'Listing_ID' from the check boxes that are checked.
I ran this code to hopefully do some debugging:
if(empty($_POST['option'])){
echo "no checkboxes checked.";
} else {
if(!isset($_POST['option'])){
echo "no set.";
}
}
and it returns "no checkboxes checked."
I have now hit a grey area as to why this for loop isn't working (this was taken from another example on the internet).
empty($_POST['option']) will return true, if either $_POST['option'] is not set (same as !isset($_POST['option']) (!)) or an empty array.
If you need to debug what's going on, use var_dump($_POST['option']); to find out what has been submitted for the option checkboxes. I also suggest you do a var_dump($_POST); so you can see what has been submitted overall - e.g. in case the post action is not post you will immediatly notice). For HTML output:
echo '<pre>', htmlspecialchars(print_r($_POST, true)), '</pre>';
That should give you the information you're looking for. For each individual checkbox, you can do:
foreach($_POST['option'] as $option) //loop through the checkboxes
{
var_dump($option);
}
First of all your code seems to be bugged to me. Maybe is just a typo but
<input type="checkbox" name="option[]" value="$listing_id">
should be
<input type="checkbox" name="option[]" value="<?=$listing_id?>"/>
Moreover using empty over an array is not good at all.
Try echoing out the $option in the loop to see what the value is and there you can see if there is something there.
foreach($_POST['option'] as $option) //loop through the checkboxes
{
echo $option . "<br />";
}
Also make sure your form's method is set to POST or that it's action is pointed to the correct place. You also have an error in your input:
<input type="checkbox" name="option[]" value="$listing_id">
I assume you meant:
<input type="checkbox" name="option[]" value="<?php echo $listing_id;?>">
UPDATE:
The error ended up not being in the code posted. Error was discovered in an if statement that always returned false that in-cased the code posted above.
I am trying to allow my users to update quantities in the database, by providing them some input fields, I know that the sql update query is perfect, because if i DONT use the foreach loop, it will submit and update the last textfield only.
But I need the foreach loop so it will loop through all the textfields and update them all in the database. Can somebody please help me figure out why it's not updating with this foreach loop? Lots of thanks in advance :)
foreach($_POST['items'] as $p=>$q)
{
// working sql code is in here.
}
And the fields are dynamically generated like so:
$ct->data[$key][0]='<input type="text" value="'.$ct->data[$key][0].'" name="product" />';
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[' . $ct->data[$key][1] . ']" />';
$ct->data[$key][2]='<input type="submit" value="Update Item">';
$ct->data[$key][3]='<p name="price">'.$ct->data[$key][3].'</p>';
You should ALWAYS check if a variable exists. That's no more that normal and should have been lession 1.
You can't iterate over $_POST['items'] if $_POST['items'] doesn't exist (obviously). So you have to check if the form has been submitted.
One way is:
if ( isset($_POST['items']) ) {
// foreach ( $_POST['items'] ....
}
I always like to be on the safe side and check for type as well:
if ( isset($_POST['items']) ) {
// form submitted at least
if ( is_array($_POST['items']) ) {
// and it's an array as it should be
foreach ( ....
}
}
POST variables are never set in a GET request, so doing the foreach first time on a page is useless. That's what the check is for: 'is the form submitted and do the necessary post vars exist?'
What values are you seeing?
foreach($_POST['items'] as $p=>$q) {
print "$p: $q\n";
}
The name of the items input field should just be items[].
So:
# Good
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[]" />';
# Bad
$ct->data[$key][1]='<input type="text" value='.$ct->data[$key][1].' id="qty" name="items[' . $ct->data[$key][1] . ']" />';