Submit is not working form me, please can anyone can solve this ?
$(function(){
$('#postImg').click(function() {
var form = $("<form/>", {
action: "checkout.php",
method: "POST"
});
form.on("submit");
$.each(['tprice', 'tevents'], function(i, v) {
$("<input/>", {
type: "hidden",
name: v,
value: $("#" + v).text()
}).appendTo(form);
});
form.submit();
return false;
});
});
What you are doing here is trying to make an HTTP POST request in a really roundabout way (creating a form just for the purpose, populating it, posting, and then discarding).
It would be much easier and less confusing to directly use jQuery.post instead.
For example:
var parameters = {
'tprice': $("#tprice").text(),
'tevents': $("#tevents").text()
};
$.post("checkout.php", parameters);
Why are you trying to post a form that's not being bound into the DOM ? Maybe
$.post("checkout.php", { paramName: "value", paramName2: "value2" } );
is what you need ?
Related
I am desperately trying to submit multiple POST variables via AJAX, but just cant get manage to get the formatting right... Problem is that I have both a hardcoded / written action=problem_lookup variable and a dynamic field input as $(this).val and just cant manage to get both into one data string...
this works well:
data: 'problem=' + $(this).val(),
This does not:
data: { action: 'problem_lookup' , problem: $("problem").val() },
data: { action: 'problem_lookup' , problem: $(this).val() },
data: { action: problem_lookup, problem: $(this).val() },
I tried numerous formats from other threads and looked at the official jquery manual, but cant seem to get this figured out. Any help is appreciated.
EDIT:
full script below, tried the solutions posted so far but no success. $("problem") is a <select> field (with Select2 running) hence shouldnt cause me so much frustration, especially since the original approach with data: 'problem=' + $(this).val(), works fine.
$(function () {
$('#problem').change(function () { // on change in field "problem"
var data = {
action: 'problem_lookup',
problem: $("problem").val()
}
$.ajax({ // launch AJAX connection
type: 'POST', // via protocol POST
url: 'ajax.php',
//data: 'problem=' + $(this).val(), // send $_POST string
//data:"{'action':'"+action+"','problem':'"+$(this).val()+"'}",
//data:"{'action':'problem_lookup','problem':'"+$(this).val()+"'}",
//data: { action: 'problem_lookup' , problem: $("problem").val() },
//data : data_string,
data: $.param(data),
dataType: 'json', // encode with JSON
success: function (data)
{
// do something
},
});
});
});
An issue is in the
$("problem")
Jquery call.
If.problem is a css class try with
$(".problem")
if problem is a css id try with
$("#problem")
For posting arrays of object you can build data as an object containing arrays, changing a little bit your structure. Something like this
Var obj={};
obj.postData=[];
obj.postData.push(/*your first object here*/);
...
obj.postData.push(/*your n-th object here*/);
$.ajax({
.....
data:obj;
......
});
Try the FormData() FormData.
var data = new FormData();
data.append('action', value);
...
You need to specify your data variable first like this:
var data = {
action: 'problem_lookup',
problem: $("problem").val()
}
In AJAX serialize your data using $.param,
data: $.param(data),
Note: Twice check if $("problem").val() is correct. If problem is a class, you need to specify like this $(".problem").val() or if it is ID, $("#problem").val()
I have a JavaScript variable, and I want to send via POST to a PHP page and I want to display the new page.
I tried this solution, but it seems not work. I have checked within Wireshark, and the variable (key and value) are sent correctly, but the page, doesn't show anything.
$(document).ready(function() {
$("#tbID tr").click(function() {
$(this).addClass('selected').siblings().removeClass('selected');
var value = $(this).find('td:nth-child(2)').html();
});
});
function send(value) {
$.post("newPhp.php", {
key : value
}).done(function(data) {
location.href="newPhp.php";
});
}
The newPhp.php:`
if(!empty($_POST["key"])){
$value = $_POST['key'];`
//something with $value
}
So what is the problem with this code ?
Are there any simpler solution ?
function send(value) {
$.post( "newPhp.php",
{key:value},
function( data ) {
location.href="newPhp.php";
}
);
}
This question already has answers here:
Creating 'form' in javascript without html form
(4 answers)
Closed 9 years ago.
Is it possible to create an HTML form with no input selectors in page and create these "input" objects via javascript and then submit it? How can I create these "items"?
Or do I have to always create any "item" to submit previously in the HTML code?
So, can I avoid to create annoying hidden inputs in order to send javascript variables? I am not really able to find any tutorial or examples about it...
Thank you very much for your help.
like this, using jquery
$('<form>', {
"id": "getInvoiceImage",
"html": '<input type="text" id="componentInvoicId" name="componentInvoicId" value="' + componentInvoiceId + '" />',
"action": window.siteRoot + 'ComponentInvoice/GetInvoiceImage/'
}).appendTo(document.body).submit();
the MUCH better way:
$.post(url, { value1: "foo", value2: "bar" } );
and if you insist on doing it with a form, here's the generalized function so you can just pass it the json data
var postRequest = function (uri, data) {
data = data || {};
var form = $('<form method="post" class="js:hidden">').attr('action', uri);
$.each(data, function(name, value) {
var input = $('<input type="hidden">')
.attr('name', name)
.attr('value', value);
form.append(input);
});
$('body').append(form);
form.submit();
};
Yes, this can be done, even completely without using a <form>.
Check out jQuery as a starter (http://api.jquery.com). For example, if you want to know the clients device width, you could add this code:
$(document).ready(function() {
$.ajax( {
url: http://yourdomain.com/yourscript.php,
dataType: 'json',
type: 'POST',
data: {
w: $(window).width,
h: $(window).height
}
});
});
This would submit the parameters w and h to your application, containing window width and height. Just as an example.
I'm planning on saving 2 forms but the 1st form is where I get the Foreign key for the Second form.
This is my Attempt to save this Using Javascript:
$("#btnSave").click(function (e) {
e.preventDefault();
$('#workForm').submit();
$('#contForm').submit();
});
But it errors on Contact Form Submit in the control because the ID of Worker Form is still null while saving the contact form that is its Foreign Key
How can I Handle This using Jquery and Javascript and Ajax?
I also Tried this method:
$("#btnSave").click(function (e) {
e.preventDefault();
if (Id != 0) {
$('#workForm').submit();
$('#contForm').submit();
} else {
$('#workForm').submit(); }
});
But it only goes at Else because the ID is null
I hope someone can help me here
Worker Address is the WorkForm Worker Contact is the ContForm
I want to save them both when they populate all the textbox
You will have to use ajax because
$('#contForm').submit();
will never be run as the page will be unloaded by the first call:
$('#workForm').submit();
You can submit the first form using ajaxSubmit() and on its success, submit the second form.
$("#btnSave").click(function() {
$('#workForm').ajaxSubmit({
forceSync: true,
error: function(errorDetails) {
//Error Handling
},
success: function(successData) {
$('#contForm').submit();
}
});
});
You need to post both forms using .ajax. Serialize the data from both forms and then post to your server script.
$("#btnSave").click(function (e) {
e.preventDefault();
//Serialize the form data
var formOne$ = $('#workForm').serialize();
var formTwo$ = $('#contForm').serialize();
$.ajax( { url : "YOURURL", type: 'post', data : { formOne : formOne$ , formTwo : formTwo$ },
success : function( responseText ){
//Forms were submitted
},
error : function( e ){
alert( ' Error : ' + e.statusText );
}
});
});
i want to set some header information and cookies with form submission using $('#formid').submit(); javascript code. Most of the site says that setRequestHeader is only working on ajax form submission. but i can not use the ajax method to submit the form.
my javascript code is
$(document).ready(function(){
$("#leftNav a").click(function(event){
event.preventDefault();
href = $(this).attr('href');
$("#dynamicform :input").remove();
var querystringArray = href.split('?')[1].split('&');
$("#dynamicform").attr("action", href.split('?')[0]);
$.each(querystringArray, function(index, value) {
var elementArray = value.split('=');
if(elementArray[0]=='methodtype') { $("#dynamicform").attr("method", elementArray[1]);}
$('<input>').attr({
type: 'hidden',
value: elementArray[1],
name: elementArray[0]
}).appendTo('#dynamicform');
});
usagelogsajax(href.split('?')[0],'ncrtester');
xhr.setRequestHeader('X-Test', 'three');xhr.setRequestHeader('X-shashi', 'three');
$('#dynamicform').submit();
});
});
function usagelogsajax(url, user) {
$.get("usagelogs.php?url="+url+"&username="+user,function(responseTxt,statusTxt,xhr){
if(statusTxt=="success");
//alert("Usagelogs created successfully!");
if(statusTxt=="error");
//alert("Error: "+xhr.status+": "+xhr.statusText);
});
}
And my page have link like
http://sitename.com/autologinSL3?readform=&userid=userid&methodtype=get&pwd=passowrd
How can i achieve my requirements.
Thanks
I think what you are looking for is the target-attribute of the form (see mdn).
If you set target="_blank" the form will be submitted and the result will be shown in a new window.