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.
Related
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
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'])
If I have two input forms such as...
<input type="text" class="foo2" name="bar1" id="someid1" value="" />
<input type="text" class="foo2" name="bar2" id="someid2" value="" />
<Some Javascript to get the variable from "bar1">
< Some PHP to set a php variable based on the previous javascript variable >
result --- $phpvariable = (the user defined variable in name="bar1")
And in the same file wanted to set the "value" from the first form to a php variable, what would be some ways I could accomplish that?
Edit.. Since the value is empty, the user inputs the value and a php variable is set before the form is submitted. I'm assuming javascript would need to be used along with json_encode, but I'm not familiar enough with Javascript to do accomplish this.
You need to ajax (see http://www.w3schools.com/ajax/) to your .php page.
Say you have a php file called ajaxMe.php with the contents:
<?php
echo $_REQUEST['passedVariable'] . "!";
?>
Now if you ajax to the url: ajaxMe.php?passedVariable=Hello, the response will be "Hello!"
I use one view for adding/editing DB data:
<input name="blah" id="blah" value="<? set_selected('blah')?> />
In my controller for edit I do this:
$_POST['blah'] = 'DB value';
$this->load->view('...');
But the input field is blank. I want the inputs to be prepopulated for my edit case.
CI Views can take a data array as the second parameter as others have mentioned.
http://codeigniter.com/user_guide/general/views.html
I don't like the idea of setting the $_POST array and then passing that as your data array. $_POST should just be used for values passed from the UI form. Since you would have to manually set your $_POST array anyways, you might as well use a separate array object. I would create an array with all your set values. i.e. array('blah' => $dbvalue); and pass that instead of a pre-populated $_POST array.
Secondly, your example code uses 'set_selected()'. The function is 'set_select()' and is meant for a option tag. So there are two issues with that line of code. It needs to either be
<input .... value="set_value('blah')" />
or
<option ....value="v1" "set_select('blah', 'v1')">
You need to pass $_POST to view, the posted data should pass from the controller to the view in the second parameter of the view loading function.
try this
$this->load->view('content', $_POST);
Whatever you pass to the view get turned into an actual variable. So your code would be.
<input name="blah" id="blah" value="<?php echo $blah; ?> />
$_POST['blah'] = 'DB value';
$this->load->view('...', $_POST);
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.