0

我用这个来添加/删除选择列表

<form action="home.php?action=test" method="post" id="myform" name="myform" onsubmit="ValidatePageForm()">
<fieldset>
<select name="selectfrom" id="select-from" multiple size="5">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
</select>
<a href="JavaScript:void(0);" id="btn-add">Add &raquo;</a>
<a href="JavaScript:void(0);" id="btn-remove">&laquo; Remove</a>
<select name="selectto" id="select-to" multiple size="5">
<option value="5">Item 5</option>
<option value="6">Item 6</option>
<option value="7">Item 7</option>
</select>
</fieldset>
<input name="submit" type="submit" value="submit" />
</form>

使用下面的脚本我可以看到所选选项的值

<script type="text/javascript">
function ValidatePageForm()
{
$('#select-to option').each(function(){
alert($(this).val());
});
}
</script>

但是我怎样才能获取这些值并将它们存储在数据库中?因为当我提交时,我在转储后看不到数据。

请对此提供任何帮助。

谢谢, 沙赫扎德

4

1 回答 1

0
<form action="home.php?action=test" method="post" id="myform" name="myform" onsubmit="ValidatePageForm()">
<fieldset>
<select name="selectfrom" id="select-from" multiple size="5">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
<option value="3">Item 3</option>
<option value="4">Item 4</option>
</select>
<a href="JavaScript:void(0);" id="btn-add">Add &raquo;</a>
<a href="JavaScript:void(0);" id="btn-remove">&laquo; Remove</a>
<select name="selectto[]" id="select-to" multiple size="5"><!--declare name as array-->
<option value="5">Item 5</option>
<option value="6">Item 6</option>
<option value="7">Item 7</option>
</select>
</fieldset>
<input name="submit" type="submit" value="submit" />
</form>

由于您试图获取多个值,首先您需要将您的元素声明为数组,然后您可以像这样在 php 中获取这些值

 <?php
    if(isset($_POST['selectto'])){
        $selecto = $_POST['selectto'];
        foreach($selecto as $selecto_vals)
             echo $selecto_vals;           
    }
  ?>
于 2012-12-19T09:55:23.367 回答