HTML Displaying the result from server on the same page












0















I am completely new to HTML and AJAX /JQuery. I want to display the result of the input as given in the form on the same page.
Below is the code snippet.



<div id = "test">
<form id="ajax-contact" method="post" action="/response/">
<div class="field">
<label for="name">your text here</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<button>Submit</button>
</div>
</form>
</div>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post(
{
type:'POST',
url:$(form).attr('action')
data: $(form).serialize()
},
function(response,status){
alert("Data: " + resp_final + "nStatus: " + status);
});
});
});




In the above snippet, resp_final is the value that is being retrieved from server where response is server route where necessary calculation / process is performed.
This works well. However, the result is being displayed on a separate page. I want to display the result on the same page.
Can anybody help please?










share|improve this question























  • Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

    – Marcelo Myara
    Nov 24 '18 at 16:32











  • Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

    – Marcelo Myara
    Nov 24 '18 at 16:33
















0















I am completely new to HTML and AJAX /JQuery. I want to display the result of the input as given in the form on the same page.
Below is the code snippet.



<div id = "test">
<form id="ajax-contact" method="post" action="/response/">
<div class="field">
<label for="name">your text here</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<button>Submit</button>
</div>
</form>
</div>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post(
{
type:'POST',
url:$(form).attr('action')
data: $(form).serialize()
},
function(response,status){
alert("Data: " + resp_final + "nStatus: " + status);
});
});
});




In the above snippet, resp_final is the value that is being retrieved from server where response is server route where necessary calculation / process is performed.
This works well. However, the result is being displayed on a separate page. I want to display the result on the same page.
Can anybody help please?










share|improve this question























  • Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

    – Marcelo Myara
    Nov 24 '18 at 16:32











  • Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

    – Marcelo Myara
    Nov 24 '18 at 16:33














0












0








0








I am completely new to HTML and AJAX /JQuery. I want to display the result of the input as given in the form on the same page.
Below is the code snippet.



<div id = "test">
<form id="ajax-contact" method="post" action="/response/">
<div class="field">
<label for="name">your text here</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<button>Submit</button>
</div>
</form>
</div>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post(
{
type:'POST',
url:$(form).attr('action')
data: $(form).serialize()
},
function(response,status){
alert("Data: " + resp_final + "nStatus: " + status);
});
});
});




In the above snippet, resp_final is the value that is being retrieved from server where response is server route where necessary calculation / process is performed.
This works well. However, the result is being displayed on a separate page. I want to display the result on the same page.
Can anybody help please?










share|improve this question














I am completely new to HTML and AJAX /JQuery. I want to display the result of the input as given in the form on the same page.
Below is the code snippet.



<div id = "test">
<form id="ajax-contact" method="post" action="/response/">
<div class="field">
<label for="name">your text here</label>
<input type="text" id="name" name="name" required>
</div>
<div class="field">
<button>Submit</button>
</div>
</form>
</div>

<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post(
{
type:'POST',
url:$(form).attr('action')
data: $(form).serialize()
},
function(response,status){
alert("Data: " + resp_final + "nStatus: " + status);
});
});
});




In the above snippet, resp_final is the value that is being retrieved from server where response is server route where necessary calculation / process is performed.
This works well. However, the result is being displayed on a separate page. I want to display the result on the same page.
Can anybody help please?







jquery html






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 16:27









pythondumbpythondumb

968




968













  • Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

    – Marcelo Myara
    Nov 24 '18 at 16:32











  • Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

    – Marcelo Myara
    Nov 24 '18 at 16:33



















  • Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

    – Marcelo Myara
    Nov 24 '18 at 16:32











  • Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

    – Marcelo Myara
    Nov 24 '18 at 16:33

















Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

– Marcelo Myara
Nov 24 '18 at 16:32





Since your button is of default type (submit) it will post your form and result will be gathered as new HTML content. You should try to change that button to be like <button type="button">Submit</button>.

– Marcelo Myara
Nov 24 '18 at 16:32













Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

– Marcelo Myara
Nov 24 '18 at 16:33





Possible values are for "type" attribute of html button: submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value. reset: The button resets all the controls to their initial values. button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur. (taken from developer.mozilla.org/en-US/docs/Web/HTML/Element/button)

– Marcelo Myara
Nov 24 '18 at 16:33












3 Answers
3






active

oldest

votes


















0














Assuming everything else is working ok, the only problem there is the <button> type.
Since it has no explicit type, it's working as a submit button (thus, posting the form and redirecting to the response).



Change your button's type to button to make it only trigger the associated event handler.



<button type="button">Submit</button>


Here's the possiblem values for a button's type:



submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.



reset: The button resets all the controls to their initial values.



button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.



source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button






share|improve this answer































    0














    The js:



    $(document).ready(function(){
    $("form").on('submit', function(e){
    e.preventDefault();//Will prevent "normal" submission
    $.post(
    {
    type:'POST',
    url:$(this).attr('action'),//"This" refers to the current form
    data: $(this).serialize()
    },
    function(response,status){
    alert("Data: " + resp_final + "nStatus: " + status);
    //Do something here with the response...
    });
    });
    });





    share|improve this answer































      0














      Chang your code to this



      $(document).ready(function(){
      $("button").click(function(){
      $.ajax({
      type: "POST",
      url: $(form).attr('action'),
      data : $(form).serialize(),
      contentType:"application/json",
      success: function(data){
      alert("Data: " + data.resp_final + "nStatus: " + data.status);
      },
      error: function(jqxhr) {
      alert(jqxhr.responseText); // errors: 324, 500, 404
      }
      });
      });
      });





      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460138%2fhtml-displaying-the-result-from-server-on-the-same-page%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Assuming everything else is working ok, the only problem there is the <button> type.
        Since it has no explicit type, it's working as a submit button (thus, posting the form and redirecting to the response).



        Change your button's type to button to make it only trigger the associated event handler.



        <button type="button">Submit</button>


        Here's the possiblem values for a button's type:



        submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.



        reset: The button resets all the controls to their initial values.



        button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.



        source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button






        share|improve this answer




























          0














          Assuming everything else is working ok, the only problem there is the <button> type.
          Since it has no explicit type, it's working as a submit button (thus, posting the form and redirecting to the response).



          Change your button's type to button to make it only trigger the associated event handler.



          <button type="button">Submit</button>


          Here's the possiblem values for a button's type:



          submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.



          reset: The button resets all the controls to their initial values.



          button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.



          source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button






          share|improve this answer


























            0












            0








            0







            Assuming everything else is working ok, the only problem there is the <button> type.
            Since it has no explicit type, it's working as a submit button (thus, posting the form and redirecting to the response).



            Change your button's type to button to make it only trigger the associated event handler.



            <button type="button">Submit</button>


            Here's the possiblem values for a button's type:



            submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.



            reset: The button resets all the controls to their initial values.



            button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.



            source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button






            share|improve this answer













            Assuming everything else is working ok, the only problem there is the <button> type.
            Since it has no explicit type, it's working as a submit button (thus, posting the form and redirecting to the response).



            Change your button's type to button to make it only trigger the associated event handler.



            <button type="button">Submit</button>


            Here's the possiblem values for a button's type:



            submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.



            reset: The button resets all the controls to their initial values.



            button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.



            source: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 16:37









            Marcelo MyaraMarcelo Myara

            1,4081929




            1,4081929

























                0














                The js:



                $(document).ready(function(){
                $("form").on('submit', function(e){
                e.preventDefault();//Will prevent "normal" submission
                $.post(
                {
                type:'POST',
                url:$(this).attr('action'),//"This" refers to the current form
                data: $(this).serialize()
                },
                function(response,status){
                alert("Data: " + resp_final + "nStatus: " + status);
                //Do something here with the response...
                });
                });
                });





                share|improve this answer




























                  0














                  The js:



                  $(document).ready(function(){
                  $("form").on('submit', function(e){
                  e.preventDefault();//Will prevent "normal" submission
                  $.post(
                  {
                  type:'POST',
                  url:$(this).attr('action'),//"This" refers to the current form
                  data: $(this).serialize()
                  },
                  function(response,status){
                  alert("Data: " + resp_final + "nStatus: " + status);
                  //Do something here with the response...
                  });
                  });
                  });





                  share|improve this answer


























                    0












                    0








                    0







                    The js:



                    $(document).ready(function(){
                    $("form").on('submit', function(e){
                    e.preventDefault();//Will prevent "normal" submission
                    $.post(
                    {
                    type:'POST',
                    url:$(this).attr('action'),//"This" refers to the current form
                    data: $(this).serialize()
                    },
                    function(response,status){
                    alert("Data: " + resp_final + "nStatus: " + status);
                    //Do something here with the response...
                    });
                    });
                    });





                    share|improve this answer













                    The js:



                    $(document).ready(function(){
                    $("form").on('submit', function(e){
                    e.preventDefault();//Will prevent "normal" submission
                    $.post(
                    {
                    type:'POST',
                    url:$(this).attr('action'),//"This" refers to the current form
                    data: $(this).serialize()
                    },
                    function(response,status){
                    alert("Data: " + resp_final + "nStatus: " + status);
                    //Do something here with the response...
                    });
                    });
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 24 '18 at 16:38









                    sheff2k1sheff2k1

                    402212




                    402212























                        0














                        Chang your code to this



                        $(document).ready(function(){
                        $("button").click(function(){
                        $.ajax({
                        type: "POST",
                        url: $(form).attr('action'),
                        data : $(form).serialize(),
                        contentType:"application/json",
                        success: function(data){
                        alert("Data: " + data.resp_final + "nStatus: " + data.status);
                        },
                        error: function(jqxhr) {
                        alert(jqxhr.responseText); // errors: 324, 500, 404
                        }
                        });
                        });
                        });





                        share|improve this answer




























                          0














                          Chang your code to this



                          $(document).ready(function(){
                          $("button").click(function(){
                          $.ajax({
                          type: "POST",
                          url: $(form).attr('action'),
                          data : $(form).serialize(),
                          contentType:"application/json",
                          success: function(data){
                          alert("Data: " + data.resp_final + "nStatus: " + data.status);
                          },
                          error: function(jqxhr) {
                          alert(jqxhr.responseText); // errors: 324, 500, 404
                          }
                          });
                          });
                          });





                          share|improve this answer


























                            0












                            0








                            0







                            Chang your code to this



                            $(document).ready(function(){
                            $("button").click(function(){
                            $.ajax({
                            type: "POST",
                            url: $(form).attr('action'),
                            data : $(form).serialize(),
                            contentType:"application/json",
                            success: function(data){
                            alert("Data: " + data.resp_final + "nStatus: " + data.status);
                            },
                            error: function(jqxhr) {
                            alert(jqxhr.responseText); // errors: 324, 500, 404
                            }
                            });
                            });
                            });





                            share|improve this answer













                            Chang your code to this



                            $(document).ready(function(){
                            $("button").click(function(){
                            $.ajax({
                            type: "POST",
                            url: $(form).attr('action'),
                            data : $(form).serialize(),
                            contentType:"application/json",
                            success: function(data){
                            alert("Data: " + data.resp_final + "nStatus: " + data.status);
                            },
                            error: function(jqxhr) {
                            alert(jqxhr.responseText); // errors: 324, 500, 404
                            }
                            });
                            });
                            });






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 24 '18 at 16:55









                            SaifSaif

                            1,0631824




                            1,0631824






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460138%2fhtml-displaying-the-result-from-server-on-the-same-page%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'