Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submit form without redirecting

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" />
like image 485
user3182036 Avatar asked Dec 16 '22 02:12

user3182036


1 Answers

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>
like image 136
Susheel Singh Avatar answered Dec 28 '22 11:12

Susheel Singh