I've recently used array notation when naming html input fields. e.g.
<input type="text" name="user[$userId][licenseStatus]">
I've never used this syntax before and although it is extremely handy I cannot see a good way to access the data held in session from the view when using Laravel.
For instance I might want to retrieve the old data back into the input like this when say a validation failure occurs:
<input type="text" name="user[$userId][licenseStatus]" value="{{session()->getOldInput(user[$userId][licenseStatus], '')}}">
But this obviously doesn't work because the array syntax on the name field means the data is held in an array in session like this:
[
_old_input => user[
32=>licenseStatus = 'xyx',
12=>licenseStatus = 'xyz'
]
]
So is there a smart way to retrieve old input values?
Thanks,
If validation error occurs, in the controller do a redirect with input values. E.g.:
return redirect('form')->withInput();
Then in the form itself, you can put the form value like this:
<input type="text" name="user[$userId][licenseStatus]" value="{{ old('user.$userId.licenseStatus') }}">
You can double check in the laravel documentation: https://laravel.com/docs/8.x/requests#flashing-input-then-redirecting
Related
I have added a <input name=whatever type=hidden> in authentication.tpl in register form so I have some stats(won't get in details)
I read that hidden POST in AuthController.php and then insert it in the database.
Problem is that the value always comes up encoded , something like this:
b40c094ed9549ae0bd311122b034a15c
I tried reading that POST the normal way + trim(Tools::getValue('whatever'));
but the result is the same.
Anyone could help me? Thank you!
You have to give a name to your input, then get by his name.
<input type="hidden" name="my_input" value="b40c094ed9549ae0bd311122b034a15c">
and in php side :
trim(Tools::getValue('my_input'));
if the value is not the good one, try simply with $_POST, try also urldecode, because some input are encoded( like the input named "back"):
urldecode(trim(Tools::getValue('my_input')));
I'm trying to post an array of variables taken using form data as in checkboxes like this:
<input type="checkbox name="parameter[]" value="XML"/>
<input type="checkbox name="parameter[]" value="test"/>
<input type="checkbox name="parameter[]" value="action"/>
When this data is submitted, I can see in the Google chrome console, the variables are each posted as parameter[]. I can enumerate over the posted values using a
foreach ($_REQUEST['parameter'] as $param) construct.
Due to the way my program is set up, how do I go about writing a query string in which I can send the data through GET request. For example writing:
taskpage.php?parameter[]=xml¶meter[]=test¶meter[]=action
doesn't seem to post the parameter data as an array.
Any advice on how to write this string?
Notice parameter[] != parameter .
It works if you remove the [].
taskpage.php?parameter=xml¶meter=test¶meter=action
Then take a look to $_GET['parameter']
var_dump($_GET['parameter'])
I have a have in PHP and I have common fields such as 'Name' and 'Surname'.
Now when the user visits the page e.g. http://www.example.com/form.php the form fields 'Name' and 'Surname' are empty.
I would like to now have a link similar to this http://www.example.com/form.php?name=John
so that when the client hits the link the PHP form will now have the name field already filled with 'John' in it.
I know this can be done in HTML but how can I do it in PHP?
Just to let to know I do not own the PHP form - I just want a link from my website to fill the PHP form (which I do not have control over).
Thanks in advance.
Can be done using $_GET
An associative array of variables passed to the current script via the URL parameters.
e.g.:
<? php
if(isset($_GET['name']))
{
$test = $_GET['name'];
}
?>
<html>
<body>
<form>
<input type="text" name="test" value="<?php if(isset($test)){echo "$test";}?>"/>
</form>
</body>
</html>
Note: code isnt tested or anything.. Also, there are possible security risks with getting values from your URL (can be considered user input), so make sure you are aware of that and how to prevent
You could store that value and then when you're about to output the input fields
you just pass along the stored value.
$name = $_GET['name'];
// ... later on
echo '<input type="text" value="'.$name.'"/>';
By using $_GET superglobal
<input name="name" value="<?php echo !empty($_GET['name']) ? $_GET['name'] : '';?>" />
<input name="surname" value="<?php echo !empty($_GET['surname']) ? $_GET['surname'] : '';?>" />
You can use the get method in php to get the name and make use of it
You can retrive this information by the $_GET["name"] function, or $_REQUEST["name"].
Reserver variables
Be carefull with those operations, you might have validation a/o security problem.
Note: if you are not sure that the "name" variable is set or not, you have to use also the
isset function to test it.
You can use the $_GET superglobal, so your input could look like this:
<input type="text" name="name" value="<?php if(isset($_GET['name'])) { echo $_GET['name']; } ?>" />
The $_REQUEST superglobal does a similar thing but I would just use $_GET.
It looks like everyone's answers here assume you are building the form yourself, which doesn't appear to be the case based on your question.
The thing that you want to do may or may not be possible. If the form accepts certain kinds of parameters in certain ways, you may be able to hook in to that functionality and set it up so that when someone clicks a link on your page, that information gets passed to the other page.
One way forms can accept this information is in the form of a "get" request. With this method, values are passed as part of the url, as in your example: http://www.example.com/form.php?name=John. Assuming your page has access to a php variable called $name, you can create a link from your code to build this kind of url like this:
Sign up!
If the page does not accept get parameters in this way (and I have a hard time imagining that they would), you may have to try other techniques to send along the information (assuming that they will even accept it!). The two other ways I imagine you could do this are by passing the value with "post" or creating a cookie for the page. If you tell us what page you are trying to set up this behavior on, we might be able to examine it and give you a better answer.
i am trying to post a form to php that contains multiple identical fields e.g. there can be multiple body_styles and multiple make and model
when i serialize the form i get the following output
SelectbsmContainer0=&body_style=hatchback&body_style=mpv&make=bmw&model=5+series+gran+turismo&valueA=200&valueB=800
how can i parse this at php end??
Change your html so that your fields are an HTML "array" like this:
<input name="body_style[]" value="" />
<input name="body_style[]" value="" />
Then you can access them via PHP's $_GET super global like so:
$first_body_style = $_GET['body_style'][0];
$second_body_style = $_GET['body_style'][1];
Or
foreach($_GET['body_styles'] as $value) {
var_dump($value);
}
Thanks to a certain PHP feature, you are going to have a lot of trouble unless you rename the fields so the names end with [], at which point they will appear in $_POST as arrays.
EDIT 2:
After writing up an incredibly long explanation in more detail I, of course, discovered my problem and it had nothing to do with the question I asked.
It was caused because I was creating a custom object of mine, assigning an uploaded images name to it's "LogoName" property...then creating a new version later in code, not assigning that property to the new object and then trying to save the new object (with no LogoName set) to the database.
Sorry to have wasted your time. Thank you for your answers. They have all been upvoted.
END EDIT 2
I have a form in a php site. The form has the usual City, State, Zip input options.
City looks like this:
<label for="city">City</label><input type="text" name="city" value="<?php echo $business->city; ?>" id="city">
Zip looks like this:
<label for="zip">Zip</label><input type="text" name="zip" value="<?php echo $business->zip; ?>" id="zip">
When I check my $_POST the values look like this: (using FirePHP)
['city'] => 'St. Louis'
['zip'] => 12345
So naturally when I put those values into my object, and try to save that object to the database (which has Zip as a varchar) I get errors because Zip is recognized as an integer.
How do I tell the form to force the Zip value in the $_POST as a string?
EDIT:
I didn't even think about this but maybe it's relevant. The form is set up to allow an image upload as well so the form has enctype="multipart/form-data" set.
Could that be causing this issue?
Other than that I don't know what to think as I am using FirePHP to log the $_POST information pretty much as soon as the form loads on Submit. I can't think of anything that would cause this issue.
You could cast it to a string like this
$zip = (string) $_POST['zip'];
I tried your example, but get
[zip] => 12345
[city] => Anywheresville
Normally, one doesn't have to worry about integers not looking like strings. I'm hard pressed to think of a case where a reasonable conversion wouldn't happen automatically. May be you could post the code which forms the structure and generates the query?
You can do strval($var);
or $val.""; :)