Javascript 从 onclick 函数将方法发布到 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35509396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:49:42  来源:igfitidea点击:

Post Method to URL from a onclick function

javascriptphphtml

提问by Techwizmatt

I need help on something that sounds easy but is difficult for me. So when someone clicks on this div:

我需要一些听起来容易但对我来说很难的事情的帮助。所以当有人点击这个 div 时:

<div onclick="<go to url sending data using the post method>">Click Me</div>

I want it to send data to a PHP file that will take the information that i want it to. I would use the GET function but I have heard that its easily hackable. If their is a lot simpler solution or something more secure please help me out.

我希望它将数据发送到一个 PHP 文件,该文件将获取我想要的信息。我会使用 GET 函数,但我听说它很容易被破解。如果他们的解决方案更简单或更安全,请帮助我。

采纳答案by Samuil Banti

If you need to use div you can do it like this but I suggest that you use button or input of type submit.

如果您需要使用 div,您可以这样做,但我建议您使用按钮或提交类型的输入。

<form id="form-id" method="post" action="your-php-file-url">
<input type="hidden" name="your-variable-name" value="your-variable-value">
<div onclick="document.getElementById('form-id').submit();">Click Me</div>
</form>

Also you may use jQuery or some other JS library.

您也可以使用 jQuery 或其他一些 JS 库。

NOTE:Keep in mind that if the data that you send is provided via browser it's really easy to manipulate (doesn't mater if you use POST or GET) so it's important to check it out when you process it.

注意:请记住,如果您发送的数据是通过浏览器提供的,那么它真的很容易操作(如果您使用 POST 或 GET 则无关紧要),因此在处理它时检查它很重要。

回答by m1alesis

Using form would be ideal. If for some reason if you don't want to use form or want to build a dynamic app then use it in this way.

使用表单将是理想的。如果出于某种原因,如果您不想使用表单或想要构建动态应用程序,请以这种方式使用它。

//jquery library
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input id="someInput">
<div onclick="sendData()">Click Me</div>

<script>
    function sendData(){
        //get the input value
        $someInput = $('#someInput').val();
        $.ajax({
            //the url to send the data to
            url: "ajax/url.ajax.php",
            //the data to send to
            data: {someInput : $someInput},
            //type. for eg: GET, POST
            type: "POST",
            //datatype expected to get in reply form server
            dataType: "json",
            //on success
            success: function(data){
                //do something after something is recieved from php
            },
            //on error
            error: function(){
                //bad request
            }
        });
    }

</script>

回答by Munawir

You can use <form>to send data

你可以<form>用来发送数据

<form action="yourpage.php" method="post">
    //form contents
     <input type="submit" value="Submit">
</form>

The action URL specifies the URL of the page to which your data has to be send.

操作 URL 指定您的数据必须发送到的页面的 URL。