<html>
<head>
<script>
function addstafflist()
{
for(var i = 0; i < document.getElementById("stafflist").options.length; i++)
{
if(document.getElementById("stafflist").options[i].selected)
{
var staffname = document.getElementById("stafflist").options[i].text;
var selectedstaffnamelist = document.getElementById("selectedstafflist");
var found = false;
for(var j = 0; j < selectedstaffnamelist.length; j++)
{
if(selectedstaffnamelist[j].text == staffname)
{
found = true;
break;
}
}
if(!found)
{
var option = document.createElement("option");
option.text = staffname;
selectedstaffnamelist.add(option);
}
}
}
</script>
</head>
<body>
<form action="test2.php" method="post">
<table border="0" width="300" style="border-collpase:collapse">
<tr>
<td width="50">
<select id="stafflist" name="stafflist[]" style="width:70px;" multiple>
<option value="ali@gmail.com">ali</option>
<option value="abu@gmail.com">abu</option>
<option value="ahmad@gmail.com">ahmad</option>
</select>
</td>
<td width="30">
<input type="button" name="add" value=">>" onclick="addstafflist()" />
</td>
<td>
<select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple>
</select>
</td>
</tr>
<tr height="20">
<td colspan="3">
<input type="submit" name="submit" value="Show" />
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
//[HERE]
}
?>
从上面的代码中,当我从下拉列表中选择ali,abu并单击"显示"按钮时,我想在单击[HERE]部分中的"显示"按钮后使用PHP显示其电子邮件地址(即 ali@gmail.com,abu@gmail.com(。有人可以帮助我吗?
通过所有已发布的员工列表和回显电子邮件添加以下代码;
<?php
if(isset($_POST['submit']))
{
foreach($_POST['stafflist'] as $email){
echo $email . "<br/>";
}
}
?>
虽然我不确定你使用你所有的JS是为了什么。
我为您的代码提供了一些建议。这是一种正确的方法,您需要验证您的提交:
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
// If stafflist is array(), then you can go to loop
if(is_array($_POST['stafflist']))
{
// Run the loop
foreach($_POST['stafflist'] as $email)
{
// Show all emails per line
echo $email . "<br/>";
}
}
?>
<form action="test2.php" method="post">
<table border="0" width="300" style="border-collpase:collapse">
<tr>
<td width="50">
<select id="stafflist" name="stafflist[]" style="width:70px;" multiple>
<option value="ali@gmail.com">ali</option>
<option value="abu@gmail.com">abu</option>
<option value="ahmad@gmail.com">ahmad</option>
</select>
</td>
<td width="30">
<input type="button" name="add" value=">>" onclick="addstafflist()" />
</td>
<td>
<select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple>
</select>
</td>
</tr>
<tr height="20">
<td colspan="3">
<input type="submit" name="submit" value="Show" />
</td>
</tr>
</table>
</form>
<script>
// Put all JS before end of body
function addstafflist()
{
for(var i = 0; i < document.getElementById("stafflist").options.length; i++)
{
if(document.getElementById("stafflist").options[i].selected)
{
var staffname = document.getElementById("stafflist").options[i].text;
var selectedstaffnamelist = document.getElementById("selectedstafflist");
var found = false;
for(var j = 0; j < selectedstaffnamelist.length; j++)
{
if(selectedstaffnamelist[j].text == staffname)
{
found = true;
break;
}
}
if(!found)
{
var option = document.createElement("option");
option.text = staffname;
selectedstaffnamelist.add(option);
}
}
}
</script>
</body>
</html>
我使用了克里斯蒂亚诺的 php 建议来首先检查 POST 是否是一个数组。我也同意最好将 javascript 放在结束正文标签附近,这样脚本越多,页面渲染的延迟就越少。另一个小心代码嵌套的好建议。它使阅读和调试更容易...缺少支架会让您失去宝贵的时间。;)
迈克尔,我只在你的代码中添加了 3 行。我使用了正确的 POST,即选定的 POST。测试工作。
<html>
<head>
<title>Example</title>
</head>
<?php
if(isset($_POST['submit']))
{
//[HERE]
// If stafflist is array(), then you can go to loop
if(is_array($_POST['stafflist']))
{
foreach($_POST['selectedstafflist'] as $email){
echo $email . "<br/>";
}
}
}
?>
<body>
<form action="test2.php" method="post">
<table border="0" width="300" style="border-collpase:collapse">
<tr>
<td width="50">
<select id="stafflist" name="stafflist[]" style="width:70px;" multiple>
<option value="ali@gmail.com">ali</option>
<option value="abu@gmail.com">abu</option>
<option value="ahmad@gmail.com">ahmad</option>
</select>
</td>
<td width="30">
<input type="button" name="add" value=">>" onclick="addstafflist()" />
</td>
<td>
<select id="selectedstafflist" name="selectedstafflist[]" style="width:70px;" multiple>
</select>
</td>
</tr>
<tr height="20">
<td colspan="3">
<input type="submit" name="submit" value="Show" />
</td>
</tr>
</table>
</form>
<script>
function addstafflist()
{
for(var i = 0; i < document.getElementById("stafflist").options.length; i++)
{
if(document.getElementById("stafflist").options[i].selected)
{
var staffname = document.getElementById("stafflist").options[i].text;
var staffemail = document.getElementById("stafflist").options[i].value; // <-- Added this line
var selectedstaffnamelist = document.getElementById("selectedstafflist");
var found = false;
for(var j = 0; j < selectedstaffnamelist.length; j++)
{
if(selectedstaffnamelist[j].text == staffname)
{
found = true;
break;
}
}
if(!found)
{
var option = document.createElement("option");
option.text = staffname;
option.value = staffemail; // <-- Added this line
option.selected=true; // <-- Added this line
selectedstaffnamelist.add(option);
}
}
}
} // <-- Added this missing bracket
</script>
</body>