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" />
MrDanA
  • 11,489
  • 2
  • 36
  • 47
user3182036
  • 49
  • 1
  • 1
  • 4
  • 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... – Mathew Jan 14 '14 at 13:59
  • ive tried many things even the code below, but its not working – user3182036 Jan 14 '14 at 14:30
  • 1
    Possible duplicate of [How to submit html form without redirection?](https://stackoverflow.com/questions/25983603/how-to-submit-html-form-without-redirection) – StefansArya Aug 28 '17 at 15:30

4 Answers4

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>
Susheel Singh
  • 3,824
  • 5
  • 31
  • 66
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">
ohjeeez
  • 559
  • 4
  • 6
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()
Prakash R
  • 409
  • 2
  • 14
0

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

header('Location: yourwebsite.com');

Hopefully this works for you