0

I want to send a data from javascript to php. This data is the id of the html div that selected by user. It means that first user click a div and then the id of that div send to javascript file and now I want to send it back to my php file. I dont want use HTML form. how can I do it?

Here is my HTML code:

<div onclick="myFunctionElt(this);" class="player_interface" id="'.$id[$i].'">

</div>

This is my javascript file:

function myFunctionElt (elt) {

    $.post('phpurl.php', { id: elt.id} );

}

And now I want to do this:

$value =  $_POST['id'];
Daniel Böhmer
  • 12,527
  • 5
  • 31
  • 45
nimo
  • 7
  • 2

3 Answers3

0

Try this:

xmlhttp.open("POST","PHPFILE.PHP",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("id=" + elt.id);

Also look at this: Send POST data using XMLHttpRequest

Community
  • 1
  • 1
Robin Rijkeboer
  • 420
  • 3
  • 15
  • 1
    Why should the OP "try this"? Please add an explanation of what you did and why you did it that way not only for the OP but for future visitors to SO. – Jay Blanchard May 05 '15 at 12:39
0

try like this,

function myFunctionElt (elt) {

    $.post('phpurl.php', { id: $(elt).attr('id')} );

}
Ayyanar G
  • 1,560
  • 1
  • 10
  • 20
0

just another like this.. use myFunctionElt(this.value). you missed .value

<button onclick="myFunctionElt(this.value);" class="player_interface" id="'.$id[$i].'" value="2">Click</button>

    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
    function myFunctionElt (elt) {
    alert(elt);
        $.post('phpurl.php', { id: elt.id} );

    }

    </script>
Kavin Smk
  • 784
  • 9
  • 33