我会尽力解释这一点。
我有一个接受多个字段的表单,最后将所有字段通过电子邮件发送到特定的电子邮件地址。
例如,我有三个文本框,一个列表框和两个提交按钮。
其中两个文本框是名字和电子邮件地址
第三个文本框用于填充列表框。所以如果我输入NIKE,进入第三个文本框并按下第一个提交按钮。耐克现在将出现在列表框中。
我希望能够根据需要使用尽可能多的条目填充列表框,然后按第二个提交按钮发送所有信息(名字,电子邮件地址和列表框中的所有项目)。
问题是,推送第一个提交按钮总是会触发发送的电子邮件,因为我正在“发布”。
我现在一切正常。第三个文本框将新数据提交到mysql中的表,然后检索所有数据并将其放入列表框中。
修复此方案的最佳方法是什么?我可以阻止Post变量验证,直到使用第二个提交按钮吗?
另外,我想避免使用Javascript,谢谢
答案 0 :(得分:1)
确保两个提交按钮具有名称。 I.E:<input type="submit" name="command" value="Add">
和<input type="submit" name="command" value="Send">
。然后,您可以使用PHP来确定单击了哪一个:
if($_REQUEST['command'] == 'Add')
{
// Code to add the item to the list box here
}
elseif($_REQUEST['command'] == 'Send')
{
// Code to send the email here...
}
BONUS:要获得额外的功劳,请创建命令变量,以便轻松更改这些变量,并将其映射到函数...
<?php
$commands = array(
'doSendEmail' => 'Send Email',
'doAddOption' => 'Add Option',
);
function doSendEmail()
{
// your email sending code here...
}
function doAddOption()
{
// your option adding code here...
}
function printForm()
{
global $commands;
?>
Name: <input type="text" name="name"><br>
Email: <input type="text" name="name"><br>
<input type="text" name="add">
<input type="submit" name="command" value="<?= $commands['doAddOption'] ?>">
<select>
<?php /* some code here */ ?>
</select>
<input type="submit" name="command" value="<?= $commands['doSendEmail'] ?>">
<?php
}
if(isset($_REQUEST['command']))
{
$function = array_search($_REQUEST['command'],$commands);
if($function !== -1)
call_user_func($function);
}