multipage form with jquery and php validation - php

I am working with a multipage form, needs to validate in each page. jquery validating well, but i have to post the data in the same page through ajax to backend for php validation and if return success needs to load the next page or else need to show validation error from backend to front end.
The problem is, here the form is getting validated well in frontend but not posting data for backend validation what i am doing wrong in the code please advice. Thanks in advance
HTML
<form class="ui form segment" id="msform" action="main.php" method="post">
<ul id="progressbar">
<li class="active">Account Setup</li>
<li>Career Details</li>
<li>Personal Details</li>
</ul>
<fieldset class="step">
<div class="two fields">
<div class="field">
<label>First name</label>
<input placeholder="First Name" name="first_name" type="text">
</div>
<div class="field">
<label>Last name</label>
<input placeholder="Last Name" name="last_name" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Display name</label>
<input placeholder="Display Name" name="display_name" type="text">
</div>
<div class="field">
<label>User name</label>
<input placeholder="User Name" name="user_name" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Email</label>
<input placeholder="Email" name="email" type="text">
</div>
<div class="field">
<label>Password</label>
<input type="password" placeholder="Password" name="password" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Date of birth</label>
<div class="ui calendar" id="date_birth">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input type="text" placeholder="Date" name="dob">
</div>
</div>
</div>
<div class="field">
<label>Gender</label>
<div class="ui selection dropdown">
<input name="gender" type="hidden">
<div class="default text">Gender</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="1">Male</div>
<div class="item" data-value="0">Female</div>
</div>
</div>
</div>
</div>
<div class="field">
<label>Marital status</label>
<div class="ui selection dropdown">
<input name="marital_status" type="hidden">
<div class="default text">Marital status</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="married">Married</div>
<div class="item" data-value="single">Single</div>
</div>
</div>
</div>
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset class="step">
<div class="two fields">
<div class="field">
<label>Highest qualification</label>
<input placeholder="Highest qualification" name="highest_qualification" type="text">
</div>
<div class="field">
<label>University/college</label>
<input placeholder="University/college" name="university_or_college" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Course type</label>
<div class="ui selection dropdown">
<input name="course_type" type="hidden">
<div class="default text">Course type</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="1">Full time</div>
<div class="item" data-value="0">Part time</div>
</div>
</div>
</div>
<div class="field">
<label>Mobile</label>
<input placeholder="Mobile" name="mobile" type="text">
</div>
</div>
<div class="field" id="container">
<label>Upload resume</label>
<label for="file" class="ui icon button">
<i class="file icon"></i>
<div id="filelist">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<a id="pickfiles" href="javascript:;">Select resume</a> <br />
</label>
<input type="file" id="file" style="display:none">
<a id="uploadfiles" href="javascript:;">Upload</a>
</div>
<div class="field" id="container">
<label>Upload photo</label>
<label for="file" class="ui icon button">
<i class="file icon"></i>
<div id="filelist_new">Your browser doesn't have Flash, Silverlight or HTML5 support.</div>
<a id="pickfiles_new" href="javascript:;">Select photo</a>
</label>
<a id="uploadfiles_new" href="javascript:;">Upload</a>
<input type="file" id="file" style="display:none">
</div>
<div class="two fields">
<div class="field">
<label>Current designation</label>
<input placeholder="Current designation" name="current_designation" type="text">
</div>
<div class="field">
<label>Current company</label>
<input placeholder="Current company" name="current_company" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Annual salary</label>
<div class="ui selection dropdown">
<input name="annual_salary" type="hidden">
<div class="default text">annual_salary</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="0">Less than 1 Lakh</div>
<div class="item" data-value="1">1 to 2 Lakh</div>
<div class="item" data-value="2">2 to 3 Lakh</div>
<div class="item" data-value="3">3 to 4 Lakh</div>
<div class="item" data-value="4">5 Lakh and above </div>
</div>
</div>
</div>
<div class="field">
<label>Working since</label>
<div class="ui calendar" id="date_work">
<div class="ui input left icon">
<i class="calendar icon"></i>
<input placeholder="Working since" name="working_since" type="text">
</div>
</div>
</div>
</div>
<div class="two fields">
<div class="field">
<label>Current location</label>
<input placeholder="Current location" name="current_location" type="text">
</div>
<div class="field">
<label>Duration of notice</label>
<div class="ui selection dropdown">
<input name="duration_of_notice" type="hidden">
<div class="default text">Duration of notice</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="0">Less than 15 days</div>
<div class="item" data-value="1">15 days to 1 month</div>
<div class="item" data-value="2">1 to 2 month</div>
</div>
</div>
</div>
</div>
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="button" name="next" class="next action-button" value="Next" />
</fieldset>
<fieldset class="step">
<div class="two fields">
<div class="field">
<label>Skills</label>
<input placeholder="Skills" name="skills" type="text">
</div>
<div class="field">
<label>Passing year</label>
<div class="ui calendar" id="example8">
<div class="ui input left icon">
<i class="time icon"></i>
<input type="text" name="passing_year" placeholder="Year">
</div>
</div>
</div>
</div>
<div class="two fields">
<div class="field">
<label> Present address</label>
<input placeholder="Present address" name="address_1" type="text">
</div>
<div class="field">
<label> Permanent address</label>
<input placeholder="Permanent address" name="address_2" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>City</label>
<input placeholder="City" name="city" type="text">
</div>
<div class="field">
<label>State</label>
<input placeholder="State" name="state" type="text">
</div>
</div>
<div class="two fields">
<div class="field">
<label>Country</label>
<input placeholder="Country" name="country" type="text">
</div>
<div class="field">
<label>Certifications</label>
<input placeholder="Certifications" name="certifications" type="text">
</div>
</div>
<div class="field">
<label>Will relocate </label>
<div class="ui selection dropdown">
<input name="will_relocate" type="hidden">
<div class="default text">Will relocate</div>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item" data-value="0">Yes</div>
<div class="item" data-value="1">No</div>
</div>
</div>
</div>
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" class="submit action-button" value="Submit" />
</fieldset>
</form>
jQuery
$(document).ready(function(){
var current = 1;
widget = $(".step");
btnnext = $(".next");
btnback = $(".previous");
btnsubmit = $(".submit");
// Init buttons and UI
widget.not(':eq(0)').hide();
hideButtons(current);
setProgress(current);
// Next button click action
btnnext.click(function(){
current_fs = $(this).parents();
next_fs = $(this).parent().widget;
if(current < widget.length){
// Check validation
if($(".form").valid()){
dataString = [];
$('#msform').ajaxForm({
type: "POST",
url: "main.php",
data:{data: $(dataString).serializeArray()},
dataType: "json",
cache: false,
});
widget.show();
$("#progressbar li").eq($("fieldset").index(setProgress(current))).addClass("active");
widget.not(':eq('+(current++)+')').hide();
setProgress(current);
}
}
hideButtons(current);
})
// Back button click action
btnback.click(function(){
current_fs = $(this).parent();
previous_fs = $(this).parent().btnback;
if(current > 1){
current = current - 2;
if(current < widget.length){
$("#progressbar li").eq($("fieldset").index(current_fs)).removeClass("active");
widget.show();
widget.not(':eq('+(current++)+')').hide();
setProgress(current);
}
}
hideButtons(current);
})
// Submit button click
btnsubmit.click(function(){
dataString = [];
$('#msform').ajaxForm({
type: "POST",
url: "main.php",
data:{data: $(dataString).serializeArray()},
dataType: "json",
cache: false,
success: function(){
alert("You have submitted form successfully");
// $("form")[0].reset();
}
});
});
$('#msform').validate({ // initialize plugin
ignore:":not(:visible)",
rules: {
first_name : { required: true, minlength: 3},
last_name : "required",
display_name : { required: true, minlength: 3},
user_name : { required: true, minlength: 3},
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
dob : {
required: true,
},
gender : "required",
marital_status: "required",
highest_qualification:"required",
university_or_college: "required",
mobile:{
required:true,
minlength:10,
maxlength:10,
number: true
},
current_designation:"required",
current_company:"required",
annual_salary:"required",
working_since:"required",
current_location:"required",
duration_of_notice:"required",
skills:"required",
address_1:"required",
city:"required",
state:"required",
country:"required",
certifications:"required",
},
});
});
// Change progress bar action
setProgress = function(currstep){
current_fs = $(this).parent();
next_fs = $(this).parent().btnnext;
var percent = parseFloat(100 / widget.length) * currstep;
percent = percent.toFixed();
$(".progress-bar").css("width",percent+"%").html(percent+"%");
$("#progressbar li").eq($(".step").index(current_fs)).removeClass("active");
}
// Hide buttons according to the current step
hideButtons = function(current){
var limit = parseInt(widget.length);
$(".action").hide();
if(current < limit) btnnext.show();
if(current > 1) btnback.show();
if (current == limit) {
btnnext.hide();
btnsubmit.show();
}
}
PHP
$post_data = $_POST;
print_r($post_data);

Related

I have both Login And Register form Inside a tab content. So the Problem Is That I can set action for any one form. How can I sove it

<form id=reg_form action="includes/data.inc.php" method="POST">
<div class="login-modal-right">
<!-- Login Form -->
<div class="tab-content">
<div class="tab-pane active" id="login" role="tabpanel">
<h5 class="heading-design-h5">Login to your account</h5>
<fieldset class="form-group">
<label>Enter Email/Mobile number</label>
<input type="text" name="email_num" class="form-control" placeholder="123 456 7890" required>
</fieldset>
<fieldset class="form-group">
<label>Enter Password</label>
<input type="password" name="password" class="form-control" placeholder="********" required>
</fieldset>
<fieldset class="form-group">
<button type="submit" name="login-submit" class="btn btn-lg btn-secondary btn-block">Enter My Account</button>
</fieldset>
</div>
<!-- Register Form -->
<div class="tab-pane" id="register" role="tabpanel">
<h5 class="heading-design-h5">Register Now!</h5>
<fieldset class="form-group">
<label for="reg-name">Full Name</label>
<input type="text" name="fullname" id="reg-name" class="form-control" placeholder="Your Full Name" required>
</fieldset>
<p id="namechk"></p>
<fieldset class="form-group">
<label for="reg-email">Enter Email</label>
<input type="email" name="email" id="reg-email" class="form-control" placeholder="example#gmail.com" required>
</fieldset>
<p id="emailchk"></p>
<fieldset class="form-group">
<label for="reg-num">Enter Mobile Number</label>
<input type="text" name="number" id="reg-num" class="form-control" placeholder="123 456 7890" required>
</fieldset>
<p id="numchk"></p>
<fieldset class="form-group">
<label for="reg-pass">Enter Password</label>
<input type="password" name="pass" id="reg-pass" class="form-control" placeholder="********" required>
</fieldset>
<p id="passchk"></p>
<!-- <div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input">
<label class="custom-control-label" for="customCheck2">I Agree with Term and Conditions</label>
</div> -->
<fieldset class="form-group">
<input type="submit" id="reg-sub" value="Create Your Account" class="btn btn-lg btn-secondary btn-block" role="button">
</fieldset>
</div>
</div>
<div class="clearfix"></div>
<div class="text-center login-footer-tab">
<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<a class="nav-link active" data-toggle="tab" href="#login" role="tab"><i class="mdi mdi-lock"></i> LOGIN</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#register" role="tab"><i class="mdi mdi-pencil"></i> REGISTER</a>
</li>
</ul>
</div>
<div class="clearfix"></div>
</div>
</form>
The Problem Is I can only set an action for any one form. Let say if I set the action for the registration form then where or how I can set the action for the login form. I have even tried separating them, but it actually breaks the design. If anyone can resolve it with design (Just in case if I did any error) then you are most welcome. Or if there's any way I can set action for both of them separately. I do tried manipulating both of them from the same page(action page) but again it doesn't work.
Put separate form tag for each tab
<div class="tab-pane active" id="login" role="tabpanel">
<form>
</form>
</div>
<div class="tab-pane" id="register" role="tabpanel">
<form>
</form>
</div>

php - submit button is not working in isset

I am trying to add a form in my html which will get 12 input box and save it to mysql. but its echoing failed as because of submit button perhaps. because in if for the isset submit button the else is forwarding message. Code for my html
<body class="templatemo-bg-image-2">
<div class="container">
<div class="col-md-12">
<form class="form-horizontal templatemo-contact-form-1" role="form" action="input.php" method="post">
<div class="form-group">
<div class="col-md-12">
<h1 class="margin-bottom-15">Marketing Samurai 2017</h1>
<h2 class="margin-bottom-15">Registration form</h2>
<p>This contact form is provided by templatemo that can be used for your websites. Credit goes to <a rel="nofollow" href="http://unsplash.com">Unsplash</a> for the background image.</p>
</div>
<hr>
<h4 class="margin-bottom-15">Team Info</h4>
<hr>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">University Name</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-university"></i>
<input name="uname" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Team</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-users"></i>
<input name="tname" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Trix Id</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-money"></i>
<input name="tcode" type="text" class="form-control" id="name" placeholder="Bkash transection Code">
</div>
</div>
</div>
<h4 class="margin-bottom-15">Leader Info</h4>
<hr>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-user"></i>
<input name="lname" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Email</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-envelope-o"></i>
<input name="lmail" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Contact</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-phone"></i>
<input name="lcont" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<h4 class="margin-bottom-15">1st Member Info</h4>
<hr>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-user"></i>
<input name="m1name" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Email</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-envelope-o"></i>
<input name="m1mail" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Contact</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-phone"></i>
<input name="m1cont" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<h4 class="margin-bottom-15">2nd Member Info</h4>
<hr>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Name</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-user"></i>
<input name="m2name" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Email</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-envelope-o"></i>
<input name="m2mail" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<label for="name" class="control-label">Contact</label>
<div class="templatemo-input-icon-container">
<i class="fa fa-phone"></i>
<input name="m2cont" type="text" class="form-control" id="name" placeholder="">
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<button type="submit" value="Submit" name="submit" class="btn btn-success pull-right">></button>
</div>
</div>
</form>
</div>
</div>
Code for php
<?php
$con = mysqli_connect('localhost','root','');
if(!$con){
echo 'Not Connected To Server';
}
if (!mysqli_select_db ($con,'test')) {
echo 'Database Not Selected';
}
if (isset($_POST['submit'])){
$uni = $_POST['uname'];
$team = $_POST['tname'];
$trix = $_POST['tcode'];
$leader = $_POST['lname'];
$lemail = $_POST['lmail'];
$leadercon = $_POST['lcont'];
$mname = $_POST['m1name'];
$memail = $_POST['m1mail'];
$mcon = $_POST['m1cont'];
$mname3 = $_POST['m2name'];
$memail3 = $_POST['m2mail'];
$mcon3 = $_POST['m2cont'];
$sql = "INSERT INTO 'memberdetails' (uninam, team, trixid, mem, mail, cont, mem1, mail1, con1, mem2, mail2, con2) VALUES ('$uni', '$team', '$trix', '$leader', '$lemail', '$leadercon', '$mname', '$memail', '$mcon', '$mname3', '$memail3', '$mcon3' )";
if (!mysqli_query($con,$sql))
{
echo 'Not Inserted';
}
else
{
echo 'Inserted Successfully';
}
header("refresh:2; url=index.html");
} else {
echo "failed";
}
I haven't made any validation yet. and I am trying with msqli and database, table is connecting then why the error is coming? I just made the submit button just to insert docs. whats should I do to prevent the fail message. Thanks in advance. :)
Submit must be <input> with name or it won't be available using $_POST['submit']
<input type="submit" value="Submit" name="submit" />
Try to remove "submit" text from name of form button and replace with another text
<button type="submit" value="Submit" name="form_sumit" class="btn btn-success pull-right">></button>

Thank you message not showing when Ajax form goes through

I'm trying to hide the regular content and display a thank you message in it's place after a form is sent. However when the form sends, the fields reset but the thank you message won't appear. Please help. I'm not that great with jQuery.
I'm also using the foundation abide.js, don't know if that makes difference or not.
Here is the ajax code.
$(function() {
// Get the form.
var form = $('#form');
// Get the messages div.
var formMessages = $('#thanks');
// Set up an event listener for the contact form.
$(form).submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
}).done(function(response) {
$('#thanks').show();
$(form).hide();
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#number').val('');
})
});
});
This is the code for the form.
<div class="newsletter" id="contact">
<div class="row footer-top">
<div class="large-12 medium-12 columns updates" id="thanks" style="display: none;">
<h4>Thanks! We'll be in touch shortly.</h4>
</div>
<div class=" large-12 medium-12 column updates" id="normal">
<h4>Want more info? Contact us!</h4>
<p>Sign up to stay in touch with what's happening. Don't Worry, we don't spam.</p>
</div>
</div>
<form data-abide id="form" method="post" action="wp-content/themes/myles_custom/mailer.php">
<div class="row">
<div class="large-8 large-centered columns">
<label>
Name:
<input type="text" name="name" id="name" placeholder="Name" value="<?php if(isset($name)){echo htmlspecialchars($name);} ?>" required pattern="[a-zA-Z]+">
</label>
<small class="error">Name is required.</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Email:
<input type="email" id="email" name="email" placeholder="Email" value="<?php if(isset($email)){echo htmlspecialchars($email);} ?>" required>
</label>
<small class="error">Email is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Number:
<input type="tel" pattern="number" id="number" name="number" placeholder="Name" value="<?php if(isset($number)){echo htmlspecialchars($number);} ?>" required>
</label>
<small class="error">Number is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Campus:
<select id="campus" name="campus">
<option>FSU</option>
<option>FAMU</option>
<option>TCC</option>
</select>
</label>
<small class="error">Campus is required</small>
</div>
</div>
<div id="sucks" style="display: none;">
<label for="address" id="adress_label" style: "display: none;">Address</label>
<input name="address" id="address" type="text" style="display: none;">
</div>
<div class="row">
<div class="large-12 columns" style="text-align: center;">
<button id="submit" type="submit" value="Send">Submit</button>
</div>
</div>
</form>
</div>
You have a extra closing bracket in the code, and I also would not use $(form) with jquery instead use an id reference $("#form").
I made a quick working example at https://jsfiddle.net/mum1m1sc/
$("#myForm").submit(function(event) {
// Stop the browser from submitting the form.
event.preventDefault();
var formData = $("#myForm").serialize();
$.ajax({
type: 'POST',
url: $("#myForm").attr('action'),
data: formData
}).done(function(response) {
$('#thanks').show();
$("#myForm").hide();
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#number').val('');
});
});
<div class="newsletter" id="contact">
<div class="row footer-top">
<div class="large-12 medium-12 columns updates" id="thanks" style="display: none;">
<h4>Thanks! We'll be in touch shortly.</h4>
</div>
<div class=" large-12 medium-12 column updates" id="normal">
<h4>Want more info? Contact us!</h4>
<p>Sign up to stay in touch with what's happening. Don't Worry, we don't spam.</p>
</div>
</div>
<form data-abide id="myForm" method="post" action="">
<div class="row">
<div class="large-8 large-centered columns">
<label>
Name:
<input type="text" name="name" id="name" placeholder="Name" value="" required pattern="[a-zA-Z]+">
</label>
<small class="error">Name is required.</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Email:
<input type="email" id="email" name="email" placeholder="Email" value="" required>
</label>
<small class="error">Email is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Number:
<input type="tel" id="number" name="number" placeholder="Name" value="">
</label>
<small class="error">Number is required</small>
</div>
</div>
<div class="row">
<div class="large-8 large-centered columns">
<label>
Campus:
<select id="campus" name="campus">
<option>FSU</option>
<option>FAMU</option>
<option>TCC</option>
</select>
</label>
<small class="error">Campus is required</small>
</div>
</div>
<div id="sucks" style="display: none;">
<label for="address" id="adress_label" style: "display: none;">Address</label>
<input name="address" id="address" type="text" style="display: none;">
</div>
<div class="row">
<div class="large-12 columns" style="text-align: center;">
<button id="submit" type="submit" value="Send">Submit</button>
</div>
</div>
</form>
<div id="thanks" style="display:none;">
<h3>Thanks</h3>
</div>
hope that helps.

$.ajax inside load function not working

How to serialize this form data inside load function.it's serialize only radio value.when i click change value and hit "save" button ajax is working only for the radio...what is the problem could be?
Javascript
$(document).ready(function() {
$("#pard_configure_a").click(function() {
$("#pard_admin").load('../pard_admin/view/configure.php', function() {
$(":submit").click(function(event) {
event.preventDefault();
var data = $("#pard_site_configuration").serialize();
$("#hhh").html(data);
$.ajax({
url: "../controller/configure.controller.php",
type: "POST",
data:data,
success: function(responce) {
//$("#res").html(responce);
}
});
});
});
});
});
HTML
<div id="pard_configure">
<p>
<form id="pard_site_configuration" method="post">
<fieldset>
<legend >Site Configuration</legend>
<!-- Site Settings-->
<div id="pard_site_settings">
<label class="pard_site_settings">Site Settings</label>
<div class="input-prepend">
<span class="add-on">Site Title</span>
<input class="span6" id="prependedInput" type="text" >
</div>
<br/>
<div id="pard_site_offline_radio" class="input-prepend">
<span class="add-on">Site Offline</span>
<label id="radio_1" class="radio">
<span class="add-on">ON</span>
<input type="radio" name="optionsRadios" id="optionsRadios1" value="option1" checked>
</label>
<label id="radio_2" class="radio">
<span class="add-on">OFF</span>
<input type="radio" name="optionsRadios" id="optionsRadios2" value="option2">
</label>
</div>
<div class="input-prepend">
<span class="add-on">Admin Email</span>
<input class="span3" id="prependedInput" type="text" placeholder="example#domain.com">
</div>
<div class="input-prepend">
<span class="add-on">Meta Description</span>
<br/>
<textarea rows="3"></textarea>
</div>
<div class="input-prepend">
<span class="add-on">Meta Keywords</span>
<br/>
<textarea rows="3"></textarea>
</div>
<div class="input-prepend">
<span class="add-on">Copyright</span>
<br/>
<textarea rows="3"></textarea>
</div>
</div><!-- Site Settings end-->
<!-- Database Settings-->
<div id="pard_database_settings">
<label class="pard_database_settings">Database Settings</label>
<div class="input-prepend">
<span class="add-on">Host</span>
<input class="span2" id="prependedInput" type="text" placeholder="localhost">
</div>
<div class="input-prepend">
<span class="add-on">Database Name</span>
<input class="span2" id="prependedInput" type="text" placeholder="database">
</div>
<div class="input-prepend">
<span class="add-on">Database Username</span>
<input class="span2" id="prependedInput" type="text" placeholder="Username">
</div>
<div class="input-prepend">
<span class="add-on">Database Password</span>
<input class="span2" id="prependedInput" type="text" placeholder="Password">
</div>
</div><!-- Database Settings end-->
<div id="pard_admin_form_button">
<input id="pard_site_configuration_submit" class="btn btn-small btn-primary" type="submit" value="Save"/>
<input class="btn btn-small" type="submit" value="Close" /></div>
</fieldset></form>
</p><div id="hhh"></div></div>
Serialize Result in id="hhh"
optionsRadios=option1
NOTE i set id="hhh" as the temorory place to see serialize data
Make sure you have name attribute in all your form inputs:
<input name="someName" class="span6" id="prependedInput" type="text" >
Your code is working for radio buttons because they have a name attribute. As you are using .serialize(), this ignores elements that do not have name attributes.

Two Forms In a HTML Document

I am having a form as below.
<div class="container">
<form method="post" action="index.php">
<div id="rule">
<div class="row">
<div class="span5">
<input type="text" class="input-xlarge" value="Rule Name"/>
</div>
<div class="span6">
<input type="text" class="input-xxlarge" value="Description"/>
</div>
<div class="clearfix"> </div>
</div>
</div>
<br />
<div id="sel" class="select">
<div class="row">
<div class="span1 offset2">
<p class="btn btn-danger">If</p>
</div>
<div class="span2">
<select class="span2" name="metrics">
<option value="bounce_rate">Bounce Rate</option>
<option value="location">Locations</option>
<option value="social_media">Social Media</option>
<option value="search">Search</option>
<option value="visits">Visitors</option>
</select>
</div>
<div class="span1">
<select name="conditions" class="span2">
<option value=">">></option>
<option value="<"><</option>
<option value=">=">>=</option>
<option value="<="><=</option>
<option value="=">=</option>
</select>
</div>
<div class="span1 offset1">
<input type="text" name="percentage" class="input-small"/>
</div>
<div class="clearfix"> </div>
</div>
<br />
<div class="row">
<div class="span1 offset2">
<p class="btn btn-danger">Period</p>
</div>
<div class="span3">
<div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
<input class="span2" size="16" type="text" value="12-02-2012" />
<span class="add-on"><i class="icon-th"></i></span>
</div>
</div>
<div class="span3">
<div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
<input class="span2" size="16" type="text" value="12-02-2012" />
<span class="add-on"><i class="icon-th"></i></span>
</div>
</div>
<div class="clearfix"> </div>
</div>
</div>
<div id="dyna"></div>
<div id="end">
<div class="row">
<div class="span1">
<p class="btn btn-danger">THEN</p>
</div>
<div class="span3">
<input type="text" class="input-xlarge" value="Statement" />
</div>
<div class="clearfix"> </div>
</div>
<div class="row">
<div class="span1">
<p class="btn btn-danger">ELSE</p>
</div>
<div class="span3">
<input type="text" class="input-xlarge" value="Statement" />
</div>
<div class="clearfix"> </div>
</div>
<div class="row">
<button class="span1 btn btn-success" type="submit" value="submit">Submit</button>
<div class="span2 offset9">
<p class="btn btn-success input-small" id="addRule">Add Rule </p>
</div>
<div class="clearfix"> </div>
</div>
</div>
</form>
</div>
I have included a button 'Add Rule' to create a dynamic elements within the above . So I used a javascript for creating dynamic elements as same of above.
The javascript as below.
<script type="text/javascript">
$('.datepicker').datepicker();
(function(){
var i = 0;
$("#addRule").live('click',function(){
++i;
var $ctrl = $('<div id="'+ i +'" class="select"><div class="row"><div class="span1 offset2"><button class="btn btn-danger">If</button></div><div class="span2"><select class="span2" name="metrics"><option value="bounce_rate">Bounce Rate</option><option value="location">Locations</option><option value="social_media">Social Media</option><option value="search">Search</option><option value="visits">Visitors</option></select></div><div class="span1"><select name="conditions" class="span2"><option value=">">></option><option value="<"><</option><option value=">=">>=</option><option value="<="><=</option><option value="=">=</option></select></div><div class="span1 offset1"><input type="text" name="percentage" class="input-small"/></div><div class="span2 offset1"><button class="btn btn-danger remove" id="'+ i +'">Remove</button></div><div class="clearfix"> </div></div><br /><div class="row"><div class="span1 offset2"><p class="btn btn-danger">Period</p></div><div class="span3"><div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"><input class="span2" size="16" type="text" value="12-02-2012" /><span class="add-on"><i class="icon-th"></i></span></div></div><div class="span3"><div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"><input class="span2" size="16" type="text" value="12-02-2012" /><span class="add-on"><i class="icon-th"></i></span></div></div><div class="clearfix"> </div></div></div>');
$("#dyna").append($ctrl);
$('.datepicker').datepicker();
});
}) ();
(function(){
$(".remove").live('click',function(){
var remDiv = $(this).attr('id');
//var questionList = "questionList"+remQ;
$("#" + remDiv).remove();
});
}) ();
So I wanna get the values once i click the submit button. Is it correct way what i am using only a form for fetching the data or am i create two forms separately? So how to do that?
You do not need to create a second form if you are inserting the new elements into the existing form and as long as they have different names from any existing inputs you want to receive.
You can create a second form if you want different submit buttons for the two different collections of input data.

Categories