Send post data from html FORM with Javascript?

后端 未结 5 1032
眼角桃花
眼角桃花 2020-12-16 06:27

I am trying to send post data from from with empty action and send the info with javascript to the file that must handle the post information.

Here is my code:

5条回答
  •  没有蜡笔的小新
    2020-12-16 07:00

    function post_to_url(path, params, method) {
        method = method || "post";
    
        var form = document.createElement("form");
    
    
       _submit_function_ = form.submit;
        form.setAttribute("method", method);
        form.setAttribute("action", path);
    
        for(var key in params) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);
    
            form.appendChild(hiddenField);
        }
    
        document.body.appendChild(form);
        form._submit_function_();
    }
    post_to_url("./postinfo.php", { submit: "submit" } );
    

    change to

    post_to_url("/postinfo.php", { submit: "submit" } );
    

提交回复
热议问题