Javascript 将数据从js发送到php而不使用表单
我想将数据从javascript发送到php。此数据是用户选择的html div的id。这意味着第一个用户单击一个div,然后该div的id发送到javascript文件,现在我想将它发送回我的php文件。我不想使用HTML表单。我怎么做 以下是我的HTML代码:Javascript 将数据从js发送到php而不使用表单,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我想将数据从javascript发送到php。此数据是用户选择的html div的id。这意味着第一个用户单击一个div,然后该div的id发送到javascript文件,现在我想将它发送回我的php文件。我不想使用HTML表单。我怎么做 以下是我的HTML代码: <div onclick="myFunctionElt(this);" class="player_interface" id="'.$id[$i].'"> </div> 现在我想这样做: $value =
<div onclick="myFunctionElt(this);" class="player_interface" id="'.$id[$i].'">
</div>
现在我想这样做:
$value = $_POST['id'];
试试这个:
xmlhttp.open("POST","PHPFILE.PHP",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("id=" + elt.id);
再看看这个:像这样试试
function myFunctionElt (elt) {
$.post('phpurl.php', { id: $(elt).attr('id')} );
}
再像这样。。使用myFunctionEltthis.value。你错过了。价值
你在正确的道路上。您卡在哪里了?$值在此之后将为空!为什么OP要尝试这个?请添加一个解释,说明你做了什么,以及你为什么这样做,不仅是为了OP,也是为了未来的访客。
function myFunctionElt (elt) {
$.post('phpurl.php', { id: $(elt).attr('id')} );
}
<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>