Form submission using jQuery ajax - javascript

Form is not submit using ajax.form submit on click li. Give me some solution
My js code is here
$(document).ready(function(){
$('#sortable li').click(function() {
$("#frmgallery").submit(function(event) {
event.preventDefault();
var formdata = $(this).serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
});
});
});
HTML is here
<form method="post" enctype="multipart/form-data" id="frmgallery" name="gallery" action="<?php get_template_directory();?>admin.php?page=gallery/gallery.php">
<ul id="sortable">
Query
<li class="ui-state-default" name='liindex' id="<?php echo $row['glryRecordId'];?>" >
<span style="display:none"><?php echo $row['glryRecordId'];?></span>
<img class='thumbnail' alt='' src='<?php echo get_site_url();?>/wp-content/themes/townsley/upload/<?php echo $row['glryName']; ?>' width='80' height='60'
style="border: 1px solid #D3D3D3;padding:2px;"/><input type="hidden" value="<?php echo $row['glryRecordId'];?>" name="recordId[]" />
Remove
</li>
</ul>
</form>
Please help me
Thanks
ajax jquery javascript

You should provide your HTML too in your question, but as far as i can see, you have event in event callbacks with actually nothing to initiate the submit event. So basically you should consider something like this :
$(document).ready(function(){
$('#sortable li').click(function() {
event.preventDefault();
var formdata = $("#frmgallery").serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
});
});
});

You can use the ajaxForm/ajaxSubmit functions from Ajax Form Plugin or the jQuery serialize function.
Example:
$("#frmgallery").ajaxForm({url: 'gallery.php', type: 'post'})
or
$("#frmgallery").ajaxSubmit({url: 'gallery.php', type: 'post'})
ajaxForm will send when the submit button is pressed. ajaxSubmit sends immediately.

Have you tried return false at the end of your submit function?
$("#frmgallery").submit(function(e) {
e.preventDefault();
var formdata = $(this).serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
error: function(){alert('error');}
});
return false;
});
$('#sortable li').click(function() {
$("#frmgallery").submit();
});
Also post what you get from $.ajaxcall

$("#frmgallery").ajaxForm({url: 'gallery.php', type: 'post'})
Have you tried return false at the end of your submit function?
$("#frmgallery").submit(function(e) {
e.preventDefault();
var formdata = $(this).serialize();
alert(formdata);
$.ajax({
type: "POST",
url: "gallery.php",
data: formdata,
success: function(){alert('success');}
error: function(){alert('error');}
});
return false;
});
$('#sortable li').click(function() {
$("#frmgallery").submit();
});

Related

Submit an ajax-created form via ajax

I'm having trouble getting an ajax-loaded form (#ajaxLoadedForm) to submit via ajax. The formData object gathers no data. I figure I've got to attach an event-handler to the form so the DOM recognizes it, but I can't figure out how.
A couple of notes: I'm bypassing the 'submit' method and using a button (#button), so I can't attach the handler to that. The form itself is a sibling to #button, not a child.
<form id="ajaxLoadedForm" enctype="multipart/form-data" action="destination.php" method="POST">
<input type="hidden" name="state" value="1" />
<label for="fullname">Your Full Name</label>
<input type="text" id="name" autocapitalize="off" autocorrect="off" name="fullname" placeholder="your name" value="" />
</form>
<div id="button">Submit me!</div>
$('#button').click(function(){
var uploadData = new FormData($("#ajaxLoadedForm")[0]);
jQuery.ajax({
url: 'destination.php',
data: uploadData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
data = JSON.parse(data);
if (data['pass'] == false) {
console.log('fail');
} else {
console.log('success');
}
}
});
});
Try using the submit handler on the form itself
$('#ajaxLoadedForm').submit(function(e){
e.preventDefault();
var uploadData = new FormData(this);
});
Then make your button for submit a submit type
<button type='submit'>Submit</button>
In your php side, test the data coming by doing this:
print_r($_POST);
you can use serialize function for sending form data . Like below
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script >
$('#button').click(function(){
var uploadData = $('#ajaxLoadedForm').serialize();
jQuery.ajax({
url: 'destination.php',
data: uploadData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
data = JSON.parse(data);
if (data['pass'] == false) {
console.log('fail');
} else {
console.log('success');
}
}
});
});
</script>
Try below code..
$('#button').click(function(){
var uploadData = new FormData();
uploadData.append('fullName',$('#fullName').val());
jQuery.ajax({
url: 'destination.php',
data: uploadData,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
}
});
});
And try to access full name in php

Form submit on button click in a multistep form

I have a form which has a save and continue button. So I want the third click to submit the data. I have written the following code for it. But the submit portion doesn't work.
$(document).ready(function(){
var datastring="";
var d1= $( "#Submit3" ).mousedown(function() {
alert("Event occurred");
datastring = $("#reg_form").serialize();
console.log(datastring);
});
function submit()
{
$('form').submit(function(){
$.ajax({
type: "POST",
url: "reg.php",
data: datastring,
success: function(data) {
console.log('Data sent');
}
});
});
}
$.when(d1).done(submit());
});
Try the following code
$(document).ready(function(){
$( "#Submit3" ).on("click", function() {
alert("Event occurred");
dataString = $("#reg_form").serialize();
console.log(dataString);
submit(dataString);
});
function submit(dataString)
{
$.ajax({
type: "POST",
url: "reg.php",
data: dataString,
success: function(data) {
console.log('Data send');
}
});
}
});
You need to make the button type of the third button to "submit" and the type of other two buttons should be "button" just like this.
<button type="button">Save</button>
<button type="button">Continue</button>
<button type="submit">Submit</button>
So that when you click on the other two buttons form will not be submitted. Only on the click of third button, your form will be submitted. Here is the script to submit the form
$("form").submit(function(e){
e.preventDefault();
var formData=$("form").serialize();
console.log(formData);
$.ajax({
type: "POST",
url: "register.php",
data: formData,
success: function(response) {
console.log('Data send');
}
});
});

Using AJAX to submit a dynamically filled form to two locations

I am submitting a form that will need to go to 2 different locations. The form is hidden and auto filled with the jquery .val method. How would I write the ajax to submit this in 2 different locations? Am I able to do this if I don't have access to my server side settings?
Here is the Form I am submitting
<form action="x" method="post" class="form-inline">
<input type="hidden" name="players_name" class="form-control" id="playersFullName_submit">
<input type="hidden" name="players_email"class="form-control" id="playersEmail_submit">
<input type="hidden" name="players_score" class="form-control" id="playersScore_submit">
<input type="hidden" name="redirect_url" class="form-control" id="redirectURL_submit" value="x">
<input id="submit endGame" type="submit" class="btn btn-default" value="Submit Your Score">
</form>
Here is my jquery filling it out
$(function () { // fill form fields
$('#playersFullName_submit').val(playersFullName);
$('#p_name').append(playersFullName);
$('#playersEmail_submit').val(playersEmail);
$('#dateSubmitted').val(submitDate);
});
Here is how I think I need to set it up
$.ajax({
type: "POST",
url : "url1.com",
data: {},
success: function(msg){
// get response here
}
});
$.ajax({
type: "POST",
url : "url2.com",
data: {},
success: function(msg){
// get response here
}
});
Would I need to enter anything into data, or will it pull it from my <form>?
You need to get the form data and inject it into the AJAX calls in the data property. You can do this with JQuery's serialize() method:
// Get your reference to the form:
var theForm = document.getElementsByTagName("form")[0];
$.ajax({
type: "POST",
url : "url1.com",
data: $(theForm).serialize(), // <-- Inject serialized form data
success: function(msg){
// get response here
}
});
$.ajax({
type: "POST",
url : "url2.com",
data: $(theForm).serialize(), // <-- Inject serialized form data
success: function(msg){
// get response here
}
});
b/c you are submitting ajax not form, you need to prevent form form action submitting, declare you server side route in ajax instead.
$('#submit_btn').on('click', function(e) {
e.preventDefault(); // you need this so the form don't submit
$.ajax({
type: "POST",
url : "url1.com",
data: {},
success: function(msg1){
var msg1;
}
});
$.ajax({
type: "POST",
url : "url2.com",
data: {},
success: function(msg2){
var msg2;
}
});
});
$.ajax({
type: "POST",
url : "url1.com",
data: $('.form-inline').serialize(),
success: function(msg){
// get response here
}
});
$.ajax({
type: "POST",
url : "url2.com",
data: $('.form-inline').serialize(),
success: function(msg){
// get response here
}
});
In case you have to find yourself having to send to many urls...
var theForm = document.getElementsByTagName("form")[0],
urls = ['url1.com','url2.com', ....];
$.each(urls, function(index,value){
$.ajax({
type: "POST",
url : value,
data: $(theForm).serialize(),
success: function(msg){
// get response here
}
});
});

Send JavaScript array to database using submit

I have looked at some other question regarding sending javascript arrays to php so save the data in database.
<form action="insert.php" method="POST">
<input type='submit' id="save" value='Spara' onclick='sendFunc()'>
<script>
var rad = [0,0,0,0,0,0,0,0,0,0,0,0,0];
function sendFunc() {
$.ajax({
url: "/insert.php",
type: "POST",
data: {myArray : rad}
});
}
</script>
</form>
}
and in insert.php
<?php
$array = $_POST['myArray'];
echo $array;
?>
i did expect to see my array rad on the insert page.
What did i do wrong?
Try to add preventDefault and close your brackets properly:
function sendFunc(e) {
e.preventDefault();
$.ajax({
url: "/insert.php",
type: "POST",
data: {myArray : rad}
});
}
You need to prevent from refreshing page by return false on your click function ...
<form action="insert.php" method="POST">
<input type='submit' id="save" value='Spara' onclick='return sendFunc();'>
<script>
var rad = [0,0,0,0,0,0,0,0,0,0,0,0,0];
function sendFunc() {
$.ajax({
url: "/insert.php",
type: "POST",
data: {myArray : rad}
});
return false;
}
</script>
</form>
required jquery.js
$(document).ready(function(){
$("#save").on("click",function(e){
e.preventDefault();
$.ajax({
url: "/insert.php",
type: "POST",
data: {myArray : rad}
});
}).done(function(data){
$("body").html(data);
});
})
or you can edit your function()
function sendFunc() {
$.ajax({
url: "/insert.php",
type: "POST",
data: {myArray : rad}
}).done(function(data){
$("body").html(data);
});
return false;
}

File upload using Jquery ajax without form

Here is mycode
function addPackage(elem)
{
var dataimg = new FormData();
dataimg.append('', $("#browseimg"+elem).prop('files')[0]);
var startdate=$("#from_date"+elem).val();
var enddate=$("#to_date"+elem).val();
$.ajax({
url: "addpackage/",
type:"post",
contentType:false,
data:{startdate:startdate,enddate:enddate,packageid:elem,img:dataimg},
success: function(data) {
}
});
}
I tried post method ajax to upload image and input field data without form. In ajax call it showing [object object]. How to post image with input field without form in jquery ajax?
You can do it like following. Hope this will help you.
function addPackage(elem)
{
var dataimg = new FormData();
dataimg.append('startdate', $("#from_date"+elem).val());
dataimg.append('enddate', $("#to_date"+elem).val());
dataimg.append('packageid', elem);
dataimg.append('img', $("#browseimg"+elem)[0].files[0]);
$.ajax({
url: "addpackage/",
type:"post",
cache : false,
contentType : false,
processType : false,
data: dataimg,
success: function(data) {
}
});
}
You can try this:
Your JS Code:
<script type="text/javascript">
var data = new FormData(document.getElementById("yourFormID")); //your form ID
var url = $("#yourFormID").attr("action"); // action that you mention in form action.
$.ajax({
type: "POST",
url: url,
data: data,
enctype: 'multipart/form-data',
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
dataType: "json",
success: function(response)
{
// some code after succes from php
},
beforeSend: function()
{
// some code before request send if required like LOADING....
}
});
</script>
Dummy HTML:
<form method="post" action="addpackage/" id="yourFormID">
<input type="text" name="firstvalue" value="some value">
<input type="text" name="secondvalue" value="some value">
<input type="file" name="imagevalue">
</form>
in addpackage php file:
print_r($_POST);
print_r($_FILES);

Categories