如何将javascript变量的值分配给php变量 [英] How to assign the value of a javascript variable to a php variable

查看:85
本文介绍了如何将javascript变量的值分配给php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格.

<form action="inc/genxml.php" method="post">
      <input id="nameTxt" name="name" type="text" value="test"/>
      <button id="nameSave" class="left">Save</button>
</form>

还有一个div元素#name

And a div element #name

当我单击保存按钮时,我想将div #name的位置传递给表单操作文件. 为了获得位置,我使用的是jQuery .position().

When I click the save button, I want to pass the position of the div #name to the form action file. To get the position, I'm using jQuery .position().

类似下面的内容. (仅打印出坐标)

Something like below. (which just prints out the coordinates)

$('#nameSave').click(
    function() {
        var pos = $('#name').position();
        alert("left: " + pos.left + ", top: " + pos.top );
    }
);

我想将坐标值(pos.left和post.top)传递给表单操作文件(在这种情况下,传递给genxml.php文件).

I want to pass the coordinate values (pos.left & post.top) to the form action file (in this case to the file genxml.php).

我应该怎么做?

推荐答案

最简单的方法是使用隐藏的输入字段,并使用jQuery设置此输入字段或您所用的这些输入字段的值.

The easiest way would be to use a hidden input field and use jQuery to set the value for this input field or these input fields in your case.

HTML:

<form action="inc/genxml.php" method="post">
    <input id="nameTxt" name="name" type="text" value="test"/>
    <input id="posLeft" name="posLeft" type="hidden" />
    <input id="posRight" name="posRight" type="hidden" />
    <button id="nameSave" class="left">Save</button>
</form>

JS:

$('#nameSave').click(
    function() {
        var pos = $('#name').position();
        $('#posLeft').val(pos.left);
        $('#posRight').val(pos.right);
    }
);

这篇关于如何将javascript变量的值分配给php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆