Php javascript函数未显示正确的页面

Php javascript函数未显示正确的页面,php,javascript,Php,Javascript,我想根据下拉选择打开一个特定窗口,该函数在从数据库中提取数据集的第一行上运行良好 因为我不知道每次脚本运行时会出现多少行,所以我创建了一个循环,将函数写入的次数与出现的行相同,并将它们命名为不同的行。 问题是,当我从第一行更改下拉列表时,会打开正确的窗口,例如custom.php?id=1,但当我打开第二个窗口时,如果我从列表中选择comunicarse,它会打开custom.php?id=2 id=2是用户的id 这是代码,我真的希望你能理解我 <html> <head&g

我想根据下拉选择打开一个特定窗口,该函数在从数据库中提取数据集的第一行上运行良好 因为我不知道每次脚本运行时会出现多少行,所以我创建了一个循环,将函数写入的次数与出现的行相同,并将它们命名为不同的行。 问题是,当我从第一行更改下拉列表时,会打开正确的窗口,例如custom.php?id=1,但当我打开第二个窗口时,如果我从列表中选择comunicarse,它会打开custom.php?id=2 id=2是用户的id 这是代码,我真的希望你能理解我

 <html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
table {
border-style:ridge;
border-width:1px solid;
border-collapse:collapse;
font-family:sans-serif;
font-size:12px;
width:100%;
}
table thead th, table.excel tbody th {
background:#CCCCCC;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align:bottom;
}
table tbody th {
text-align:center;

}
table tbody td {
vertical-align:bottom;
}
table tbody td {
padding: 0 3px;
border: 1px solid 0000000;
}

table  td {
padding: 0 3px;
border: 1px solid 0000000;

}

</style>


<link rel="stylesheet" href="../css/style.css" type="text/css">



<?php 
include("../../cons/dbinfo.php");

$busco_react = "SELECT * 
FROM  `envios_mercadolibre` 
WHERE status != 'ENTREGADA'  AND status != 'CANCELADA' AND aviso != 1";
$bus1 = mysql_query($busco_react);
while ($b1 = mysql_fetch_array($bus1)){
$para = $b1['id'];

echo '<script language="Javascript" type="text/javascript">
function ReactToChange'.$para.'()
{
if (document.getElementById("DropDownList").value === "custom")
{
    window.open("custom.php?id='.$para.'")
}
else if (document.getElementById("DropDownList").value === "comunicarse")
{
    window.open("comunicarse.php?id='.$para.'")
}
else if (document.getElementById("DropDownList").value === "esperando")
{
    window.open("esperando.php?id='.$para.'")
}
else if (document.getElementById("DropDownList").value === "compro")
{
    window.open("compro.php?id='.$para.'")
}
else {}
}
</script>';

 }
?>
</head>
<body>
<table width = "2200px">
<tr><td colspan="3" align="center"><a href="../index.php">Back to index</a></td></tr>
</table>
<br />
<?php 

echo "<form method='post' action='sendemails.php'>";
echo "<table  border='1' cellpading ='0' cellspacing ='0'>";
echo "<thead><tr><th>MAIL</th><th>Email Address</th><th>Content</th>
<th>Extra 1</th><th>Extra 2</th><th>Full Address</th><th>Full Name</th></tr>
</thead><tbody>";

$bus = "SELECT * FROM  `envios_mercadolibre` WHERE status != 'ENTREGADA'  AND status   !=    'CANCELADA' AND aviso != 1";
$bu = mysql_query($bus) or die("este");
while ($b = mysql_fetch_array($bu)){
$contenido = $b['contenido'];
$extra1 = $b['extra1'];
$extra2 = $b['extra2'];
$usuario_id = $b['usuario_id'];
$para1 = $b['id'];

$datosUsuario = "SELECT nombre, apellido, email from usuarios WHERE id = $usuario_id";
$datosU = mysql_query($datosUsuario) or die("foo");
while ($c = mysql_fetch_array($datosU)){
$nombre = $c['nombre'];
$apellido = $c['apellido'];
$email = $c['email'];


$todoelnombre = $nombre.", ".$apellido;

echo "<tr>
<td><select id='DropDownList' onchange='ReactToChange$para1()'>
<option value='later'>LATER</option>
<option value='comunicarse'>COMUNICARSE CON NOSOTROS</option>
<option value='esperando'>ESPERANDO CALIFICACION EN MERCADO LIBRE</option>
<option value='compro'>NO COMPRO</option>
<option value='custom'>CUSTOM</option>
</select></td>
<td>$email</td>
<td>$contenido</td>
<td>$extra1</td>
<td>$extra2</td>
<td>$todaladire</td>
<td>$todoelnombre
<input type='hidden' name ='datos-$para1' value ='$email' />
</td>
</tr>
";



}

}

echo "<tr><TD colspan ='7' align='center'><input type='submit' /></td></tr></table>";




?>


</div>
</body>
</html>

您可以编写多个函数,而不是编写多个函数

function ReactToChange(para)
{
  //only call document.getElementById("DropDownList").value once - good programming practice
  var dropdownvalue = document.getElementById("DropDownList").value;
  if (dropdownvalue  === "custom")
  {
     window.open("custom.php?id='"+para+"'")
  }
  else if (dropdownvalue  === "comunicarse")
  {
     window.open("comunicarse.php?id='"+para+"'")
  }
 ...
}


<select id='DropDownList' onchange='ReactToChange($para1)'>
  <option value='later'>LATER</option>
  ...
</select>
现在你的代码更短了,因为你只有一个函数来处理所有的事情 请注意,您正在将Id作为参数传递

如果您有数千条记录,则会生成数千个函数,使页面速度减慢

不确定.value是否始终正常工作。在代码中考虑下面的变化:

function ReactToChange'.$para.'()
{
    var dropdown = document.getElementById('DropDownList'),
    selectedValue = dropdown.options[dropdown.selectedIndex];

    // now make all comparisons against selectedValue
    // ...
    if (selectedValue === 'comunicarse') {
        window.open("comunicarse.php?id='.$para.'")
    }
    // ...
}

在没有看到数据库结构的情况下很难给出准确的答案,但首先我不会在while循环中创建javascript函数,因为它会生成多个函数。最重要的是,如果您使用一个参数化的javascript函数,并且稍后使用适当的参数调用该函数,那么您确实不需要第一次选择

我已经修改了您的代码,请看下面一个更有效的javascript函数及其调用:

<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
table {
border-style:ridge;
border-width:1px solid;
border-collapse:collapse;
font-family:sans-serif;
font-size:12px;
width:100%;
}
table thead th, table.excel tbody th {
background:#CCCCCC;
border-style:ridge;
border-width:1;
text-align: center;
vertical-align:bottom;
}
table tbody th {
text-align:center;

}
table tbody td {
vertical-align:bottom;
}
table tbody td {
padding: 0 3px;
border: 1px solid 0000000;
}

table  td {
padding: 0 3px;
border: 1px solid 0000000;

}

</style>

<link rel="stylesheet" href="../css/style.css" type="text/css">

<script language="Javascript" type="text/javascript">
function ReactToChange(para,ptype)
{
    window.open(ptype+".php?id="+para);
}
</script>

</head>
<body>
<table width = "2200px">
<tr><td colspan="3" align="center"><a href="../index.php">Back to index</a></td></tr>
</table>
<br />
<?php 

echo "<form method='post' action='sendemails.php'>";
echo "<table  border='1' cellpading ='0' cellspacing ='0'>";
echo "<thead><tr><th>MAIL</th><th>Email Address</th><th>Content</th>
<th>Extra 1</th><th>Extra 2</th><th>Full Address</th><th>Full Name</th></tr>
</thead><tbody>";

$bus = "SELECT * FROM  `envios_mercadolibre` WHERE status != 'ENTREGADA'  AND status   !=    'CANCELADA' AND aviso != 1";
$bu = mysql_query($bus) or die("este");
while ($b = mysql_fetch_array($bu)){
$contenido = $b['contenido'];
$extra1 = $b['extra1'];
$extra2 = $b['extra2'];
$usuario_id = $b['usuario_id'];
$para1 = $b['id'];

$datosUsuario = "SELECT nombre, apellido, email from usuarios WHERE id = $usuario_id";
$datosU = mysql_query($datosUsuario) or die("foo");
while ($c = mysql_fetch_array($datosU)){
$nombre = $c['nombre'];
$apellido = $c['apellido'];
$email = $c['email'];


$todoelnombre = $nombre.", ".$apellido;

echo "<tr>
<td><select id='DropDownList' onchange='ReactToChange($para1,this.options[this.selectedIndex].value)'>
<option value='later'>LATER</option>
<option value='comunicarse'>COMUNICARSE CON NOSOTROS</option>
<option value='esperando'>ESPERANDO CALIFICACION EN MERCADO LIBRE</option>
<option value='compro'>NO COMPRO</option>
<option value='custom'>CUSTOM</option>
</select></td>
<td>$email</td>
<td>$contenido</td>
<td>$extra1</td>
<td>$extra2</td>
<td>$todaladire</td>
<td>$todoelnombre
<input type='hidden' name ='datos-$para1' value ='$email' />
</td>
</tr>
";



}

}

echo "<tr><TD colspan ='7' align='center'><input type='submit' /></td></tr></table>";




?>


</div>
</body>
</html>

我很高兴看到你代码中的亵渎已经翻译成英语-这个函数可以工作,但我还是得到了同样的结果,如果我的第一个选择是'comunicarse',那么打开的窗口是comunicarse.php;但是当我在第二行选择custom时,打开的页面是comunicarse.php而不是custom.php。。。