Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

submitting html form via jquery ajax() and serialize()

I want to submit this form via jquery ajax, this is what I have made and it is not working. i.e. Form is submitting with page refresh and I am not seeing the response i.e. printing array on the same page.

HTML

  <link rel='stylesheet' type='text/css' href='css/pepper-grinder/jquery-ui-1.10.4.custom.css' />
    <script  type='text/javascript' src='js/jquery-1.10.2.js' ></script>
    <script  type='text/javascript' src='js/jquery-ui-1.10.4.custom.min.js' ></script>
        <form  id="form1" method="get" action="submit.php   ">
            <label>Name of Organization</label>
            <input type="text" name="OrgName" id="OrgName" class="textfield">
            <label>Address of Organization</label>
            <input type="text" name="OrgAddress" id="OrgAddress" class="textfield">
            <input type="submit" value="Register Organization">
        </form>
        <div id="response">ads</div>

    <script>
    $document.ready(function(){
        $("#form1").click((function(event){
            event.preventDefault();

            $.ajax({
                    url:'submit.php',
                    type:'GET',
                    data:$(this).serialize(),
                    success:function(result){
                        $("#response").text(result);

                    }

            });
        });
    });
    </script>

PHP (submit.php)

<?php
print_r($_GET);
?>
like image 811
Muhammad Arslan Jamshaid Avatar asked Apr 09 '14 11:04

Muhammad Arslan Jamshaid


People also ask

What is serialize () in AJAX?

The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

Can we submit form using AJAX?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.

What is data $(' form ') serialize ()?

The .serialize() method creates a text string in standard URL-encoded notation. It can act on a jQuery object that has selected individual form controls, such as <input> , <textarea> , and <select> : $( "input, textarea, select" ).serialize();

What is the difference between AJAX and form submit?

A standard form submit sends a new HTTP request (POST or GET) and loads the new page in the browser. In Ajax, the data is sent to the server (POST or GET) in the background, without affecting the page at all, and the response is then received by javascript in the background, again without affecting the page at all.


2 Answers

Use this - there have been a few syntax errors and the event has to be submit

 $(function(){
        $("#form1").submit(function(event){
            event.preventDefault();

            $.ajax({
                    url:'submit.php',
                    type:'GET',
                    data:$(this).serialize(),
                    success:function(result){
                        $("#response").text(result);

                    }

            });
        });
    });
like image 111
Dimag Kharab Avatar answered Sep 25 '22 00:09

Dimag Kharab


 $document.ready(function(){

        $("#form1 input[type='submit']").click(function(event){
            event.preventDefault();

            $.ajax({
                    url:'submit.php',
                    type:'GET',
                    data:$(this).serialize(),
                    success:function(result){
                        $("#response").text(result);

                    }

            });
        });
    });

I your code $("#form1").click(.... does not have any meaning here... You want the event handler when you press the submit button. So I think If you take appropriate the selector then It might work perfectly

like image 40
Tuhin Avatar answered Sep 23 '22 00:09

Tuhin