I am looking for a neat solutions for the multiple Forms.
Have a look at the example screenshot:
There are three networks in the dropdown, AT&T, Verizon Wireless and T-Mobile.
I've selected 'AT&T' from the dropdown list and then 'Sale Type' radios appear.
T-Mobile network may have same Sale Type as 'AT&T' network but two extras Sale Type.
Majority of the text boxes from all the Sale Type are the same. For example:
Consumers will have 20 Fields and Business have 27 Fields (Extras Fields). Sim-Only will have less fields - 15 Fields (A few removed and new fields).
What is a good solution implement like this and DRY principle?
I have used jQuery with a lot of $('.name').hide(); $('.name').hide(); but it get real messy. Example:
$(".at&t_consumer_radio").click(function(){
showSaleType("at&t_consumer");
});
function showSaleType(type) {
if (type == "at&t_consumer") {
$('.name').hide();
$('.name').hide(); and so on..
}
}
When completing the form, then I use PHP to validate it.
Here's the Fiddle: http://jsfiddle.net/GvGoldmedal/FxEGP/
Your best bet is to create the following class tags:
.att, .verizon. , .tmobile, .consumer, .business, .sim
Then you can tag the fields based on when they need to be hidden. If a field is used for verison then it'd get a verizon class. If a field is bussiness type then it'd get the business class. Also add the "hide" class to any field that isn't ALWAYS showing.
Some Field:
<input type="text" class=" hide verizon att consumer" />
Another Field
<input type="text" class = " hide att business" />
Make sure to label every field. Also make sure your radio buttons and the options in your select have values that match the classes. And give your select and radio button a class of 'option', like so:
att
verizon
tmobile
<input type='radio' name='sale_type' class='option' value="business" />
<input type='radio' name='sale_type' class='option' value="consumer" />
<input type='radio' name='sale_type' class='option' value="consumer" />
.. and so on
Now for the Jquery:
//selectId is the ID of your select
// myform is the id of your form
$(".option").change(function()
{
// get the value of our select and option
carrier = $('#selectId option:selected').val();
sale_type = $('input[name=sale_type]:checked').val();
// Hide all fields that don't always show up.
$(".hide").hide();
//Show all fields that match our carrier and sale type
$(".hide").each(function(){
if($(this).hasClass(carrier) && $(this).hasClass(sale_type))
{
$(this).show();
}
});
});
There you have it. Anytime a new option is selected all the fields that aren't part of the form all the time will be hidden and the ones matching your carrier and sale type will then be shown. It will happen almost immediately and the user won't be able to tell the different. You can do some fading to make it look more smooth if you want. Also, if a field is visible for both verizon and att then just add both classes to that input. Let me know if you have any questions.
I'll try and get a Fiddle up showing exactly how to do it.
Hide all the fields that are only shown for specific choices e.g Business or sim free.
Like so
<input type="text" class="standard" />
<input type="text" class="business" style="display:none" />
<input type="text" class="simOnly" style="display:none" />
<input type="text" class="standard" />
<input type="text" class="business simOnly" style="display:none" />
Not sure i'm explaining this that well. :(
Then use jquery to just show the business ones if that option is chosen
$("#businessRadio").click(function(){
$('.business').show();
});
This way you aren't showing and hiding everything, just those inputs specific to each sale type
Related
I want to display the input from a input-box text, right beside the input-box if it got data.
I want to use it in our checkout for the customer name.
That when the customer has entered their name, it echo beside it "Hi (customer-name)"
The input-box is on the same page. So the echo needs to be displayed right beside the input-box.
The data needs to be displayed when the customer is not focussing anymore on this input-box. So when it clicks on an other input-box or beside it, it needs to be displayed.
How can I fix that?
If you are OK with pure inline javascript, you can attain this using onblur event:
<input type="text" value="" onblur="if(this.value !=''){document.getElementById('hi').innerHTML='Hi, '+this.value;}else{document.getElementById('hi').innerHTML='';}" />
<span id='hi'></span>
Empty span, or whatever other html place holder will fill with "Hi, (username)" if the input loses focus and is not empty.
You are looking for Javascript, rather than php.
To make it very simple:
function sayHi(){
var name = document.getElementById("box1").value;
document.getElementById("username").innerHTML = name;
}
<input type="text" id="box1" onblur="sayHi();"><span id="username"></span>
I'm working on a project where the document object model is loaded from jqote templates that use input names with brackets to generate arrays on postback. The project was built this way so the rows could be sortable using jquery sortable prior to postback.
<form id="the_form">
<input type="text" id="field1" name="options[]" /><br />
<input type="text" id="field2" name="options[]" /><br />
<input type="text" id="field3" name="options[]" /><br />
<input type="submit" value="submit" />
</form>
My problem is that jquery validate does not play nice with the field names that include brackets. Sometimes it validates and sometimes it misses a field or two. I've been reading a lot of other posts stating this should work and I am wondering if I am doing something wrong or if there's a work around.
jQuery("#the_form").validate({
rules: {
"options[]": {
required: true
}
}
});
I've created a jsFiddle so others can see what I'm experiencing. If you run the jsFiddle and select Submit in the form, you'll immediately notice it only validates the first field, however if you tab through the fields, you see a different behavior. Thanks!
http://jsfiddle.net/PV6j7/1/
That is because they are currently all the same name.
They need to be different names in order to work
Quote:
"...My problem is that jquery validate does not play nice with the field names that include brackets."
This is true, but that's easily fixed with quotation marks. However, "brackets" are not really the problem here.
The root problem is that the plugin will not operate properly without a unique name on each field. There is no way around this.
Using a jQuery $.each() to assign rules via the .rules('add') method is discussed elsewhere, but it's been disproved as a viable workaround. That's because the root problem is not with selecting elements & assigning rules; the problem is that the plugin uses the name attribute to keep track of the elements being validated. This is impossible if the name is the same on all.
failed jQuery .each() demo
try this:
jQuery("#the_form").validate({
rules: {
"options": {
required: true
}
}
});
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();
I'm trying to generate the total price on a one page store.
I have different products on the same page laid out like this within a foreach:
<input type="hidden" name="price_<?=$product_id?>" value="<?=$product_price?>" />
<input type="text" name="<?=$product_id?>" value="" onchange="calculateTotal();" />
So, the first input is hidden and contains the price of the product. The second input contains the quantity, with the name set as the product_id. The only issue is that there can be multiple inputs on one page.
Which would be the best way to do it? Either using Javascript to calculate the price, or an Ajax post and using PHP to do the calculations.
why not use the new data attribute and skip the hidden price field? Something like this:
<input class='quantity' type="number" data-price="1.5" name="prod1" value="" />
<div class='subtotal'>0.00</div>
<input class='quantity' type="number" data-price="7" name="prod2" value="" />
<div class='subtotal'>0.00</div>
<div id='total'>0.00</div>
with this:
$('.quantity').on('change', function(){
var sub = $(this).val() * $(this).data('price');
$(this).next('div.subtotal').html(sub).data('sub',sub);
var tot=0;
$('.subtotal').each(function(){
tot+= $(this).data('sub');
});
$('div#total').html(tot);
});
Noting that using HTML to store a price variable is usually bad etiquette why not keep a running total of all prices? Something like
$running_total = $running_total+$product_price;
This would output the total of all products that have been cycled through. If I misinterpreted your question I'm sorry.
Storing prices as a an HTML entity to be passed via POST or GET leaves the variable open for manipulation by the end user.
Do it however you want. As long as you perform the calculation at checkout (again?) on the server.
I am converting an old double dropdown box search form. With the old method, the form was submitted on each user selection using this:
<form name="navTwo">
<select name="item" id="item" onChange="document.location.href=document.navTwo.game.options[document.navTwo.game.selectedIndex].value">
The problem with the old method is that users were forced to look thru the second dropdown that contained an ever growing number of options.
I opted to make a new search using one auto submit drop down and a new jquery type ahead search field (thanks to Jamie McConnell, jamie#blue44.com). Everything works great with the type ahead. However, I cannot figure out how to submit the new form once the user picks the type ahead item. Ideally I would like to force the user to click submit once they've selected that second item.
I've tried carrying the id of the second search item and placing it in a hidden input but I cannot get the variable set to the id. Here is what I've tried so far, unsuccessfully;
//The page name is dash3.php
//If list = 1 it will add the record
//The jquery stuff works fine, it adds the value to the input field, I need it to grab the id of that record, not just the title. The $vid is empty, not sure how to set it.
//The code below is missing the submit button, I tried adding a link so that I could see the variables.
<form name="nav">
<div>
Start typing the name of the item, select the correct title when it appears:<br />
<input type="text" size="30" value="" id="inputString" onkeyup="lookup(this.value);" onblur="fill();" />
<input type="hidden" value="?list=1&ptfm_ctrl=1&vid=<?=$vid?>" />
</div>
<div class="suggestionsBox" id="suggestions" style="display: none;">
<img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
<div class="suggestionList" id="autoSuggestionsList">
</div>
</div>
<strong>+</strong>
</form>
So, to make my question more clear;
How can I grab the id of the type ahead record that is being placed in the input field by a jquery autocomplete script and make a self referencing form carrying this id to the same page?
Thanks much!
Maybe this example in the autocomplete documentation will help: http://jqueryui.com/demos/autocomplete/#custom-data
The idea is that you populate your autocomplete boxes with enough data for the user to select the right choice and then provide the id and hook into the success event.
select: function( event, ui ) { ... }
This might help get you started: http://jsfiddle.net/shanabus/HGF59/
Instead of the alert that fires upon the second dropdown selection, do something like update your form action:
$("#myForm").attr("action", $("#myForm").attr("action") + "/" + ui.item.id);
and then submit if needed. Hope this helps!