Is it possible to serialize a form in separate groups?
<form id="form">
<input class="nameF" type="text" name="fName" value="first name">
<input class="nameF" type="text" name="lName" value="last name">
<input class="address" type="text" name="addOne" value="home address">
<input class="address" type="text" name="zip" value="zip code">
<input class="address" type="text" name="country" value="country">
</form>
$('#form').serialize();
//this gives me the complete form serialized
but can I break it down in groups with the fields containing class name?
Something like nameF : first+name&last+name and same for address, is this doable?
You can do like :
console.log($('.nameF').serialize());
console.log($('.address').serialize());
If you didn't get your answer , elaborate your question with full example.
<form id="form">
<input class="nameF" type="text" name="fName" value="first name"/>
<input class="nameF" type="text" name="lName" value="last name"/>
<input class="address" type="text" name="address[addOne]" value="home address"/>
<input class="address" type="text" name="address[zip]" value="zip code"/>
<input class="address" type="text" name="address[country]" value="country"/>
</form>
fiddle
You can use -
$('input .nameF').serialize();
$('input .address').serialize();
You can test it using-
alert(JSON.stringify($('input .nameF').serialize()));
Related
I want this to apply to the age input box and to only except the values that are between 1 - 100,in 'javascript'.
<h1>Form</h1>
<form name="form" onsubmit="myFunction()" method="post" action="">
<div>
<input type="text" required minlength="3" maxlength="20" placeholder="Enter your first name">
</div>
<div>
<input type="text" minlength="3" maxlength="20" required placeholder="Enter your last name">
</div>
<div>
<input id="age" required name="number" type="number" placeholder="Enter your age">
</div>
<div>
<input type="text" required placeholder="Enter your email">
</div>
<div>
<input type="submit" required value="Submit">
</div>
</form>
The attributes min and max is probably what you're looking for.
<input id="age" required name="number" type="number" placeholder="Enter your age" min="1" max="100">
You may need to clarify what you mean by "only except the values that are between 1 - 100,in 'javascript'." if not.
I have a pretty basic form that allow user to post simple classified ads. It looks like this:
<form action="">
<fieldset>
<h3>Personal Informations</h3>
<input type="text" placeholder="Your Name">
<input type="text" placeholder="Your Address">
</fieldset>
<fieldset>
<h3>Car Informations</h3>
<input type="text" placeholder="Manufacturer">
<input type="text" placeholder="Model">
<input type="text" placeholder="Year">
Add More Ads
</fieldset>
</form>
I would like to allow the visitor to post more ads then one via "Add More Ads" button/link which would generate same 3 fields as much times as visitor needs ads.
What would be the simplest solution to do this? Any help/guide would be great!
Thanks!
P.S Also it would be great if the user could also remove the fields if he doesnt need them
Try this.
$('#addMOre').on('click',function(){
var self = $('.apendable').first().clone().insertAfter(".apendable:last");
$('.apendable:last input').val('');
self.append('<button class="close-info">close</button>');
$('.close-info').on('click', function(){
$(this).parent().remove();
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<fieldset>
<h3>Personal Informations</h3>
<input type="text" placeholder="Your Name">
<input type="text" placeholder="Your Address">
</fieldset>
<fieldset id="info">
<h3>Car Informations</h3>
<div class="apendable">
<input type="text" placeholder="Manufacturer">
<input type="text" placeholder="Model">
<input type="text" placeholder="Year">
</div>
Add More Ads
</fieldset>
</form>
You can set inputs in <div class="row"> then copy first row class.
$("#addAdsFiels").click(function(){
var clone = $(this).siblings(".row:first").clone();
$(clone).find("input").val("");
$(this).before(clone);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="">
<fieldset>
<h3>Personal Informations</h3>
<input type="text" placeholder="Your Name">
<input type="text" placeholder="Your Address">
</fieldset>
<fieldset>
<h3>Car Informations</h3>
<div class="row">
<input type="text" placeholder="Manufacturer">
<input type="text" placeholder="Model">
<input type="text" placeholder="Year">
</div>
Add More Ads
</fieldset>
</form>
POST http method in my form does not work when I use JavaScript:
<form id="user" method="post"action="url" onsubmit="return false;">
<span>First Name:</span>
<input type="text" class="input" id="fname" name="fname" required>
<span>last Name:</span>
<input type="text" class="input" id="lname" name="lname" required>
<input class="button" type="submit" value="submit" onclick = "checkForm()">
</form>
However, it does seem to work when I do not use JavaScript:
<form id="user" method="post"action="url">
<span>First Name:</span>
<input type="text" class="input" id="fname" name="fname" required>
<span>last Name:</span>
<input type="text" class="input" id="lname" name="lname" required>
<input class="button" type="submit" value="submit">
</form>
How can I use JavaScript validations in my form and send the form to a given URL?
<form id="user" method="post"action="url" onsubmit="return checkForm();">
<!-- blah blah-->
<input class="button" type="submit" value="submit">
</form>
Thanks for your help; this is working fine now.
It is not submitting to server because onsubmit="return false;" tells the browser the validation failed and do not send to server. If onsubmit is not specified or returns true the data will be submitted to the specified url. The following code should work if the function checkForm() returns true when passing validation and returns false while failing validation.
<form id="user" method="post" action="url" onsubmit="checkForm()">
<span>First Name:</span>
<input type="text" class="input" id="fname" name="fname" required>
<span>Last Name:</span>
<input type="text" class="input" id="lname" name="lname" required>
<input class="button" type="submit" value="submit">
</form>
when you reduce the browser window to iphone lay out....
you can see the different lay out....
the problem is right side of the text fields are being cut off...
how to fix it...
providing my code below.....
http://jsfiddle.net/jPvzL/1/
<input type="text" placeholder="Company Name " name="fullname" maxlength="30" required="required" id="id_username">
<input type="text" placeholder="Contact Name" name="company" maxlength="30" required="required" id="id_company">
<input type="text" placeholder="Address1" name="email" maxlength="75" required="required" id="id_email">
<input type="password" placeholder="Address2" name="password" maxlength="16" required="required" id="id_password">
<input type="password" placeholder="Address3" name="repassword" required="required" id="id_repassword">
<input type="text" placeholder="City " name="fullname" maxlength="30" required="required" id="id_username">
<div class="controls">
<select name="select" id="state" style="width: 300px;height: 38px;">
<option value="California" selected="">California</option>
<option value="New York">New York</option>
<option value="Texas">Texas</option>
</select>
</div>
<input type="text" placeholder="Zip" name="email" maxlength="75" required="required" id="id_email">
<div class="controls">
<select name="select" id="country" style="width: 300px;height: 38px;">
<option value="USA" selected="">USA</option>
<option value="India">India</option>
<option value="Korea">Korea</option>
<option value="Vietnam">Vietnam</option>
</select>
</div>
<input type="text" placeholder="Phone" name="company" maxlength="30" required="required" id="id_company">
<input type="text" placeholder="Fax" name="email" maxlength="75" required="required" id="id_email">
<input type="password" placeholder="Email" name="password" maxlength="16" required="required" id="id_password">
<input type="text" placeholder="URL" name="company" maxlength="30" required="required" id="id_company">
<input type="text" placeholder="GL Account" name="email" maxlength="75" required="required" id="id_email">
<input type="password" placeholder="" name="password" maxlength="16" required="required" id="id_password">
<textarea name="inquiry" placeholder="Notes" rows="3" required="required" id="id_inquiryForm" style="width:290px;"></textarea>
Looks like your #signup-modal div is set to a width of 125px. Just increase it to be the correct sizes to fit the inputs. Or remove 'overflow:hidden'
Had this issue a few weeks ago. I don't know what your CSS looks like, but try adding a percentage width to your input fields:
form input{width: 99%;}
YMMV. You may also have to add something for your textarea and select fields and tweak the percentage as needed.
I have a simple form using the Value to inform the user what they're to put in each text area. I have been able to make the text area's auto clear using this javascript:
<script type="text/javascript">
function setValue(field)
{
if(''!=field.defaultValue)
{
if(field.value==field.defaultValue)
{
field.value='';
}
else if(''==field.value)
{
field.value=field.defaultValue;
}
}
}
</script>
What would I need to add to have the Value re-appear if the user doesn't fill the text area and they no longer have it selected? Here's the form markup, if that helps any:
<form method="post" action="<?php echo $PHP_SELF; ?>">
<input type="text" name="first" value="First Name" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)">
<input type="text" name="last" value="Last Name" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)"> <br>
<input type="text" name="address" value="Address" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)">
<input type="text" name="city" value="City" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)"><br>
<input type="text" name="state" value="State" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)">
<input type="text" name="zip" value="Zip" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)"><br>
<input type="text" name="phone" value="Phone" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)">
<input type="radio" name="day" value="Day"> Day
<input type="radio" name="evening" value="Evening"> Evening <br>
<input type="text" name="email" value="Email" class="slide-form-input" onfocus="setValue(this)" onblur="setValue(this)">
<input type="submit" name="submit" value="Send Request" class="push_button blue">
(sorry for the poor wording, it's early!)
Thank you!
Any help with this would be much appreciated.
You are looking for the placeholder html5 attribute
<input type="text" palceholder="First Name" />
http://jsfiddle.net/MesvN/
http://davidwalsh.name/html5-placeholder