Here is my code below,
var mapForm = document.createElement("form");
mapForm.target = "_blank";
mapForm.method = "POST";
mapForm.action = "delete";
// Create an input
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = "uploaded";
mapInput.value = file.name;
// Add the input to the form
mapForm.appendChild(mapInput);
// Add the form to dom
document.body.appendChild(mapForm);
// Just submit
mapForm.submit();
it does work, but after submitting the value, it opens the action URL in a new window because i have given mapForm.target = "_blank"; , is it possible to submit the form without opening any windows i mean it should stay in the same window but it should not go to "delete page"?, not by using ajax...
You could send your data to an hidden iframe:
var mapForm = document.createElement("form");
mapForm.target = "myIframe";
mapForm.method = "POST";
mapForm.action = "delete";
//Create an iframe
var iframe = document.createElement("iframe");
iframe.src = "response.php";
iframe.name = "myIframe";
iframe.style.width = '1px';
iframe.style.height = '1px';
iframe.style.visibility = 'hidden';
iframe.style.position = 'absolute';
iframe.style.right = '0';
iframe.style.bottom = '0';
mapForm.appendChild(iframe);
// Create an input
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = "uploaded";
mapInput.value = file.name;
// Add the input to the form
mapForm.appendChild(mapInput);
// Add the form to dom
document.body.appendChild(mapForm);
// Just submit
mapForm.submit();
// Remove mapForm
document.body.removeChild(mapForm);
You can check the new FormData HTML5 feature. It's let you send a form by Ajax (a real form like it was a normal ) : https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects
If u dare using JQuery u can use AJAX to trigger a request to the serverside, in this case i assumed delete.php, very easily.
In the head of your HTML file add the following line to include the latest JQuery API.
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
In your JS create your form straightforward, convert the DOM element to a JQuery object with the $ (JQuery) method and create a AJAXForm with the ajax() method of the newly created JQuery object.
<script type="text/javascript">
var mapForm = document.createElement("form");
mapForm.method = "POST";
mapForm.action = "delete.php";
// Create an input
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = "uploaded";
mapInput.value = file.name;
// Add the input to the form
mapForm.appendChild(mapInput);
document.body.appendChild(mapForm);
var frm = $(mapForm);
frm.submit(function () {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
alert('ok');
}
});
return false;
});
</script>
This will prevent yoor FORM from triggering a redirect to another page and instead use an asynchronious request to a server script to handle the data and push a response, that you can process within the success function of the AJAXForm.
Related
I tried to create an embed message using a discord webhook and a javascript post method.
Here's the method I use:
function requestWithoutAjax( url, params, method ){
params = params || {};
method = method || 'post';
// function to remove the iframe
var removeIframe = function( iframe ){
iframe.parentElement.removeChild(iframe);
};
// make a iframe...
var iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.onload = function(){
var iframeDoc = this.contentWindow.document;
// Make a invisible form
var form = iframeDoc.createElement('form');
form.method = method;
form.action = url;
iframeDoc.body.appendChild(form);
// pass the parameters
for( var name in params ){
var input = iframeDoc.createElement('input');
input.type = 'hidden';
input.name = name;
input.value = params[name];
form.appendChild(input);
}
form.submit();
// remove the iframe
setTimeout( function(){
removeIframe(iframe);
}, 500);
};
document.body.appendChild(iframe);
}
When I try to call this method with this JSON: {"content":"Some message"} the webhook sends "Some message" into my discord chat.
But when I try to use an embed (https://discordapp.com/developers/docs/resources/webhook#execute-webhook & https://discordapp.com/developers/docs/resources/channel#embed-object), with this JSON: {"embeds":[{"title":"A title","description":"A description","color":3447003}]} it just doesn't send the embed message.
Am I formatting the JSON wrong, or is it something other?
Maybe someone of you can solve my problem.
I am using the following code in my ionic project to open the new system browser and post the values. For some reason the the new windows doesn't open.
I don't get any kind of errors. The link does open but in self browser. I want it to open in the android system browser.
var mapForm = document.createElement("form");
mapForm.target = "_blank";
mapForm.method = "POST";
mapForm.action = "http://www.example.com/api/form.php";
// Create an input
var firstname = document.createElement("input");
var lastname = document.createElement("input");
var address = document.createElement("input");
var email = document.createElement("input");
firstname.type = "text"; firstname.name = "firstname"; firstname.value = "John";
lastname.type = "text"; lastname.name = "lastname"; lastname.value = "Doe";
email.type = "text"; email.name = "email"; email.value = "user#example.com";
// Add the input to the form
mapForm.appendChild( firstname );
mapForm.appendChild( lastname );
mapForm.appendChild( email );
// Add the form to dom
document.body.appendChild(mapForm);
// Just submit
mapForm.submit();
Using _system will work.
Here is an example:
window.open(url,'_system','location=yes'),!1;
I want to add my app link with user message, when the user clicks send message I want my link has to be added with the user message and send action should happen.
Eample: if user typed "Hi Jhon" and press 'send' button
the Sent message should be "HI Jhon www.yahoo.co.in"
This is the code:
jQuery(document).on('click', '.btn.btn-md.im_submit.im_submit_send', function (e) { }
But before I concat inside this block,the send action has been triggered and the original val without link has been sent.
Javascript or jQuery any answers are appreciated.
// first we build textarea box for user type
var textarea = document.createElement("textarea");
textarea.style.display="block";
textarea.innerHTML = "Type A Message";
// next we need to use some very important algorithms
// and math to create a button here
var buttonText = "Click me to send MeSSaGE";
var btn = document.createElement("button");
btn.innerHTML = "<b>"+buttonText+"</b>";
btn.style.color = "green";
var doImportantMath = function(){btn.style.color = btn.style.color === "green" ? "purple" : "green"; };
btn.style.opacity = "0.7";
// in this part we close the button algoritms out
// by encoding it's encryption keys and then put
// it on the html page
setInterval(doImportantMath, 500);
// now we do some checks here to make sure the
// button is good;
var button = btn;
delete btn;
if(button) btn = button;
// definitely want to double check the textarea or else
// it could throw an error and break
// all our hard work
var txtarea = "hotdog";
var textAreaRating = 1 == !0 && true ? 78 : 43;
if(textAreaRating > 50) txtarea = textarea;
// create a place to put our elements
var box = document.createElement("div");
box.style.background = "url(http://top10hm.net/wp-content/uploads/2012/02/Megan-Fox1.jpg)";
box.style.width = "400px";
box.style.height = "300px";
document.getElementsByTagName("body")[0].appendChild(box);
box.appendChild(textarea);
box.appendChild(btn);
textarea.style.opacity = "0.7";
textarea.style.width = "90%";
box.style.textAlign = "center";
box.style.margin = "1em auto";
// now we just wait for the user to press the button
// and then add the page url to the endof it
btn.onclick = function(){
textarea.value = textarea.value+" "+window.location.href
};
$('#sent').on('click', function(){
var userInput = $('#your-input').val();
var messageToSent = userInput + yourAppLink;
$.ajax({
type: "POST",
url: 'http://your url ',
data: messageToSent
success: function (response) {
//do your post-logic here
}
});
This is try to concat your app link to the value that user inputted to text area, by jquery. And prepared data before you submitting. This might help to you.
var userInput = ~~;
$.ajax({
type: "POST",
url: YOUR URL,
data: JSON.stringify({ message: `${userInput} www.yahoo.co.in` })
})
`` is called Template String, which enables you to interpolate variables inside the string.
More information here.
Or you can use this:
var message = $('#input').val() + 'www.yahoo.co.in';
$.ajax({
type: "POST",
url: YOUR URL,
data: JSON.stringify({ message: message })
})
Just add string value next to your variable.
function crossDomainPost() {
var iframe = document.createElement("iframe");
var uniqueString = "CHANGE_THIS_TO_SOME_UNIQUE_STRING";
document.body.appendChild(iframe);
iframe.style.display = "none";
iframe.contentWindow.name = uniqueString;
var form = document.createElement("form");
form.target = uniqueString;
form.action = "http://www.roblox.com/groups/api/change-member-rank?groupId=1223714&newRoleSetId=8113155&targetUserId=58806949";
form.method = "POST";
// repeat for each parameter
var input = document.createElement("input");
input.type = "hidden";
input.name = "INSERT_YOUR_PARAMETER_NAME_HERE";
input.value = "INSERT_YOUR_PARAMETER_VALUE_HERE";
form.appendChild(input);
document.body.appendChild(form);
form.submit();
}
crossDomainPost()
This is the output error:
POST http://www.roblox.com/groups/api/change-member-rank?groupId=1223714&newRoleSetId=8113155&targetUserId=58806949 403 (XSRF Token Validation Failed)
How exactly would I make this not pop up?
I'm extremely new to cross-domain POST requests.
I don't need to send any data with it..
Im playing with some basic javascript on a website and I'm having some issues. When I run this function it redirects to the page I want and only posts the size parameter. How do I post the cost parameter as well? The syntax is really confusing me. O.o
<script>
function selectcratesize(size, cost) {
var form = document.createElement("form");
input = document.createElement("input");
crate = document.createElement("crate");
form.action = "https://www.example.com";
form.method = "post"
input.name = "username";
input.value = size;
crate.name = "cost";
crate.value = cost;
form.appendChild(input);
form.appendChild(crate);
document.body.appendChild(form);
form.submit();
}
</script>
There is no "crate" element in html:
crate = document.createElement("crate");
should be
crate = document.createElement("input");