convert string with array structure back to array in php - php

I have <input type="text" name="info"> with the value like this:
array() {
[name]=> 'Tien'
[sex]=> 'male'
[address]=> 'ABC'
[code]=> '888'
}
I submit this input to another site (Note that the input value is a string), all I want is convert that value from string back to array array("name"=>"Tien", "sex"=>"male", "address"=>"ABC", "code"=>888). Is possible to convert the string back to array. If yes please help me solve this. Thanks and sorry because my bad English

What you're asking for is serialisation, i.e. expressing an arbitrarily complex data structure in the lowest common denominator as text. If you simply choose a serialisation format which can easily be serialised and unserialised, this is trivial. I'd suggest to use either serialize and unserialise or json_encode and json_decode. Whatever format you came up with there is simply not easily unserialisable.

I can't completely understand your question, but from my best guess, use this form data:
<input type="text" name="info[name]" value="Tien" />
<input type="text" name="info[sex]" value="male" />
<input type="text" name="info[address]" value="ABC" />
<input type="text" name="info[code]" value="888" />
The other site can then handle it as an array stored within $_POST['info']

Related

HTTP ordering variables in form

given a form like the following
<form action="/page" method="POST>
<input type="hidden" name="input" value="12" />
<input type="hidden" name="input" value="24" />
</form>
Using Google Chrome 31.x and PHP 5.5, /page now has a $_POST variable for input of 24
This happens because when the $_POST array is created, The value is over written in the array. And the latter value is the value which is preserved.
Most browsers Ive tested this is the case, But Is there any HTTP spec / browser spec which says that form inputs should be sent in the order they are defined ? Or could an update in the future (or an old browser) send these updates in the reverse order for example ? or a random order ?
Edit:
to give more context, It will not be used like the above in all cases. only in a certain case.
The first form element is a SELECT box, But depending on the options chosen, Javascript will be able to change the value, Without changing the Select box value
Regardless of the order in which HTTP sends your two values, PHP can only have one value for $_POST['input'].
To solve this, use array notation:
<form action="/page" method="POST>
<input type="hidden" name="input[]" value="12" />
<input type="hidden" name="input[]" value="24" />
</form>
Now you'll have an array $_POST['input'] with both values.
To answer your question about the spec, see this page:
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4
See the bullets for the application/x-www-form-urlencoded default content type.
The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.
To my knoweledge there is no specification as to the order in which a browser should parse the from before submitting.
But I would say that you can pretty much assume that the form fields will be parsed from top to bottom, because the whole dom is parsed like this.
Here is a little bit additional information as to how a form submit is processed/handled.
http://www.w3.org/TR/html401/interact/forms.html#successful-controls
Steve

PHP - HTML - Get multiple checkbox value without making it become array

I have read through many questions and do my searches for hours, but i still cannot find the solution to what i exactly want.
<form method="POST">
<input type="checkbox" name="fruit" value="0" />No Preference
<input type="checkbox" name="fruit" value="1" />Apple
<input type="checkbox" name="fruit" value="2" />Orange
<input type="checkbox" name="fruit" value="3" />Banana
</form>
print_r($_POST);
I already did validation to checkbox value 0 to 1,2,3 so they will inverse and i did it using looping javascript search for the element name. The problem is, i have to use the element name without changing it become array name. (e.g Fruit => Fruit[] ). So i need to use this element name to retrieve all checked information inputted by the customer. I've seen this can be done in ASP, but i could not figure how they do as it's long time ago already.
My question is, could any one figure how to do this without changing the element name into array format (e.g Fruit => Fruit[] ) ? T.T
Any help will be appreciated. Thank you..
As you already figured, when you submit the same name multiple times, the latest one will overwrite former ones. A solution is to set up a hidden field
<input type="hidden" value="" name="foobar_as_array">
and use jQuery or plain JS to concat your values into that field on submit
<form ... onSubmit=$('#foobar_as_array').value(...concatenate them...);">
I changed the validation using this line :
var GenBox = eval("document.forms['SubmitSearchAll']['" + oCheckBox+"[]']");
and it helps me alot. Now it is solved.
Thanks everyone for contributing.

how to turn html inputs into php array

I am working on an application which, in the end, will produce a .CSV file. I have already found how to turn an array or arrays into a .CSV file, but I am having trouble figuring out the preceding step.
I have a bunch of html inputs in a form that will be sent to PHP, I am hoping to have one array consisting of all the input names (input name="example") and another array that consists of the values put into those fields by the end user.
In the end I was the CSV to be a product of both arrays. The first row being things like Height, Width, Depth, and the second row being 10 mm,20 mm,9 mm, etc etc.
I am wondering if a mixture of html classes and a foreach could be used, but I'm really not sure. Any help would be great! Thanks!!
using the square bracket notation will convert these named fields into arrays, here are a few examples
<input name="array[]">
<input name="array[]">
<input name="array[]">
<input name="array[foo]">
<input name="array[bar]">
<input name="array[0][foo]">
<input name="array[0][bar]">
<input name="array[1][foo]">
<input name="array[1][bar]">
Then to get a quick preview of your data use var_dump() or print_r()
var_dump($_REQUEST['array']);

HTML & PHP input[] <- array ? Limitations

It's more of a wether can someone confirm my theory, as for going short ways, when you add another input which shall have similar name ex. myvar_0, myvar_1 you are supposed to use javascript to generate those inputs, but there is input "array" type, where you crate an input with name myvar[], myvar[], myvar[] and this acts as an array and passes values via post to PHP as an array, but recently i've discovered that for some weird reason this array has limitation of 197 values ( or 196 is the maximum capable index value ) as on chrome for now ( didn't text it on other browser ).
So does anyone else encoutered a similar problem ?
If you're using suhosin, that brings a limit to the max_vars sent via POST.
Default is set to 200, so could be your problem.
See: suhosin.post.max_vars
array format got nothing to do with html or browser , for browser square brackets doesn't mean any special thing it will send all the key , value pairs with same key as opt[] e.x
for
<input type="hidden name="opt[]" value="1"/>
<input type="hidden name="opt[]" value="2"/>
<input type="hidden name="opt[]" value="3"/>
browser will send
opt[]=1
opt[] =2
opt[]=3
as a request to the server;
Its the PHP which is smart enough to interpret this as index array with name of "opt" .
I've just done a simple test (See below) which returns (for me) 200 items in a post array.
<form method="post">
<?php
print count($_POST['opt']);
for($i = 0; $i < 200; $i++){
?><input type="hidden" name="opt[]" value="1" /><?php
}
?>
<input type="submit" />
</form>
I get the impression this is more about the data being sent, or the server itself, than a limitation of PHP.
Well the answer was the Suhoshin security module, after some research i found that acually max_post_vars was set to 200, that was kinda blocking the rest of data from being processed, thanks for all your answer :-)

two values for one name in input

I have one input (type radio) that I want to insert it 2 values, something like that:
<input type="radio" name="name" value1="value1" value2="value2" />
And after draw each value seperated with PHP.
There is a way to do it? (And no.. I dont want to insert input with type="hidden")
Thank you.
Well, not the way I would do it, but you could use a delimiter for your value(s)
<input type="radio" value="Value1|Value2" name="two_values" />
Then, in PHP, just list($value1,$value2) = explode('|', $_POST['two_values']);
EDIT
As #user387302 said, you would obviously be limited to not having any values containing your delimiter, for example value="One|PipedVariable|andAnother" would not work to extract two values of "One|PipedValue" and "andAnother"
Why not do:
<input type="radio" name="name" value="value1#value2" />
and then split on "#" (or any other symbol) server-side?
If I understand your question correctly, can you set the value of your radio to something like "value1-value2" and then in your php just seperate value1 from value2 with explode(). You could use any other seperator other than '-' too.
Edit
based on your exmaple:
<input type="radio" name="name" value="value1-value2"/>
Can't do it.
You need either a hidden input (which you say you don't want) or use value="value1,value2" and then explode in the PHP script.
Use this :
<input type="radio" name="name[]" value1="value1" />
<input type="radio" name="name[]" value1="value2" />
returns array

Categories