我想在提交按钮上单击一下合并所有选项值。
就像你在下一张图片中看到的那样,我逐一选择选项值中的值。
下一张图片有最终结果的例子,我想在数据库中提交表格中给出的值。该表是动态的,我可以在数据库中删除或添加更多参数,并在页面中选择。
以下代码用于输出顶部:
<?php
$id = $_GET['menuId'];
// A sessão precisa ser iniciada em cada página diferente
mysql_connect("localhost", "root" , "") or die(mysql_error()); // connecta-se ao servidor
mysql_select_db("teste") or die(mysql_error()); // selecciona a database do server escolhido
echo "<br>";
$sql = "SELECT * FROM menu WHERE $id = menuIdPai ORDER BY menuId ";
$consulta = mysql_query($sql);
if ($consulta !=0) {
echo('<table width="500px" border="1" align="center" class="tabela" cellspacing="0">');
echo('<th class="tabela" colspan="5" align="center" bgcolor="#FFFFFF"> </th>');
echo('<tr> <td width="250px" class="ver" bgcolor="FFFFFF"> <center> Grupo/Parametro: </center> </td> <td width="350px" class="ver" bgcolor="FFFFFF"> <center> Seleccione uma opção: </center> </td> </center> ');
while ($mostrar = mysql_fetch_array($consulta)) {
$id = $mostrar['menuId'];
$utilizador = $mostrar['menuNome'];
$idfilho = $mostrar['idfilho'];
// Se a sessão não existir, inicia uma
if (!isset($_SESSION)) session_start();
// Salva os dados encontrados na sessão
$_SESSION['menuID'] = $mostrar['menuId'];
//echo 'userid:'.$_SESSION['menuID'];
//echo 'sessionid:'.session_id();
$_SESSION['menuNome'] = $mostrar['menuNome'];
{
echo("<tr>
<td align=\"center\" bgcolor='FFFFFF'>$utilizador</td>
<td align=\"center\" bgcolor='FFFFFF'>
<form method=\"post\" action=\"sucesso.php\">
<select name='sucesso'\>;
<option value=\"0\">0</option>;
<option value=\"0.05\">0.05</option>;
<option value=\"0.1\">0.1</option>;
<option value=\"0.15\">0.15</option>;
<option value=\"0.20\">0.20</option>;
<option value=\"0.25\">0.25</option>;
<option value=\"0.30\">0.30</option>;
<option value=\"0.35\">0.35</option>;
<option value=\"0.40\">0.40</option>;
<option value=\"0.45\">0.45</option>;
<option value=\"0.50\">0.50</option>;
<option value=\"0.55\">0.55</option>;
<option value=\"0.60\">0.60</option>;
<option value=\"0.65\">0.65</option>;
<option value=\"0.70\">0.70</option>;
<option value=\"0.75\">0.75</option>;
<option value=\"0.80\">0.80</option>;
<option value=\"0.85\">0.85</option>;
<option value=\"0.90\">0.90</option>;
<option value=\"0.95\">0.95</option>;
<option value=\"1.0\">1</option>;
</select>
<br><br>
</center>
</form>
");
if ($idfilho == 1)
{
echo "<td align=\"center\" bgcolor='FFFFFF'> <a href='avaliacoes2.php?menuId=".$id."'>Seleccionar</a> </td>";
}
else
{
echo "";
}
echo '</tr>';
}
}
echo"</table>";
}
echo "<br><br><br>";
echo ('<input type="submit" value="Submeter" class="link-style2" />');
echo ('<br><br>');
echo ('<input type="button" value="Voltar" class="link-style2" onclick="history.back(-1)" />');
?>`
该代码用于在数据库中插入我想要介绍的值。
这是在sucesso.php中完成的。
<?php
{
mysql_connect("localhost","root","") or die(mysql_error()); // connecta-se ao servidor
mysql_select_db("teste") or die(mysql_error()); // selecciona a database do server escolhido
$id_resp = (isset($_POST['id_resp']) ? $_POST['id_resp'] : '');
$corp_resp = (isset($_POST['corp_resp']) ? $_POST['corp_resp'] : '');
$id = $_SESSION['UtilizadorID'];
$data = (isset($_POST['data']) ? $_POST['data'] : '');
$menuId = (isset($_GET['menuId']) ? $_GET['menuId'] : '');
mysql_query("INSERT INTO respostas (id_resp, corp_resp,id,data,menuId) VALUES ('','" . $corp_resp . "','$id', NOW( ) , '$menuId')") or die("Alguma coisa correu mal durante o registo. MySQL erro: ".mysql_error());
echo '<br>';
echo '<br>';
echo "<center>AdicIonada com sucesso.</center>";
echo '<br>';
echo '<br>';
}
?>
Foreach可以用于那种情况吗?
感谢您的支持。
最诚挚的问候。
答案 0 :(得分:0)
您必须创建选择数组的名称。像这样:
<select name='sucesso[]'\>;
<option value=\"0\">0</option>;
<option value=\"0.05\">0.05</option>;
<option value=\"0.1\">0.1</option>;
<option value=\"0.15\">0.15</option>;
<option value=\"0.20\">0.20</option>;
<option value=\"0.25\">0.25</option>;
<option value=\"0.30\">0.30</option>;
<option value=\"0.35\">0.35</option>;
<option value=\"0.40\">0.40</option>;
<option value=\"0.45\">0.45</option>;
<option value=\"0.50\">0.50</option>;
<option value=\"0.55\">0.55</option>;
<option value=\"0.60\">0.60</option>;
<option value=\"0.65\">0.65</option>;
<option value=\"0.70\">0.70</option>;
<option value=\"0.75\">0.75</option>;
<option value=\"0.80\">0.80</option>;
<option value=\"0.85\">0.85</option>;
<option value=\"0.90\">0.90</option>;
<option value=\"0.95\">0.95</option>;
<option value=\"1.0\">1</option>;
</select>
现在,无论何时检查$_POST['sucesso']
,它都会显示如下内容:[0]=>0.25,[1]=>0.45,[2]=>0.65
等...
之后你可以像这样使用它:
$array=$_POST['sucesso'];
echo $array[0]; //echoes 0.25
echo $array[1]; //echoes 0.45
echo $array[2]; //echoes 0.65
您可以对这些变量采取任何措施。将它们相乘,将它们插入到数据库中,无论您想用它做什么。
是可以使用Foreach。在我的示例中,foreach使用变量$array
。