cakephp: get textbox value in controller through postlink - php

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.

Related

Laravel Request show what form the input was captured in

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.

$_POST is empty when submitting a value that was posted to the form

I am pretty new to web programming and I cannot figure this problem out. To keep things simple, say I have 2 pages. The first page has a form with two selection boxes and a submit button. The second page has a form with two text input boxes and a submit button. After the form on the first page is submitted it goes to the second page and the two text input boxes are filled with the values from the first form with a $_POST.
My problem is, when I submit the second form (which goes to the same second page on submit) the $_POST variables of the form are empty.
I thought the if-else in the value would fix it. The purpose of that was because after submitting the $_POST from the previous page no longer has a value and I want the value in this forms field to be displayed after submitting (which is still the same value from the first form). Not only do I want it to display the value in these fields, I want to use them for a database query (which is why them being blank is a problem).
The values in the form when the second page is reached are correct. Also in the else case if I echo "test" instead of the $_POST it is displayed in the box so I believe I have it narrowed down to the $_POST being blank after submitting but I have no idea why.
<form method="post" action="newSerialNumber.php">
JON: <input type="text" name="newSNJON" value="<?php if ($_POST['JON'] != ""){ echo $_POST['JON'];}else{ echo $_POST['newSNJON'];} ?>" disabled>
Part Number: <input type="text" name="newSNPN" value="<?php if ($_POST['PN'] != ""){ echo $_POST['PN'];}else{ echo $_POST['newSNPN'];} ?>" disabled>
<input type="submit" name="Button" value="Add">
</form>
You have the disabled attribute set in those input fields.
Disabled form elements are not send when the form is submitted.
http://www.w3.org/TR/html5/forms.html#attr-fe-disabled:
“The disabled attribute is used to make the control non-interactive and to prevent its value from being submitted.”
If you don’t want the user to be able to change the values, but still send them with the form when it is submitted – use the readonly attribute instead.
Yes, it's right.
Disabled fields are not included in the submit. Remove the disabled attributes and you can see it works.

Submit button showing in GET URL

I'm having a problem with my HTML GET form that's connected to a PHP script, so, basically, when the action is done I see the SUBMIT button value in the URL, so it's like this http://url.com/?valueI=Want&submit=Submit+Value.
How do I stop that from happening?
Remove the name attribute from the submit element to prevent it from being passed in the query parameters.
See: Stop the 'submit' button value from being passed via GET?
This is the nature of GET requests. The submitted values, aka Query String, are shown as part of the URL after a ? suffixing the page URL.
If you don't want it to show up, use POST method, or make a script that submits using Ajax.
Now if the question is only about the text in the submit button being shown, if you don't want it to get submitted along with the rest of the form all you have to do is not give it a name.
<input type="submit" value="Send Form">
No name="..." in the tag.
you need to set the form method
<form action"/your/path" method="post">
...
</form>
You can use button tag to submit the value using GET method.
<button type="submit">Submit</button>
do something like:
<form action="myfile.php" method="get">
(your form elements here)
<input type="submit" value="Submit" />
</form>

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

Remember form value when return back to submit due to some error

After filling the form when submit, accidentally due to some filling error ,the form is not submit and return to back,in this condition the value of all text box is blank. i want to stable value of all fields in this condition . I'm using php with smarty framework. Please reply with solution as soon as possible.
Thanks.
If the form is submitted to the page that contains it then you will have access to the submitted values, and can use them to populate your form. For example, if you are submitting the form via POST:
<input name="something" value="<?=$_POST['something']?>" />
If you are submitting the form to a different script, you could send the values back to the page with the form as URL parameters, or you could use temporary session variables, and unset them when the input passes whatever validation you are using:
$_SESSION["temp_something"] = $_POST["something"]; //In form processing script
Then in your form:
<input name="something" value="<?=$_SESSION['temp_something']?>" /> <!--In form-->
You can fill the form fields, on the second round, by filling the content inside the value attributes of html tags, like so:
<input type="text" value="<?php echo $_REQUEST['test']; ?>" name="test">
Pay attention: this is a fast and simple solution. It gives you an idea. In good web programming practice you should sanitize the form data received by client in order to avoid security issues.

Categories