将Javascript变量传递给PHP POST[复制]

时间:2022-08-27 17:42:51

Possible Duplicate:
JavaScript post request like a form submit

可能的副本:像表单提交一样的JavaScript post请求

I have a value calculated in JS that I'd like to pass as part of an input form to a PHP script. How can I get a value the JS value to the PHP as a POST parameter?

我有一个用JS计算的值,我想把它作为输入表单的一部分传递给PHP脚本。如何将JS值作为POST参数传递给PHP ?

Basically, on submit, I need the total var to be passed via post to the next script.

基本上,在提交时,我需要将总var通过post传递到下一个脚本。

The first idea that comes to mind is create an invisible input form that has the value in it and is inputted with the form, is that possible?

我想到的第一个想法是创建一个无形的输入表单,其中包含有值,并与表单一起输入,这可能吗?

4 个解决方案

#1


13  

Yes you could use an <input type="hidden" /> and set the value of that hidden field in your javascript code so it gets posted with your other form data.

是的,您可以使用,并在javascript代码中设置该隐藏字段的值,以便与其他表单数据一起发布。

#2


14  

There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.

有很多方法可以实现这一点。关于你问的方式,用一个隐藏的表单元素。

create this form element inside your form:

在表单中创建这个表单元素:

<input type="hidden" name="total" value="">

So your form like this:

你的表格是这样的:

<form id="sampleForm" name="sampleForm" method="post" action="phpscript.php">
<input type="hidden" name="total" id="total" value="">
<a href="#" onclick="setValue();">Click to submit</a>
</form>

Then your javascript something like this:

你的javascript是这样的:

<script>
function setValue(){
    document.sampleForm.total.value = 100;
    document.forms["sampleForm"].submit();
}
</script>

#3


7  

You can do this using Ajax. I have a function that I use for something like this:

您可以使用Ajax实现这一点。我有一个函数,我用它来做类似的事情:

function ajax(elementID,filename,str,post)
{
    var ajax;
    if (window.XMLHttpRequest)
    {
        ajax=new XMLHttpRequest();//IE7+, Firefox, Chrome, Opera, Safari
    }
    else if (ActiveXObject("Microsoft.XMLHTTP"))
    {
        ajax=new ActiveXObject("Microsoft.XMLHTTP");//IE6/5
    }
    else if (ActiveXObject("Msxml2.XMLHTTP"))
    {
        ajax=new ActiveXObject("Msxml2.XMLHTTP");//other
    }
    else
    {
        alert("Error: Your browser does not support AJAX.");
        return false;
    }
    ajax.onreadystatechange=function()
    {
        if (ajax.readyState==4&&ajax.status==200)
        {
            document.getElementById(elementID).innerHTML=ajax.responseText;
        }
    }
    if (post==false)
    {
        ajax.open("GET",filename+str,true);
        ajax.send(null);
    }
    else
    {
        ajax.open("POST",filename,true);
        ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        ajax.send(str);
    }
    return ajax;
}

The first parameter is the element you want to change. The second parameter is the name of the filename you're loading into the element you're changing. The third parameter is the GET or POST data you're using, so for example "total=10000&othernumber=999". The last parameter is true if you want use POST or false if you want to GET.

第一个参数是要更改的元素。第二个参数是要加载到要更改的元素中的文件名的名称。第三个参数是您正在使用的GET或POST数据,例如“total=10000&othernumber=999”。如果您希望使用POST,则最后一个参数为true;如果希望获取,则为false。

#4


3  

Your idea of an hidden form element is solid. Something like this

您对隐藏表单元素的想法是可靠的。像这样的东西

<form action="script.php" method="post">
<input type="hidden" name="total" id="total">
</form>

<script type="text/javascript">
var element = document.getElementById("total");
element.value = getTotalFromSomewhere;
element.form.submit();
</script>

Of course, this will change the location to script.php. If you want to do this invisibly to the user, you'll want to use AJAX. Here's a jQuery example (for brevity). No form or hidden inputs required

当然,这将把位置更改为script.php。如果您希望在用户看不到的情况下执行此操作,那么您将希望使用AJAX。这里有一个jQuery示例(为了简洁)。不需要任何形式或隐藏的输入。

$.post("script.php", { total: getTotalFromSomewhere });

#1


13  

Yes you could use an <input type="hidden" /> and set the value of that hidden field in your javascript code so it gets posted with your other form data.

是的,您可以使用,并在javascript代码中设置该隐藏字段的值,以便与其他表单数据一起发布。

#2


14  

There is a lot of ways to achieve this. In regards to the way you are asking, with a hidden form element.

有很多方法可以实现这一点。关于你问的方式,用一个隐藏的表单元素。

create this form element inside your form:

在表单中创建这个表单元素:

<input type="hidden" name="total" value="">

So your form like this:

你的表格是这样的:

<form id="sampleForm" name="sampleForm" method="post" action="phpscript.php">
<input type="hidden" name="total" id="total" value="">
<a href="#" onclick="setValue();">Click to submit</a>
</form>

Then your javascript something like this:

你的javascript是这样的:

<script>
function setValue(){
    document.sampleForm.total.value = 100;
    document.forms["sampleForm"].submit();
}
</script>

#3


7  

You can do this using Ajax. I have a function that I use for something like this:

您可以使用Ajax实现这一点。我有一个函数,我用它来做类似的事情:

function ajax(elementID,filename,str,post)
{
    var ajax;
    if (window.XMLHttpRequest)
    {
        ajax=new XMLHttpRequest();//IE7+, Firefox, Chrome, Opera, Safari
    }
    else if (ActiveXObject("Microsoft.XMLHTTP"))
    {
        ajax=new ActiveXObject("Microsoft.XMLHTTP");//IE6/5
    }
    else if (ActiveXObject("Msxml2.XMLHTTP"))
    {
        ajax=new ActiveXObject("Msxml2.XMLHTTP");//other
    }
    else
    {
        alert("Error: Your browser does not support AJAX.");
        return false;
    }
    ajax.onreadystatechange=function()
    {
        if (ajax.readyState==4&&ajax.status==200)
        {
            document.getElementById(elementID).innerHTML=ajax.responseText;
        }
    }
    if (post==false)
    {
        ajax.open("GET",filename+str,true);
        ajax.send(null);
    }
    else
    {
        ajax.open("POST",filename,true);
        ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        ajax.send(str);
    }
    return ajax;
}

The first parameter is the element you want to change. The second parameter is the name of the filename you're loading into the element you're changing. The third parameter is the GET or POST data you're using, so for example "total=10000&othernumber=999". The last parameter is true if you want use POST or false if you want to GET.

第一个参数是要更改的元素。第二个参数是要加载到要更改的元素中的文件名的名称。第三个参数是您正在使用的GET或POST数据,例如“total=10000&othernumber=999”。如果您希望使用POST,则最后一个参数为true;如果希望获取,则为false。

#4


3  

Your idea of an hidden form element is solid. Something like this

您对隐藏表单元素的想法是可靠的。像这样的东西

<form action="script.php" method="post">
<input type="hidden" name="total" id="total">
</form>

<script type="text/javascript">
var element = document.getElementById("total");
element.value = getTotalFromSomewhere;
element.form.submit();
</script>

Of course, this will change the location to script.php. If you want to do this invisibly to the user, you'll want to use AJAX. Here's a jQuery example (for brevity). No form or hidden inputs required

当然,这将把位置更改为script.php。如果您希望在用户看不到的情况下执行此操作,那么您将希望使用AJAX。这里有一个jQuery示例(为了简洁)。不需要任何形式或隐藏的输入。

$.post("script.php", { total: getTotalFromSomewhere });