Laravel Request show what form the input was captured in - php

I am currently using the Laravel 5.4 Framework and during form submission
I am collecting the inputs of a Request and would like to see the form name that the inputs were captured in.
<form name="my_form">
<input type="checkbox" name="my_input" value="my_value">
</form>
So in the example above I would like to see the checkbox named "my_input" to be contained in the form "my_form". Currently with the way Request works it will just take "my_input" and show that.
The reason for my need of seeing the form name because my inputs are dynamic and am creating objects based off of the form name.
Thank You

When you create a <form> with some name, also create a field in the form with that name as its value.
For example, using the form in your question:
<form name="my_form">
<input type="hidden" name="form_name" value="my_form">
<input type="checkbox" name="my_input" value="my_value">
</form>
Then you can use $request->input('form_name') in Laravel (or any of the other ways to get input data).
If you dynamically change the name of the form, you can use the same code to change the value of the field. For example (with jQuery):
function setFormName(oldName, newName) {
var form = $('form[name="' + oldName + '"]');
form.attr('name', newName);
$('input[name="form_name"]', form).val(newName);
}
This can also be done without jQuery, but it's a bit more code, so it is left as an exercise to the reader.

Related

php get data from another form using a different form

Is it possible to get data from another using a different form?
I don't want to use one form
<?php
echo $_POST['2'];
?>
<html>
<head>
</head>
<body>
<form method="post">
<input type="submit" name="submit" />
</form>
<form method="post">
<input type="text" name="2" />
</form>
</body>
</html>
No, that's not possible because browsers will only ever submit one form at a time (the one containing the clicked submit button, typically).
They can't possibly submit multiple forms at once because each form has its own action and method attribute which determines the request to send.
As #peter said, you can submit only one form at a time. But there are some workarounds for your needs.
Method 1
Post your form to a php script(say form_1_action.php) and then store the form input in a Session variable.
$_SESSION['form_data_1'] = $_POST;
Then you will be able to access it in different pages. Like,
$_SESSION['form_data_1']['field_name']
Method 2
Post your form to a php script(say form_1_action.php) and then store the form input in a PHP variable.
$formData1 = $_POST;
Then you can use the data from the first form in the second form (the second form should be on the same file form_1_action.php) like
<input name="name" value="{$formData1['field_name']"}>
You should pass the data from the first form in a hidden field on the second form if you need it on the form_2_action.php.
Method 3
Use Javascript to accomplish your requirements in a more userfriendly way.
try using jquery to Prevent the other form from submiting and try updating the value using event listening of the first form and update that input.
$( '#Submit' ).click( function ( event ) {
event.preventDefault();
var value = <?= $postedValue ?>;
$('input[name="input_name/2"]').val(value);
}

form type="url" POST to PHP

So I have a form that requires a user to submit their website to a form. Here is the html line:
<input type='url' name='link'>
And I'm using <input type="submit" value="submit" formmethod="post"> to submit the form to a php
And I'm trying to retrieve the values in my php file with:
$link = $_POST['link'];
Why isn't this working? At first I thought it was because I had htmlspecialchars() but it's not coming through without it either. I can't find anything in any google search that even mentions anything related to this kind of problem (with a type="url" form)
What do I need to do to process form data with type of "url" in PHP with a $_POST?
Get your form method to be set to post e.g
<form method=post>,
if you submit the form and in the url in your browser u can see some more inf then be sure 2 check your form method
I think this is wrong,
method="post"
Its only method, not formmethod
Also make sure, you dont have one more for element name with link.

cakephp: get textbox value in controller through postlink

i have a textbox and i wish to use the value entered in the textbox in controller- onclick of a link(not form submit). So i assume i have to use postlink to submit. But how do i get the value of that textbox in postlink?
following is my code:
<?php echo $this->Form->postLink(
'Get Coords',
array('action' => 'test', $this->request->data['Rideoffer']['PickFrom'])
);
?>
i get an error on $this->request->data['Rideoffer']['PickFrom']. data['Rideoffer']['PickFrom'] is name of my cakephp textbox(i saw it in firfox inspect element).
How do i get the textbox value?
The FormHelper::postLink method has no way of getting a textbox value. The postLink method pretty much just creates an <a> link element that submits a hidden form using Javascript. Here is an example of what postLink spits out:
<form action="/posts/delete/16" name="post_511c870e05d25" id="post_511c870e05d25" style="display:none;" method="post">
<input type="hidden" name="_method" value="POST"/>
</form>
Delete
As you can see, when you click the <a> element, it submits the form with the hidden input. You can change the value of what this input submits by passing in parameters to postLink, but you cannot dynamically get a value of a textbox that a user can modify and submit it with the form without doing something extra.
There are two similar options (one being slightly more Javascript heavy):
1) Since you are using Javascript, you can use Javascript (or jQuery) to dynamically change the value of the hidden input to whatever the user types. Even better, you can do it so the Javascript/jQuery only updates the hidden form input when the user clicks the link. Note it may be easier to not even use the postLink function and do all the form stuff yourself (or with Cake's FormHelper).
2) Don't use the postLink method at all. Create a normal form with the textbox input and mimic what postLink does. Specifically, you wouldn't have a submit button for your form. You would just basically just copy what it spits out.
<form action="test" name="UNIQUE_ID" id="UNIQUE_ID" method="post">
<input type="text" name="data[RideOffer][PickFrom]" value="POST"/>
</form>
Click
Note in the above example you should match UNIQUE_ID as the same value and you must also remove style="display:none;" from the <form> tag.

When submitting a web form, don't reset data fields

How can I submit a form to itself without clearing the data in the fields using HTML, javascript and PHP?
You could take different approaches (e.g. cookies, jquery, etc...), however HTML + a line in PHP are more than enough in this case. Try this example code:
<form name="test" method="post">
Your Name: <input type="text" name="YourName" <?php if (isset($_POST['YourName'])) echo 'value="'.$_POST['YourName'].'"';?> >
<input type="submit" value="Submit">
</form>
In the code above if something has been posted to the receiving page (that can be the same page, such as in your case), then the posted value is printed out in the corresponding field. You can use this approach for all the fields composing your form.
If you want, you can also use similarly the $_GET method in the form.
If you use the traditional form submit, you need to save the parameters and rewrite the form input elements when you write the form the next time. But a better way is to use AJAX -- then the field data is sent without a form submission, and the input elements retain their data. See this link: http://www.w3schools.com/ajax/default.asp

How can I dynamically change text based on input field?

How can I dynamically change a link based upon an input field in a form. For example, if I input 1.00 into the input field, I want to change the link to this:
donate.php?amount=1.00
Where the amount changes to the amount specified in the input field.
I'm guessing its JavaScript which isn't my strongest point but any help would be awesome. :)
Thanks
markup:
<input type="text" id="amount" onkeyup="changeLink(this);" />
donate now!
Javascript:
function changeLink(inputElement)
{
$('#donateLink').attr("href","donate.php?amount="+inputElement.value);
//console.log($('#donateLink').attr("href"));
}
jsfiddle working example here.
This can be done with html forms:
<form action="donate.php" method="GET" id="donateform">
<input type="text" name="amount" />
<input type="submit" value="Donate" />
</form>
You could also have input entered via a drop down list so they don't enter invalid values. Or you could validate the input with javascript. To use a link to submit the form, you can use javascript:
Donate
You don't need to do anything, just use a form. By using the GET method with a form and naming your input field 'amount', that will already be added to the URL at the time of form submission. Go ahead and try submitting a form when you enter 1.00 into the box. When the page loads, your URL will be donate.php?amount=1.00 like expected. The URL does not need to be changed whatsoever.
If you're using POST, I would merely suggest not doing this. It serves no purpose in that case.

Categories