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.
Related
I have two different textarea and I want to store that both in one field of database with line breaks. Its working good if I store one textarea value in table field, But I need to store both textarea value in one field.
Example :-
textaera 1)
Hello
text from textarea1
textarea 2)
How are you?
its second textarea text
Output that I need with all line breaks and also between two textarea value :-
Hello
text from textarea1
How are you?
its second textarea text
Suppose you both values are coming into variables $textarea1 and $textarea2 correspondingly. You can do it like below:-
$combine_data = $textarea1."\n".$textarea2;
OR
$combine_data = $textarea1 . PHP_EOL . $textarea2; and use nl2br($combined) to show it again in text-area //#iainn suggestion
OR
$combine_data = $textarea1 ."<br/>". $textarea2;
Reference Taken :-
add line break between 2 variables
As mentioned, if you want to be able to retrieve the blocks separately (for greater flexibility) but store both in one column, you can use serialize():
To store the data:
$data = array('p1'=>$_POST['textarea1'], 'p2'=>$_POST['textarea2']);
$compress = serialize($data);
//insert value $compress into one column in your database
To recall the data:
// Get column from database
$data = $row['db_column'];
$decompress = unserialize($data);
// Echo to browser using html break and End Of Line constant (for compatibility)
echo implode('<br />'.PHP_EOL,$decompress);
You can also echo each value separately after unserializing:
echo $decompress['p1'].'<br />'.PHP_EOL.
$decompress['p2'].'<br />'.PHP_EOL;
By doing it this way, you can recall the data in their respective blocks, that way if you decide to change something later, you still have all the raw data in blocks.
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.
I have a html form in php file in which there is a textbox in which a user can post multiple links . suppose
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
www.4shared.com/video/UryixZ7l/Puppy_loves_ringtones.htm
There are four links here . If i submit the form and the data is send to target page then it counts the whole textbox suppose named "links" as a string and send all of its data into database which it should . But i wanted to send these links and the data along with them such as uid , name to be submitted 4 times into database ie as many times as many links are there instead on 1 entry with all four links and the name and id in one row . How can i make this possible ?
I edited this entire post after I got more information by the author
Assuming you are using a textarea where the user can enter his information you could process that in PHP:
<form action="mytarget.php" method="post">
<textarea name="urls"></textarea>
</form>
Assuming the user should enter one URL per line, on PHP:
$data = explode("\n", $_POST['urls']);
if(count($data) > 0) {
foreach($data as $url) {
echo $url . "<br />";
}
}
Maybe this are the droids you are looking for. :P
Sorry I've been trying to figure how a technique with just the textarea:
This does use jQuery, I'm not sure if that is something you want to do or not. You can easily convert it to javascript if you like.
<script>
$(document).ready(function() {
$('textarea').on('focusout', function() {
var text = $('textarea').val();
var delimiter = ",";
$('textarea').val(text + delimiter);
});
});
</script>
What that does is appends a comma to the end of each url when the user focuses out of the textarea (pick whichever event you want, i don't expect you to use focusout), so that when your form is submitted, you can then explode the textareas text and get the urls and store them in separate variables.
The regular expression will expect the url to always end in .htm, so if you are wanting other url endings then add to the regex.
This will add a delimiter after every focusout event, even if its just plain text (i.e. not a valid url). But you can check against this once you've exploded (the text I mean :D), and it can be a good way to sanitize your data to make sure you're getting valid urls.
Hope this helps in some way
PS, the delimiter can be anything, i.e. a new line would be best \n, just remember to use the same delimiter in explode() i.e. explode("\n", $_POST[urls]);
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 a dynamic form in which i can add and remove textarea.
The name of textareas is MyTextarea[]
<textarea style="display:inline;" name="MyTextarea[]"></textarea>
<textarea style="display:inline;" name="MyTextarea[]"></textarea>
So when I want to treat this textarea with PHP i'm doing a :
echo $_POST['MyTextarea'];
So a Array is display on the screen, up to now it's ok
So I do a print_r($_POST['MyTextarea']); and I have again the same result : Array
I want to know if it's possible to have many textarea with same name with [] to generate an array.
If it's possible how can I do, or what's wrong with my code.
Thanks
Yes, in php if you have an input field with a name like this "MyTextarea[]" is posted as an array.
So if you want to access your data, you have to do:
echo $_POST['MyTextarea'][0];
If you have multiple textareas with the same name, you'll get an array where each index has one textarea. The first textarea in the form is the first textarea in the array
you could do
foreach ($_POST['MyTextarea'] as $textarea){
//do wat you need
}
This is obviously a killer feature to use if you need to add multiple textareas dinamically.
Which kind of framework are you using, I'm quite sure there is something at one point that is casting you're array into a string, maybe something that apply a treatment on POST variable like this:
foreach ($_POST as $key => $value) {
if ($value && !$is_magic_quotes_gpc) {
$_POST["$key"] = addslashes($value);
}
In this case you've to remove this function...
To be sure of what I'm talking about, you can try a var_dump($POST[MyTextarea]) =>string 'Array' (length=5) (should be an array)