4

I want to submit a form to action page which is a different site however i don't want to be directed to the action page. i've tried everything but its not working for me, i've heard about ajax does the job can someone give me examples

What I want:

  • i want to submit a form but not be redirected
  • submit the form as i'm being redirected to action page i get redirected to my original page.

My code:

<form id="user-login-form" accept-charset="UTF-8" action="http://xxxxxn" method="post"> <div> <div class="user_login_block&amp;op=Log+in"><label for="edit-name"><span class="form-required" title="This field is required.">*</span></label> <input type="hidden" /> <div> <div class="user_login_block&amp;op=Log+in"><label for="edit-name"><span class="form-required" title="This field is required.">*</span></label> <input type="hidden" /> <input class="form-text required" id="edit-name" type="hidden" maxlength="60" name="name" size="15" value="xxxxxx2" /></div> <div class="form-item form-type-password form-item-pass"><label for="edit-pass"></label> <input class="form-text required" id="edit-pass" type="hidden" maxlength="60" name="pass" size="15" value="9sQ&amp;ampzW#7PKhpqbzcCFz9jvY" /> <input type="hidden" /></div> </div> <input type="hidden" name="form_build_id" value="form-odWc3MFMsKIL_5vCQtPmiv0AVf0tFwBu7eP2-8" /> <input type="hidden" name="form_id" value="user_login_block" /> <div class="form-actions form-wrapper" id="edit-actions"><input class="form-submit" id="edit-submit" type="submit" name="op" value="submit" /> 
3
  • You need to make an attempt in solving the problem before coming here to ask for help. AJAX is indeed the right way; what have you tried whilst trying to implement AJAX so far? There are a number of tutorials out there to help... Commented Jan 14, 2014 at 13:59
  • ive tried many things even the code below, but its not working Commented Jan 14, 2014 at 14:30
  • 2
    Possible duplicate of How to submit html form without redirection? Commented Aug 28, 2017 at 15:30

4 Answers 4

11

Check the below example:

check this link: http://susheel61.0fees.net/ajaxtest.php

NOTE: This is basic a example for ajax. You cant do it many other ways like using $.post, $.get etc. but $.ajax has many options like showing a loading image before the content loads.

html:

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>ajax example</title> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <div id="message"></div> <form id="myform" action="test.php"> <input type="text" name="test"/> <input type="submit" value="submit"/> </form> <div id="response"></div> <script> $(function() { $("#myform").on("submit", function(e) { e.preventDefault(); $.ajax({ url: $(this).attr("action"), type: 'POST', data: $(this).serialize(), beforeSend: function() { $("#message").html("sending..."); }, success: function(data) { $("#message").hide(); $("#response").html(data); } }); }); }); </script> </body> </html> 
Sign up to request clarification or add additional context in comments.

6 Comments

Where do i place it in the form tag or outside?
Do i have to place the above code you gave in my form or outside my form
I updated the answer please check. you keep the javascript in head also. but best practice is to put it at the end
I tried the above code but it's not working i'm being redirected to the action page
just copy and paste the above code and change the form code for yourself. it should work fine. you are doing some mistake and you must know debugging..
|
1

If nothing works, you could redirect to a hidden iframe.

<iframe id="iframe" name="my_iframe"></iframe> <form class="form" id="userform" target="my_iframe"> 

1 Comment

Useful alternative to AJAX for an invisible CSRF attack.
0

try this:

$('form').submit(function(event){ var serialObject = $(this).serialize(); //ex for post request $.post('path_to file',{ parameters:serialObject },function(data){ // do something after submitting }); }); event.preventDefault() 

Comments

0

In the php, you can redirect to back to the form by using headers:

header('Location: yourwebsite.com'); 

Hopefully this works for you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.