I am trying to populate an HTML form with values from a previous form being posted. The first form is created dynamically and is a list of t-shirt styles with 5 different sizes available.
I'm having trouble figuring out a way to populate the second form.
The HTML is this:
echo '<input name="'.$filename.'-s" type="text" size="3" value="'.$quantity.'"'>;
What I'd like to do for $quantity is something like this:
$quantity = $_POST['{$filename.$size}'];
Is it possible to use a variable with $_POST?
I think you're looking for this:
$quantity = htmlentities($_POST["$filename$size"]);
htmlentities() is necessary in case you have " or other junk in the input
$_POST[$var1.$var2] worked. Nothing came up in my initial search of previous answers, although I see several applicable questions in the related questions on this page.
Related
So I hava a problem. On client side users insert theris data in textbox, radio in textarea. All number of input is stored in hidden type, sove php script on my server side knows how many input does it has. Sometimes there is just 20 inputs, sometimes 25 or 30, so the holl stuf is daynamic.
I have two questions:
1. How on server side dynamic generate variables and use them as $input1, $input2 and os on.
2. Let's say that I have somehow managde first problem so my second question is how to make query which sometimes uses only 20 parameters, sometimes 25 and so on. I don't wanna use arrays and tables;
I stareted php code:
for($i=1;$i<=$num; $i++){ //I get num from a hidden type
${"question".$i}="j";
if(isset($_POST["${"question".$i}"])){
${"question".$i}=$_POST[${"question".$i}];
echo question1; //this doesn't work but I want make created variables
//to use like this
}
else
{
echo "You have error with reading ".$i." question";
}
}
Change echo question1; by echo $question1; (append $ symbol before your var name)
Or in dynamic way:
echo ${"question" . $i}
Why would you like to use the variables like this?
If the input is dynamic use it like an array! -> Easier and cleaner.
There is good example how to handle a dynamic array input: Post array from html to php
I have some custom meta boxes on WordPress, storing some information such as page titles and descriptions, but I am having a bit of a problem which I can't wrap my head around.
The meta input boxes need to be able to accept " and ' (Speech marks and apostrophes), and WordPress is storing the data fine, and I can display it fine on the front end, but when it is echo'd back into the <input> box, it messes up because its trying to print something like this:
E.g: <input value="Hello we're called "example" and we suck" />
So no matter how I go about it, it's being printed in the page edit screen (once I save) like this:
or something to a similar effect. Because I need the use of both characters, I can't use either of them to wrap the attributes in as an easy fix.
I'm just having a bit of a brainfart but really can't figure out the logic behind a solution to solve this, because if I escape the characters, they will just get shown to the end user as Hello we're called "example" and we suck which will confuse them even more.
Encode with esc_attr(), example from the Codex:
echo '<input type="text" name="fname" value="' . esc_attr( $_POST['fname'] ) . '">';
I'm trying to make a dynamic menu in my web, in which only some pages from each section will appear.
The code I wrote was:
$menulist=array();
$menulist[1]='file1%#16';
$menulist[2]='file2%#9';
$menulist[3]='file3%#19';
$menulist[4]='file4%#8';
$menulist[5]='file5%#13';
$menulist[6]='file6%#14';
$menulist[7]='file7%#10';
$menulist[8]='file8%#23';
$menulist[9]='file9%#19';
$menulist[10]='file10%#18';
$menulist[11]='file11%#12';
function actualizaciones($matriz)
{
$linea=explode("%#",$matriz);
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'</li>;
}
echo '<ul>';
array_walk($menulist,'actualizaciones');
echo '</ul>';
Every $linea[0] string is the name of another array (not shown in this code) which contains the text that should be in every possible link corresponding to every key passed by $linea[1].
I must have done something wrong, because the hyperlinks work fine but there's no text showing on them.
use the simple character like below
echo '<li><a href="first_chunk_of_URL'.$linea[0].'middle_chunk_of_url'.$linea[1].'last_chunk_of_URL">'.${$linea[0]}[$linea[1]].'<li>';
and the problem in your code is
.'</li>;
^^^^^
here is the problem it should be
.'</li>';
If I'm reading the question right, you're asking how to use variable variables in PHP.
This can be done using the double-dollar syntax - ie $$linea[0]. See the PHP manual for more info: http://uk.php.net/manual/en/language.variables.variable.php
But if that is what you're doing, I would say you're not writing good code: if variable variables are involved, there's almost always a better way of doing it.
Can't really offer much better assistance here without understanding more about what you're trying to do, but it sounds like you should be using subarrays rather than separate named variables for everything.
Hope that helps.
So, I have this HTML form:
<form id="search_form" class="form_wrapp"
accept-charset="utf-8" method="get" action="http://testing.com/results">
<input class="inputbox" type="text" name="search_query">
<input class="ic_search" type="submit" value="">
<input type="checkbox" value="checkbox1" name="search_filter[]">
<label for="Checkbox1">Checkbox1</label>
<input type="checkbox" value="checkbox2" name="search_filter[]">
<label for="Checkbox2">Checkbox2</label>
</form>
and it redirects to this URL upon submit with the 2 checkboxes checked
results?search_query=dreams&search_filter[]=checkbox1&search_filter[]=checkbox2
It works like this (inside codeigniter I get the data with $this->input->get('search_filter')), but my question is: I am doing something wrong inside the form, or this is how it's supposed to work?
And I mean about: &search_filter[]=checkbox1&search_filter[]=checkbox2. Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ? And if not, how can I make it work like that?
If you want it in the comma format you can do the following:
$filters = (array) $this->input->get('search_filter');
$filters = implode(',',$filters);
If you want to alter the format in which the form is submitted, assuming jquery for js:
$('#search_form').submit(function() {
var $hidden = $('<input type="hidden" name="search_filter" />').appendTo($(this)),
$filters = $('input[name^=search_filter]'),
value = '';
//loop through the filters check if there checked and add them to the value
$hidden.val(value);
$filters.remove();
});
Of course if the user doesn't have js enabled it will submit natively
Am I doing something wrong inside the form, or this is how it's supposed to work?
That's how it's supposed to work. At least if you need to read query string with PHP, those brackets need to be there to read the whole query string without each search_filter value being overwritten by the next one.
And if not, how can I make it work like that?
If you have to, you can use a POST request instead, process the submission, and redirect to the URL of your choice with whatever query string you want.
From your comment:
I wanted to make the url like this &search_filter[]=checkbox1,checkbox2 just to make it a bit more "beautiful"
Don't worry about that, seriously. The only time this matters is when you're doing extreme SEO and you don't want two URLs that point to the same place. It's common practice in those cases to remove all unused keys and alphabetize them so that all URLs with query strings are consistent, but mangling them into something custom still isn't a part of that.
Besides that, don't fight against the behavior - work with it - it's not "broken" and making it "beautiful" won't matter to anyone, plus you'll have to guess/remember which pages process query strings the correct way, and which ones use your "custom" method.
I am doing something wrong inside the form, or this is how it's supposed to work?
That is how it is supposed to work
Shouldn't it be something like: &search_filter[]=checkbox1,checkbox2 ?
Then you couldn't tell the difference between two items and one item that had a comma in it.
And if not, how can I make it work like that?
Obtrusive JavaScript. Don't do that. Forms work well the way they work.
That's perfectly normal. form data is always sent in key=value pairs, with one single value. Submitting key=value,value is not part of the HTTP spec, and would have the values treated as a single monolithic string, not two separate comma-separated values.
You can certainly use some JS to rebuild your form on the fly to use the value,value format, but then you'll have to mod your server-side scripts to accept that new format as well. PHP won't auto-split the values before you, because it's not a standard representation.
&search_filter[]=checkbox1,checkbox2
Why you need this?
Use this like:
<?php
$searchFilter = $this->input->get('search_filter');
foreach($searchFilter as $filter)
// some actions with filters.
You search_filter[] is simple array with values from checkbox inputs.
I'm creating an HTML form using jQuery that has certain text boxes grouped together under a single question (e.g., "List each URL that this request applies to.") For that question (and others), there are 3 text boxes below it along with a button to add additional text boxes if necessary. When I pass this form data to be processed by PHP, how can I have all these text box values be grouped together as a single array variable within the $_POST array variable? I tried giving all the text boxes the same name attribute followed by brackets, but that didn't seem to work (e.g., <input name='myarray[]' type='text' />). Any suggestions?
EDIT: Here are the specifics on the error I'm getting:
I'm using this in PHP
$myarray ='';
foreach ($_POST['myarray'] as $value) {
$myarray .= $value . '\n';
}
The error I get is:
"Warning: Invalid argument supplied for foreach()"
foreach($_POST["myarray"] as $myarray)
{
echo $myarray . "";
}
This is quite simple. I suppose you are using $_POST['myarray[]'] (notice brackets[]) which is incorrect. This example should work perfectly fine.