【发布时间】:2014-07-18 14:37:09
【问题描述】:
我想知道您是否可以拥有一个包含多个操作的 HTML 表单。好吧,我的意思不是指定多个action="" 属性。假设我有一个简单的表格:
form.html
<form action="action.php" method="post">
<label><input type="radio" value="r1" id="r1" name="r" />Insert in table1</label><br/>
<label><input type="radio" value="r2" id="r2" name="r" />Insert in table2</label><br/>
<label>Value 1<input type="text" id="t1" name="t1" /></label><br/>
<label>Value 2<input type="text" id="t2" name="t2" /></label><br/>
<label><input type="submit" value="Submit" /></label><br/>
</form>
action.php
$con = mysqli_connect("example.com","user","pwd","db");
$r = $_POST["r"];
$t1 = $_POST["t1"];
$t2 = $_POST["t2"];
switch($r):
case 'r1':
if($q = $con -> prepare("INSERT INTO table1 VALUES (?, ?)")):
$q -> bind_param("ss",$t1,$t2);
if($q -> execute()):
echo 'Successfully updated table1.';
$q -> close();
endif;
endif;
break;
case 'r2':
if($q = $con -> prepare("INSERT INTO table2 VALUES (?, ?)")):
$q -> bind_param("ss",$t1,$t2);
if($q -> execute()):
echo 'Successfully updated table2.';
$q -> close();
endif;
endif;
break;
endswitch;
mysqli_close($con);
所以我根据无线电输入更新table1 或table2,并使用用户提交的值。请注意,我已经阻止了 SQL 注入。所以我的问题是,这是一种好的做法,还是我应该制作两个表格,每个表格都有不同的action=""(使用query string 在它们之间切换,比如/main.php?form=form1 或/main.php?form=form2)?
【问题讨论】: