i use the following code to load the form as below code
as Ajax code as below
function assessment_form() {
$this = $(this);
$.ajax({
url: 'view/assessment/assessment_form.php',
datatype: 'html',
success: function (data) {
$box = $('.span12');
$box.after(data);
$box.remove();
}
});
}
$(document).on('click', '#assessment_form', $(this), assessment_form);
and the form page as below in directory view/assessment/assessment_form.php
Add New User
<div class="box span12">
<div class="box-header well" data-original-title>
<h2> Add New User</h2>
</div>
<div class="box-content">
<form class="form-horizontal" id="user_form_data" enctype="multipart/form-data">
<fieldset>
<div class="control-group">
<label class="control-label" for="firstname">ROTC NO</label>
<div class="controls">
<input class="input-xlarge focused" id="rotc_no" name="rotc_no" type="text" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="lastname">Password</label>
<div class="controls">
<input class="input-xlarge focused" id="password" name="password" type="text" />
</div>
</div>
Simply, You can load html page into a div by using jquery's load() function.
Try this:
$('#yourDiv').load('pages/page1.html');
use this $(document).on('click','#assessment_form', assessment_form);
instead of $(document).on('click','#assessment_form',$(this),assessment_form);
Take a look for more information $.get()
// Make it little more flexible and easy
function assessment_form() {
$.get('your_url', function(data){
$box = $('.span12');
$box.after(data);
$box.remove();
});
}
Related
I have 2 Google Recaptcha's on same page in different forms (Login and Register Forms). I had to put both the forms on the same page due to the designing requirements which can't be changed. Now the problem is that the 1st form is the login form followed by the register form in the 2nd. Now, the recaptcha on the login form takes effect for register form as well. I mean that I can see recaptcha on both the forms but the one in register form doesn't work (which means it doesn't sends the correct value to the PHP page where the form will be processed). When I tried verifying the recaptcha on the login form and submitting the register form it worked. This means that the 1st loaded recaptcha (on the login form) is working for both which should not be. Both the recaptchas should work separately for each forms. Also, when I get the values of both the recaptchas via AJAX to send to the PHP page for the server side validation I am using captcha: grecaptcha.getResponse() for both. THIS CAN BE THE ACTUAL PROBLEM I GUESS. If I could get separate values for both maybe the things would have gone down well. Any help would be appreciated.
Here are my codes.
<div class="container">
<div class="row">
<div class="col-md-6">
<h3 class="com-reps cr-panel">Login Now</h3>
</div>
<div class="col-md-6">
<h3 class="com-reps cr-panel">Register Now</h3>
</div>
<div class="col-md-6">
<div class="post-holder">
<div class="login-message"></div>
<div class="post-content">
<form method="post" action="">
<div class="form-group">
<input type="text" id="uname" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="pass" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="<?php echo $mainf['set_recaptcha_sitekey']; ?>"></div>
</div>
<div class="form-group">
<input type="submit" id="login" value="Log In" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="post-holder">
<div class="register-message"></div>
<div class="post-content">
<form method="post" action="">
<div class="form-group">
<input type="text" id="name" class="form-control" placeholder="Full Name">
</div>
<div class="form-group">
<input type="text" id="username" class="form-control" placeholder="Username">
</div>
<div class="form-group">
<input type="password" id="password" class="form-control" placeholder="Password">
</div>
<div class="form-group">
<input type="password" id="cpass" class="form-control" placeholder="Confirm Password">
</div>
<div class="form-group">
<input type="text" id="email" class="form-control" placeholder="Email">
</div>
<div class="form-group">
<input type="text" id="dob" class="form-control" placeholder="Date of Birth">
</div>
<div class="form-group">
Sex
<input type="radio" id="sex" name="sex"> Male
<input type="radio" id="sex" name="sex"> Female
</div>
<div class="form-group">
<input type="checkbox" id="legal" name="legal"> I accept the Terms of Service and Privacy Policy.
</div>
<div class="form-group">
<div class="g-recaptcha" data-sitekey="<?php echo $mainf['set_recaptcha_sitekey']; ?>"></div>
</div>
<div class="form-group">
<input type="submit" id="submit" value="Register" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function() {
$("#login").click(function(){
var dataString = {
uname: $("#uname").val(),
pass: $("#pass").val(),
captcha: grecaptcha.getResponse()
};
$.ajax({
type: "POST",
//dataType : "json",
url: "processes/login.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#login").val("Please wait...");
$(".login-message").hide();
},
success: function(html){
$('.login-message').html(html).fadeIn();
if($('.login-message').find('#responseBox').hasClass('alert-success')){
setTimeout(function(){
window.location.replace("index.php");
}, 500);
}else{
$("#login").val("Log In");
}
}
});
return false;
});
$("#submit").click(function(){
var dataString = {
name: $("#name").val(),
uname: $("#username").val(),
pass: $("#password").val(),
cpass: $("#cpass").val(),
email: $("#email").val(),
dob: $("#dob").val(),
sex: $("input[name='sex']:checked").val(),
captcha: grecaptcha.getResponse()
};
$.ajax({
type: "POST",
//dataType : "json",
url: "processes/register.php",
data: dataString,
cache: true,
beforeSend: function(){
$("#submit").val("Please wait...");
$(".register-message").hide();
},
success: function(html){
$('.register-message').html(html).fadeIn();
$("#submit").val("Register");
}
});
return false;
});
});
</script>
I got the solution. In case someone comes looking for this it will help them.
In both HTML Forms replace
<div class="g-recaptcha" data-sitekey="YOUR_SITE_KEY"></div>
to
<div id="recaptcha1"></div> and <div id="recaptcha2"></div> respectively.
Then replace your script calling from
<script src="https://www.google.com/recaptcha/api.js></script>
to
<script src="https://www.google.com/recaptcha/api.js?onload=myCallBack&render=explicit" async defer></script>
And insert these code in your <script></script>. Note that this should be outside of $(document).ready(function() { and above your next form submission AJAX codes.
var recaptcha1;
var recaptcha2;
var myCallBack = function() {
recaptcha1 = grecaptcha.render('recaptcha1', {
'sitekey' : 'YOUR_SITE_KEY'
});
recaptcha2 = grecaptcha.render('recaptcha2', {
'sitekey' : 'YOUR_SITE_KEY'
});
};
Next and last thing, change the captcha: grecaptcha.getResponse() in your form submission AJAX code to captcha: grecaptcha.getResponse(recaptcha1) and captcha: grecaptcha.getResponse(recaptcha2) respectively (for both AJAX codes of login and register forms in my case). For full form submission code please refer to the code in my question above. Just replace the captcha: part here as said.
Done! Now you can proceed to server side validation in your language (PHP or ASP whatever).
Right now I am facing a problem : I am trying to insert records in the database with the help of jQuery & Ajax. Unfortunately, I tried to alert inserted values but It doesn't show. I also checked through serialize function and I am unable to do that.
Here is my code of ajax
<script type="text/javascript">
$(document).ready(function(){
$("#add_new").click(function(){
$("#add").slideToggle();
});
$("#submit").click(function(){
var stud_no=$("#roll").val();
if(stud_no==''){
$("#msg").html("Input Roll Number");
} else {
var datastr = $("#sampleform").serialize();
alert(datastr);
$.ajax({
type:'POST',
url:'add_it.php',
data:datastr,
success:function(response){
$("#my_form")[0].reset();
$("#msg").html("Student Successfully Added");
},
error: function (xhr, ajaxOptions, thrownError) {
}
});
}
});
});
</script>
Here is body code :
<body>
<a id="add_new">+add new item</a><br /><br />
<div id="msg"></div><br />
<div id="add">
<form id="sampleform">
<fieldset>
<div class="form-group row">
<label for="roll" class="col-sm-2 col-form-label">Roll Number</label>
<div class="col-sm-6">
<input type="text" name="roll" class="form-control" id="roll">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-6">
<input type="text" name="name" class="form-control" id="name">
</div>
</div>
<div class="form-group row">
<label for="clas" class="col-sm-2 col-form-label">Class</label>
<div class="col-sm-6">
<input type="text" name="standard" class="form-control" id="standard">
</div>
</div>
<div class="form-group row">
<label for="mail" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-6">
<input type="email" name="mail" class="form-control" id="mail">
</div>
</div>
<button type="submit" id="submit" class="btn btn-primary">Submit</button>
<button type="reset" class="btn btn-default">Reset</button>
</fieldset>
</fieldset>
</form>
</div>
Here is my add_it.php
<?php
include('connectdb.php');
$stud_no = trim($_POST['stud_no']);
$name = trim($_POST['name']);
$standard = trim($_POST['standard']);
$mail = trim($_POST['mail']);
$query = "insert into student (stud_no,name,standard,email) values ('$stud_no','$name','$standard','$mail')";
mysqli_query($con,$query) or die (mysqli_error());
?>
Your HTML form fields doesnt have any of these variables sent. You need to add name="email" etc to your form fields.
So for example the email field has to look like this:
<input type="email" name="email" class="form-control" id="mail">
id, class etc is not sent in POST - and therefor can not be recieved in the other end.
Jquery's serialize() function also only handles "name" fields.
https://api.jquery.com/serialize/
Snippet :
For a form element's value to be included in the serialized string, the element must have a name attribute
I've been trying to use ajax to insert data into my database but it didn't work. So I wrote a code to try a basic call for a php file and it didn't work either:
here is my html code :
<form >
<h4 class="header-title m-t-0 m-b-30">Cordonnées de facturation</h4>
<div class="form-group">
<label class="col-md-4 control-label">code facture</label>
<div class="col-md-6">
<input type="text" id="code_facture" class="code_facture form-control" placeholder="Ex : 008A00098">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Numéro du dossier</label>
<div class="col-md-6">
<input type="text" id="code_dossier" class="code_dossier form-control" placeholder="Ex : 008A00098">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">TPS</label>
<div class="col-md-6">
<input type="text" id="tps" class="tps form-control" placeholder="Ex : 0">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">TVQ</label>
<div class="col-md-6">
<input type="text" id="tvq" class="tvq form-control" placeholder="Ex : 0">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Référence</label>
<div class="col-md-6">
<input type="text" id="reference" class="reference form-control" placeholder="Ex : VAR R4055555">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">PNR</label>
<div class="col-md-6">
<input type="text" id="pnr" class="pnr form-control" placeholder="Ex : M15478LD265">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Date de facturation</label>
<div class="col-md-6">
<input type="text" placeholder="12/12/2012" data-mask="99/99/9999" id="date_facturation" class="date_facturation form-control">
<span class="font-13 text-muted">jj/mm/aaaa</span>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn" id="addfacturebutton"> ajouter</button>
</div>
and here is my ajax script
<script>
$('#addfacturebutton').click(function(e){
e.preventDefault();
$.ajax({
type: 'POST',
url: 'insertFacture.php',
data:{
code_facture:$('input[id=code_facture').val(),
code_dossier:$('input[id=code_dossier').val(),
tps:$('input[id=tps').val(),
tvq:$('input[id=tvq').val(),
reference:$('input[id=reference').val(),
pnr:$('input[id=pnr').val(),
date_facturation:$('input[id=date_facturation').val(),
},
success:function(data){
},
});
});
</script>
so in my php file I tried to write die('test'); but inserting into database didn't work either.
it doesn't call the functions
here is my php file:
<?php
include('functions.php');
$data = $_POST['code_dossier'];
die('works');
?>
Have you tried to dump $_POST of your insertFacture.php file? It seems like you do have wrong php filename or something wrong with the functions.php
Your code does not confirm if the js code works or not.
Try this:
Change this line:
success:function(data){
}
to:
success:function(data){
alert(data)
}
And change every occurrence of:
').val()
to
]').val()
If there is an alert, then the js is working. And I cannot see anything related to database in your php code.
To reduce the lines of code in ajax, you can alternatively add an id attribute on the form tag and then either serialize() or use FormData()
<form id='myFormTag'>
Then in ajax, simply call FormData() on submit()
$('#myFormTag').submit(function(e){
e.preventDefault();
$.ajax({
data: new FormData(this),
//other AJax Stuff
success:function(data){
alert(data)
}
});
});
I am trying to get select menus show hide by using ajax and serialised hashes. I had this system working last night but I changed the #selector from a form to a div and suddenly its stopped running. I had to broaden the form for additional data on post and did not want to serialise all the data at once for this as it would be additional strain on the system.
The page somewhat works as expected. It shows the first select, allows me to select an option, I can see the AJAX posting but the hash value is empty which I believe is breaking the PHP above. I cant figure out why the hash is posting empty. I assume its not getting the value from the select but I cant work out why..
If possible can you please point out where I am going wrong?
<section id="add">
<div class="container">
<form method="post">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><strong>Step 1: Instance Select</strong></h3>
</div>
<div id="selector">
<div class="panel-body">
<div class="col-md-6">
<select class="form-control box1" name="box1"></select>
<input name="box1hash" class="box1hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box2" name="box2"></select>
<input name="box2hash" class="box2hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box3" name="box3"></select>
<input name="box3hash" class="box3hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box4" name="box4"></select>
<input name="box4hash" class="box4hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box5" name="box5"></select>
<input name="box5hash" class="box5hash" type="hidden" />
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><strong>Step 2: Event Details</strong></h3>
</div>
<div class="panel-body">
<input name="event_name" type="text" class="form-control" />
</div>
</div>
<input type="submit"/>
</form>
</div>
$(document).on('change', '#selector', function(e) {
ajax_post(this);
});
ajax_post();
})(jQuery);
function show_hide_select(select){
if ($(select).find('option').length < 1) {
$(select).hide();
} else {
$(select).show();
}
}
function ajax_post(element=null) {
var frm = $('#selector');
if (element != null) {
// Reset selections
var found=false;
frm.find('select').each(function(e){
if (found==true) {
$(this).hide().find('option:selected').prop("selected", false)
}
if (element==this) found=true;
});
}
$.ajax({
url: '?ajax=1',
type: "POST",
data: frm.serialize(),
dataType: 'json',
success: function (data) {
if (data.box1hash != frm.find('.box1hash').val()) {
frm.find('.box1').html(data.box1?data.box1:'');
frm.find('.box1hash').val(data.box1hash);
show_hide_select(frm.find('.box1'));
}
if (data.box2hash != frm.find('.box2hash').val()) {
frm.find('.box2').html(data.box2?data.box2:'');
frm.find('.box2hash').val(data.box2hash);
show_hide_select(frm.find('.box2'));
}
if (data.box3hash != frm.find('.box3hash').val()) {
frm.find('.box3').html(data.box3?data.box3:'');
frm.find('.box3hash').val(data.box3hash);
show_hide_select(frm.find('.box3'));
}
if (data.box4hash != frm.find('.box4hash').val()) {
frm.find('.box4').html(data.box4?data.box4:'');
frm.find('.box4hash').val(data.box4hash);
show_hide_select(frm.find('.box4'));
}
if (data.box5hash != frm.find('.box5hash').val()) {
frm.find('.box5').html(data.box5?data.box5:'');
frm.find('.box5hash').val(data.box5hash);
show_hide_select(frm.find('.box5'));
}
}
});
}
</script>
Contrary to my earlier comments, a form tag cannot be nested within another form tag. jquery serialize() only works on forms, so unfortunately it's not possible to focus it on a div component of the form. Instead however you can serialize the whole form as a string, and then extract the subsection of that string for reduced ajax posting. Snippet below with inline comments...
$('form').on({
'change':function(e){
e.preventDefault();
var dats = $(this).serialize(); // serialize the whole form into a string
var pico = dats.indexOf('&event_name='); // get the index of the end point of the desired data
var newDats = dats.slice(0 , pico); // get the edited section for ajax submission
$('#results').text(dats+' '+newDats);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="add">
<div class="container">
<form method="post" id='bob'>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><strong>Step 1: Instance Select</strong></h3>
</div>
<div id="selector">
<div class="panel-body">
<div class="col-md-6">
<select class="form-control box1" name="box1">
<option>a</option>
<option>b</option>
<option>c</option>
</select>
<input name="box1hash" class="box1hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box2" name="box2"></select>
<input name="box2hash" class="box2hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box3" name="box3"></select>
<input name="box3hash" class="box3hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box4" name="box4"></select>
<input name="box4hash" class="box4hash" type="hidden" />
</div>
<div class="col-md-6">
<select class="form-control box5" name="box5"></select>
<input name="box5hash" class="box5hash" type="hidden" />
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><strong>Step 2: Event Details</strong></h3>
</div>
<div class="panel-body">
<input name="event_name" type="text" class="form-control" />
</div>
</div>
<input type="submit"/>
</form>
</div>
<p><tt id="results"></tt></p>
I try to get data from ajax pass from front page to my controller but i got only string to my controller so how can i separate it one by one.
this is form:
<form class="form-horizontal form" action="<?php print base_url() ?>insertCategory" method="post" enctype='multipart/form-data'>
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">name</label>
<div class="col-sm-10">
<input type="text" name='name' required class="form-control" id="inputEmail3" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">name</label>
<div class="col-sm-10">
<input type="text" name='test' required class="form-control" id="inputEmail3" placeholder="Name">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Category Detail</label>
<div class="col-sm-10">
<textarea id="txtEditor" ></textarea>
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Image</label>
<div class="col-sm-10">
<input type="file" name="img" required class="form-control" >
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<button type="submit" class="btn btn-info pull-right submit">Submit</button>
</div>
<!-- /.box-footer -->
</form>
this my code ajax:
$(".submit").click(function (e) {
e.preventDefault();
var data = $('.form').serialize();
var val = $("#txtEditor").Editor("getText");
var page = "<?php echo $page ?>";
var url = "<?php echo base_url(); ?>insertCategory"
$.ajax({
type: "POST",
url: url,
data:{
"data" : data
},
success: function (data) {
console.log(data);
}
});
return false;
});
and this is my controller:
public function insertCategory()
{
if($this->session->userdata('logged_in')==1){
$data = $this->input->post('data');
var_dump($data);
and this my data respond by ajax:
C:\....\AdminController.php:308:string 'name=dsfdsfs&test=dfsdf' (length=23)
Use this instead,
var_dump($_POST);
it is posted as same as post work
For reference, use this code below.
type: "POST",
url: url,
data: $('.form').serialize(),
success: function (data) {
console.log(data);
}
Use
data: $('.form').serialize()
instead of
var data = $('.form').serialize();
data:{
"data" : data
},
At the controller the contents of your form will be available in the $_POST array. So the "name" field on your form can be accessed with $_POST['name'].
That said, you will be better off using CodeIgniter's methods to retrieve this data.
For example:
$name = $this->input->post('name');
If, for learning purposes, you wanted to see everything "posted" to the controller this will do the trick.
public function insertCategory()
{
if($this->session->userdata('logged_in')==1)
{
$posted = $this->input->post(); //get all $_POST items
var_dump($posted);
}
}
use parse_str(); that can return array after parse string
public function insertCategory()
{
if($this->session->userdata('logged_in')==1){
$data = $this->input->post('data');
parse_str($data, $parseData);
var_dump($parseData);