问题:
如何以每个商品名称始终唯一的方式显示表单。 我不想将表单重定向到新页面。
<?php
$sqltable = mysql_query("SELECT * from TABLE");
?>
<form action="" method="post" onsubmit="window.location.reload()">
//output truncatted
<table>
<?php
while ($row = mysql_fetch_array($sqltable))
{
echo "<tr> ";
echo "<td>" . $row[offername] . "</td>";
echo "<td>" . $row[points] . "</td>";
echo "<td><input type='submit' value='Purchase' name='offername'</td>";
}
echo "</tr> ";
?>
</table>
</form>
offername= offer1, offer2 , offer3, offer4, offerN
points = 100 , 200 , 10 , 50 , xxx
期望的输出。
当使用offer1单击提交按钮时,应将其名称更改为offer1,其他商品名称也同样如此。或者,表格应提交给各自的要约名称。
答案 0 :(得分:0)
这将创建一个输入元素作为数组。
<?php
$sqltable = mysql_query("SELECT * from TABLE");
?>
<form action="" method="post" onsubmit="window.location.reload()">
//output truncatted
<table>
<?php
while ($row = mysql_fetch_array($sqltable))
{
echo "<tr> ";
echo "<td>" . $row[offername] . "</td>";
echo "<td>" . $row[points] . "</td>";
echo "<td><input type='submit' value='Purchase' name='offername[]'></td>";
}
echo "</tr> ";
?>
</table>
</form>
//offername= offer1, offer2 , offer3, offer4, offerN
//points = 100 , 200 , 10 , 50 , xxx
if(isset($_POST['offername'])){
$offernameArr=$_POST['offername'];
foreach($offernameArr as $key => $offername ){
//your code goes here
}
}
答案 1 :(得分:0)
试试此代码
<?php
$sqltable = mysql_query("SELECT * from TABLE");
//offername= offer1, offer2 , offer3, offer4, offerN
//points = 100 , 200 , 10 , 50 , xxx
if(isset($_POST['Purchase']) && $_POST['Purchase'] == 'Purchase'){
if(isset($_POST['offername']))
echo 'Submitted offer name is '.$_POST['offername'][0];
else
echo 'No offers selected';
}
?>
<form action="" method="post" onsubmit="window.location.reload()">
<table>
<?php
while ($row = mysql_fetch_array($sqltable))
{
echo "<tr> ";
echo "<td>" . $row['offername'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "<td><input type='radio' name='offername[]' value='".$row['offername']."'></td>";
echo "</tr> ";
}
echo "<tr><td colspan='3'><input type='submit' value='Purchase' name='Purchase'></td></tr>";
?>
</table>
</form>
答案 2 :(得分:0)
//form.php
<?php
$sqltable = mysql_query("SELECT * from TABLE");
if($_GET['offername']){
$offer = $_GET['offername'];
// now you have desired offer name in $offer you can fetch data from db with given offername in where clause and use here
}
?>
<table>
<?php
while ($row = mysql_fetch_array($sqltable))
{
echo "<tr> ";
echo "<td>" . $row['offername'] . "</td>";
echo "<td>" . $row['points'] . "</td>";
echo "<td><a href='form.php?offername=".$row['offername']."'><input type='submit' value='".$row['offername']."' name='submit'></a></td>";
}
echo "</tr> ";
?>
</table>