JQuery and then instantly display published data on one page

How can I send data using ajax and display published data in a div on one page without refreshing the page?

+3
source share
3 answers

To do this, you do not have to submit the form, but register the form data using the click event, send the data via AJAX, and as a last ... enter the data into the element DIV. For instance.

JQuery

$("#submitdata").click(function() {
  var FormVal={ datafield1:$('#field1').val(),
                datafield2:$('#field2').val()};

  $.ajax({
    type: "POST",
    url: "myupdatepage.cfm",
    dataType: "json",
    data: FormVal,
    async: false,
    success: function(response) { 
               $('#fillmein').html($('#field1').val()+'<br />'+$('#field1').val()); // Assign the values to the DIV
             }
  });
});

HTML

<form action="">
  <input type="text" id="field1">
  <input type="text" id="field2">

  <input type="button" id="submitdata" value="Submit data" />
</form>

<div id="fillmein"></div>
+6
source

best jquery form plugin http://jquery.malsup.com/form/ even the ability to upload files - ajaxed

0
source

- ...

<form action="http://google.co.uk/search" method="get" onsubmit="postTheForm(this); return false;">
    <input type="hidden" name="q" value="Stackoverflow"/>
    <input type="submit" value="Click here"/>
</form>
<div id="postResults"></div>

script ...

function postTheForm(theForm){
    $.post(
        theForm.action, 
        $(theForm).serializeArray(), 
        function(data, textStatus, xmlHttpRequest){
            $("#postResults").html(data);
        }
    );
}

GET, , Google POST - , POST (, POST )

0

Source: https://habr.com/ru/post/1751753/


All Articles