1

So I'm trying to send the form info to php with ajax form every second, but for some reason it doesn't want to.

Here is my latest attempt, I tried every other similar combination(like put everything in the function or just put everything into the setInterval).

$(document).ready(function() {
    var ajaxCall=function() {
        $("#myForm").ajaxForm(function(e) {
            $.ajax({
                type:'post',
                url:'php1.php',
                data:$("#myForm").serialize(),
                success:function(data) {
                    document.getElementById("result").innerHTML=data;
                }
            });
        });
    }
    setInterval(ajaxCall,1000);
});

EDIT

Solved with M.M answer, thank you for the help!

Boanta Ionut
  • 402
  • 8
  • 24

2 Answers2

0

Simply change ajaxForm to ajaxSubmit

See this (question) and this (documentation) for more information on AjaxForm vs AjaxSubmit

Essentially AjaxForm submits when the user clicks the button and AjaxSubmit does it immediately so your code should be:

$(document).ready(function()
{
    var ajaxCall=function()
    {
    $("#myForm").ajaxSubmit(function(e)
    {
        $.ajax(
        {
            type:'post',
            url:'php1.php',
            data:$("#myForm").serialize(),
            success:function(data)
            {
                document.getElementById("result").innerHTML=data;
            }
        });
    });
    }
    setInterval(ajaxCall,1000);
});

Update after comment explanation

$(document).ready(function(){
    //live feed
    var ajaxCall=function(){
        $("#myForm").ajaxSubmit(function(e){
            ajax_submit();
        });
    }
    setInterval(ajaxCall,1000);

    //real submit
    $("#myForm").ajaxForm(function(e){
        ajax_submit();
    });

    function ajax_submit(){//ajax_code
        $.ajax({
            type:'post',
            url:'php1.php',
            data:$("#myForm").serialize(),
            success:function(data) {
                document.getElementById("result").innerHTML=data;
            }
        });
    }
});

If you wish to differentiate the feed from the submit you can pass a parameter to the ajax_submit function

Community
  • 1
  • 1
M.M
  • 2,254
  • 1
  • 20
  • 33
  • it works really nice that way, but in my case i really need to press the submit button because I have an option list. Any way I can do it with ajaxForm? – Boanta Ionut Aug 27 '16 at 16:37
  • Walk me through it, is the user supposed to submit the form once and you need to read the information every second? – M.M Aug 27 '16 at 16:39
  • yes, it's some kind of remote access to Linux, so the user selects one command (ex. ifconfig) and it should give him a live feed of that. – Boanta Ionut Aug 27 '16 at 16:40
  • So i suggest you separate the actions, one function to check the submition and another to receive the info, I'll update my answer – M.M Aug 27 '16 at 16:42
  • @BoantaIonut if this answer truly solved your problem consider accepting it as the correct answer. – M.M Aug 27 '16 at 21:33
0

Getting rid of the ajaxForm() call seems to accomplish what you are trying to do:

$(document).ready(function() {
  var ajaxCall = function() {
      $.ajax({
        type: 'post',
        url: 'php1.php',
        data: $("#myForm").serialize(),
        success: function(data) {
          document.getElementById("result").innerHTML = data;
        }
      });
  }
  setInterval(ajaxCall, 1000);
});
Chizzle
  • 1,707
  • 1
  • 17
  • 26