5

I need help on something that sounds easy but is difficult for me. So when someone clicks on this 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.

Tobi Nary
  • 4,566
  • 4
  • 30
  • 50
Techwizmatt
  • 63
  • 1
  • 1
  • 6
  • 1
    Well, use a form, and submit it ? :D – r4phG Feb 19 '16 at 15:56
  • Taka look at http://www.w3schools.com/html/html_forms.asp and https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/My_first_HTML_form. – Zakaria Acharki Feb 19 '16 at 16:01
  • I think the best you can do is look for tutorial videos like this one https://www.youtube.com/watch?v=IQmpTxKh9k8 ... It will teach you more than can be explained here .. – Daniel Feb 19 '16 at 16:23
  • Possible duplicate of [Make a link use POST instead of GET](http://stackoverflow.com/questions/3915917/make-a-link-use-post-instead-of-get) – Michał Perłakowski Feb 19 '16 at 23:08

3 Answers3

4

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

<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.

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.

Samuil Banti
  • 1,735
  • 1
  • 15
  • 26
4

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>
m1alesis
  • 670
  • 6
  • 16
2

You can use <form> to send data

<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.

r4phG
  • 470
  • 8
  • 16
Munawir
  • 3,346
  • 9
  • 33
  • 51