2010-05-05 53 views
0

我会尽我所能解释这一点。php在不提交表单的情况下动态填充列表框

我有一个表单接受多个字段,并最终通过电子邮件将所有字段发送到特定的电子邮件地址。

因此,例如,我有三个文本框,一个列表框和两个提交按钮。

的文本框有两个是名字和电子邮件地址

第三个文本框是用来填充列表框中。所以如果我在第三个文本框中输入NIKE,并推送第一个提交按钮。耐克现在将在列表框中。

我希望能够根据需要填充尽可能多的条目,然后按第二个提交按钮发送所有信息(名字,电子邮件地址和列表框中的所有项目)。

问题是,推第一个提交按钮总是触发发送的电子邮件,因为我是“POST”。

我现在一切正常。第三个文本框将新数据提交给mysql中的表格,然后检索所有数据并将其放入列表框中。

修复这种情况的最佳方法是什么?我可以停止验证Post变量,直到使用第二个提交按钮?

另外,我想避免使用Javascript,感谢

回答

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... 
} 

奖金:对于额外的信用,使命令变量,使他们能够很容易地改变,并将它们映射到功能...

<?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); 
} 
+0

当然,所有的代码都是未经测试的......但基本原理是健全的。 – Josh 2010-05-05 21:56:41

+0

工作完美!谢谢! 你知道一篇我可以阅读的文章(网站),它解释了你的“make命令变量,映射到函数”吗? – Kukoy 2010-05-05 22:22:17

+0

请参阅我发布的“奖励”部分。也就是说,这些命令存储在一个PHP变量中,并且它们映射到函数。 (反过来,但你明白了) – Josh 2010-05-05 22:23:53

相关问题