JavaScript n PHP w /选择框 [英] JavaScript n PHP w/ Select Boxes

查看:142
本文介绍了JavaScript n PHP w /选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例是此处



我将国家从一个选择框移到另一个选择框,当我提交表单时,我想让php使用正确的文本框中的值。当我给正确的方框命名一个php数组时,如ToLB [],Javascript失败。我该如何处理这个问题,以便提交的值将被php进程使用?



解决方案

我看了一下你的代码。
有一些缺失的部分和修改,以使其工作。
我没有测试修正,但我认为它们应该有效。
$ b $ 1)你必须在表单中添加隐藏字段。

 < form name =combo_boxaction =test.php> 
< input type =hiddenname =valuesToSubmitvalue =>

2)然后您必须将id赋给选择框,因为您使用id来引用对象像这样var selectobject = document.getElementById(ToLB);

 < select multiple size =10name = ToLBid =ToLBstyle =width:150> 

3)用正常的按钮更改提交按钮,这样您可以强制只在循环

 < input type =buttonname =submitid =submitvalue =SubmitonClick =updateValues()> 

4)在javascript结尾处强制提交

  function updateValues(){
var selectobject = document.getElementById(ToLB);
var myValues =;
for(var i = 0; i< selectobject.length; i ++){
myValues = myValues + selectobject.options [i] .value +,;
}
document.form.valuesToSubmit.value = myValues;
document.combo_box.submit();
}


Example is here.

I'm moving countries from one select box to another, when I submit the form I want the values in the right text box to be used by php. When I give the right box a name for a php array, like ToLB[] the Javascript fails. How can I handle this so that the submitted values will be used by php processes?

解决方案

I have taken a look at your code. There are some missing parts and amendments to do to make it works. I didn't test the amendments but I think they should work.

1)you have to add the hidden field inside the form.

<form name="combo_box" action="test.php">
<input type="hidden" name="valuesToSubmit" value="">

2)Then you have to give the id to the select box because you use the id to reference the object like this var selectobject=document.getElementById("ToLB");

<select multiple size="10" name="ToLB" id="ToLB" style="width:150">

3)Change th submit button with a normal button so you can force the submit only when the loop is ended and the values have been passed into the hidden field.

<input type="button" name="submit" id="submit" value="Submit"  onClick="updateValues()">

4)Force the submit at the end of the javascript

function updateValues(){
    var selectobject=document.getElementById("ToLB");
    var myValues = "";
    for (var i=0; i<selectobject.length; i++){
    myValues = myValues  + selectobject.options[i].value + ",";
    }
    document.form.valuesToSubmit.value=myValues;
    document.combo_box.submit();
    }

这篇关于JavaScript n PHP w /选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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