I have a multi field search form I'm creating. There are 9 different search fields. None of them are mandatory, though. The only requirement is that you have to fill in at least one field.
They all show a default value (i.e. 'State') upon load instead of using labels to indicate what the purpose of the search field is. So, if you edit one field but leave the other eight alone then all nine will still have a value posted.
Is there any good, efficient way to handle this? I'd prefer to not have to manually do the logic in either jQuery before posting (e.g.if($(inputid).val() == 'Default Value') { ...) or the controller (e.g. if($this->input->post('name') == 'default value') { $data['name'] = '') because there will be a few iterations of this search throughout the site, so a dynamic solution would be incredibly helpful and save a bunch of time.
Thanks!
EDIT
here's the gist of what I ended up with after #veddermatic's post below:
rendered form:
<form id="search-form" action="spend/search">
<input id='fname' value='First Name' data-original_value='First Name' name='fname'/>
<input id='lname' value='Last Name' data-original_value='Last Name' name='lname'/>
...
</form>
ghost form:
<form id="ghost-form" action="spend/search">
<!--nothing here yet-->
</form>
jquery to handle it all:
$('.do-the-search').click(function(e) {
e.preventDefault();
$('#search-form input').each(function() {
var $this = $(this);
if($this.attr('data-original_value') != $this.val())
{
var clone = $this.clone();
$("#ghost-form").append(clone);
}
});
$("#ghost-form").submit();
});
One option would to have two forms, the "normal" form you have currently, and a "ghost" form (it has no submit button or visible fields) with the same target and action as the one presented to the user.
When you build / render the form to the user, give each input a class you can use to iterate over them with, and put the pre-filled value into a data attribute:
<input type="text" class="myFormInput" name="in1" data-original_value="blah" value="blah" />
<input type="text" class="myFormInput" name="in2" data-original_value="something" value="something" />
....
Then handle the user-facing form submit with javascript, iterate over the .myFormInput elements, and if they have a value different from the original value, create an input element with the same name in your "ghost" form, then submit that form. This will only send changed elements, and if for some reason they have javascript off / disabled, your gracefully degrade because your original form will still submit.
EDIT: you could very easily turn this into a plugin and use it on all your forms if you do have multiple instances so you'd just have to do something like: jQuery('#userFacingForm').ghostForm();
Related
So, i have 2 forms to filter products, one with checkboxes and the other with select options.
When i check one checkbox the form is submited and the query string is updated with it's value, when i check another checkbox the same happens, but when i select a option, the current query string resets and add only that option value instead of adding it after or before the current query.
What i want is to unite both $_GET indexes in the query string, independent of the order of submit. How can i do this? I'll be grateful if someone knows.
$marca_get = isset($_GET['marca']) && is_array($_GET['marca'])
? $_GET['marca'] : [];
if ( isset($_GET['ordem']) ) {
if ( $_GET['ordem'] == 'vendas' ) {
$orderby = 'vendas';
}
if ( $_GET['ordem'] == 'avaliacoes' ) {
$orderby = 'avaliacoes';
}
}
<form method="get">
<input type="checkbox" id="amd" class="checkbox-filtro" name="marca[]"
value="amd">
<label for="amd">AMD</label>
<input type="checkbox" id="intel" class="checkbox-filtro" name="marca[]"
value="intel">
<label for="intel">Intel</label>
</form>
<form method="get">
<select id="ordem" name="ordem">
<option value="vendas">Mais vendidos</option>
<option value="avaliacoes">Melhor avaliados</option>
</select>
</form>
$('#ordem, .checkbox-filtro').on('change', function() {
this.form.submit();
});
Quotes from Mozilla Developer Network:
The HTML <form> element represents a document section that
contains interactive controls for submitting information to a web
server.
When a form uses the get method, the fields are put into the URL Query String:
get: Corresponds to the HTTP GET method; form data are appended to the action attribute URI with a '?' as separator, and the resulting URI is sent to the server.
Your page has two forms, which is OK if they are unrelated. For example a page with a search form in the header, and a comment form at the bottom, would have separate forms because they do different things. The search form is also likely to have a different action URL than the comment form.
Option 1
The easiest way to get two forms to share state, is to just use one form instead of two.
If you want two forms to be united, and they also share the same action URL, this indicates that they are basically the same form.
The form element groups the fields that belong to a particular action.
The default style of a form element means users cannot see the form's boundary, and a form element can contain other 'non-form' content, just like a form on paper would.
Option 2
Copy the other form's fields, but make them hidden.
<input type="hidden" name="ordem" value="<?php echo $_GET['ordem']; ?>">
Suppose your visual design has exposed a form's boundary (CSS border or background). It may not be visually desirable to contain a significant amount of content.
This is slightly more maintenance. It may be worth redesigning the HTML/CSS so the form element is not styled directly.
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.
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.
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
I have a form with two submit buttons.
The user fills field-A and hits submit.
Done that, some input fields will be filled with data.
After that first submission, the value on the field-A should not disappear.
How can we preserve this value after the first submission?
Should we, on the field-A value attribute, place:
value="<?php echo isset($_POST['fieldA'])) ? $_POST['fieldA'] : ''; ?>" ?
The form submits to self.
Update - Additional details:
This is a form that will have two submit buttons on the same page (sort of speak).
Submit Button A - Will grab some data based on a input field, and fill the other input fields on that form.
Submit Button B - Once the form is filled, it will use all that data to do another submission.
This is a very simple case, no frameworks are in place here. I do have, however, some sort of MVP structure here.
Thanks in advance,
MEM
In general, such things being done using 2 forms, no one.
And GET method, not POST. At least for the first form.
But as you cannot ask a question, it's impossible to give you an answer.
Here you go:
index.php
<form action=edit.php>Enter name: <input name="name"><input type=submit></form>
edit.php
<? $row = dbget("row","SELECT * FROM domains WHERE name = %s",$_GET['name']); ?>
<form method="POST" action="save.php">
Edit data for <?=htmlspecialchars($row['name'])?>:</br>
NS: <input name="ns" value="<?=htmlspecialchars($row['ns'])?>"><br>
Another: <input name="another" value="<?=htmlspecialchars($row['another'])?>"><br>
<input type="hidden" name="name" value="<?=htmlspecialchars($row['name'])?>"><br>
<input type=submit>
</form>
save.php
do whatever you do usually to save info
I would store these values into $_SESSION, as user fabrik said. This way they can be stored across the entire form submission process(assuming it is multiple pages) and posted all at once at the end.
Assuming you're having some kind of submission system with a "next" button to go to the next set of forms, using session_start() and $_SESSION is certainly the best method. More information could be found here, or various tutorial sites--
http://php.net/manual/en/reserved.variables.session.php
It's ok to do that with $_POST, some people dont like the ternary operator but for me it works just fine. Although, there are better ways to deal with forms using O.O.P. You could create a class that manages your form, and pass an array to the constructor of that class (eventually you could pass the $_POST) and the class will create your form according to the info submited. You could even use the same class to valdidate your form
I don't see the need of using $_SESSIONS, cause this is not information that you need to preserve during the whole session.. or not?
Try this:
<?php
$fieldA = (isset($_POST['fieldA']) ? $_POST['fieldA'] : '')
?>
// and in your form
<INPUT type="text" name="fieldA" id="fieldA" value="<?=fieldA?>" />
as you mentioned, this should work.