我已经完成了电话簿所需的一切。但我唯一不能做的就是这个。
我需要这个表单有2个按钮,它有一个将新用户添加到数据库中但我需要另一个按钮,将新用户详细信息通过电子邮件发送到电子邮件地址。我需要先显示电子邮件按钮。然后在发送之后必须出现提交按钮。我花了大约2个小时浏览有关mail()和邮件功能的不同教程,但我不明白。我仍然是html和php的新手。
到目前为止,使用一个工作按钮来说明我的代码。
<h2>Add Contact</h2>
<form name="form1" action="<?=$_SERVER['PHP_SELF'];?>?mode=added" method="post">
<div align="center"><table class="searchable">
<tr><td>Extension:</td><td><div align="center">
<input type="text" name="ext" required />
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Name:</td><td><div align="center">
<input type="text" name="name" required />
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Department:</td><td><div align="center"><select name="department" required>
<option value="ADMIN">ADMIN</option>
<option value="AFTER-SALES DIRECTOR">AFTER-SALES DIRECTOR</option>
<option value="ALPINE DEALER PRINCIPAL">ALPINE DEALER PRINCIPAL</option>
<option value="AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)">AUTO ARMOUR/AUTO ENHANCE - FITMENT CENTRE (Smash and Grab)</option>
<option value="BANDIT-VW">BANDIT-VW</option>
<option value="BOOKINGS VW">BOOKINGS VW</option>
<option value="DRIVEWAY/WASHBAYS">DRIVEWAY/WASHBAYS</option>
<option value="FINANCE AND INSURANCE">FINANCE AND INSURANCE</option>
<option value="IT DEPARTMENT">IT DEPARTMENT</option>
<option value="MARKETING DEPARTMENT">MARKETING DEPARTMENT</option>
<option value="MASTER CARS">MASTER CARS</option>
<option value="MAYOR OF PINETOWN">MAYOR OF PINETOWN</option>
<option value="NEW CAR PREP DEPARTMENT">NEW CAR PREP DEPARMENT</option>
<option value="NUMBER PLATES">NUMBER PLATES</option>
<option value="PANELBEATER - EASIFIX - CAR CARE">PANELBEATER - EASIFIX - CAR CARE</option>
<option value="PARTS">PARTS</option>
<option value="PARTS DISPATCH">PARTS DISPATCH</option>
<option value="PARTS TELESALES">PARTS TELLESALES</option>
<option value="USED CAR PREP AND ORDERS">USED CAR PREP AND ORDERS</option>
<option value="VW NEW CARS ADMIN AND STOCK CONTROL">VW NEW CARS ADMIN AND STOCK CONTROL</option>
<option value="VW NEW VEHICLE SHOWROOM">VW NEW VEHICLE SHOWROOM</option>
<option value="VW SERVICE ADVISORS">VW SERVICE ADVISORS</option>
<option value="VW WORKSHOP">VW WORKSHOP</option>
<option value="VW WORKSHOP FOREMEN">VW WORKSHOP FOREMEN</option>
<option value="WARRANTY & CLAIMS">WARRANTY & CLAIMS</option>
<option value="WORKSHOP DRIVERS">WORKSHOP DRIVERS</option>
</select>
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Email:</td><td><div align="center">
<input type="text" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$" name="email" title="Example: user@company.co.za" required/>
<span class="error">* <?php echo $nameErr;?></span>
</div></td></tr>
<tr><td>Cellphone:</td><td><div align="center">
<input type="text" name="phone" />
</div></td></tr>
<tr><td colspan="2" align="center"><a href="javascript:history.go(-1);">Back</a> | <input type="submit" name="submit" id="submit" value="Add New Contact"<?php if($disable ==1){?>disabled<?php } ?>/></td></tr>
<input type="hidden" name="mode" value="added">
</table>
</div>
</form>
答案 0 :(得分:2)
您可以使用两个不同的提交按钮。看一下下面的例子:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['submit'])) {
switch ($_POST['submit']) {
case 'one':
echo "You submitted using the first button";
break;
case 'two':
echo "You submitted using the second button";
break;
}
}
?>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="submit" value='one' />
<input type="submit" name="submit" value='two' />
</form>
这是在同一页面中执行两个操作的更高级示例。这将需要多次重定向。
<?php
session_start();
if (isset($_POST['submit']) && $_POST['submit'] == 'save') {
// Insert into the database
// Add the $_POST data to a session
$_SESSION['postdata'] = $_POST;
// Unset the POST
unset($_POST);
// Redirect to same page
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} elseif (isset($_POST['submit']) && $_POST['submit'] == 'email') {
// Send your email
mail(...);
unset($_SESSION['postdata']);
// Redirect to same page and add another user
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
?>
<?php if (!isset($_SESSION['postdata'])): ?>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
<input type="text" name="username" />
<input type="submit" name="save" />
</form>
<?php else: ?>
<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>">
The user with username <?= $_SESSION['postdata']['username']; ?> has been saved.
<p>Do you want to submit an email as well?</p>
<input type="submit" name="mail" />
</form>
<?php endif; ?>
答案 1 :(得分:1)
当您将表单发布回自身时,如果您设置一个带有隐藏表单字段的标志以指示表单已提交,则会有所帮助。然后,只有在提交表单后,才能在页面上运行特定代码。这可以验证数据并显示“发送电子邮件”按钮。然后,您可以设置第二个标志,在回发时使表单显示添加用户到数据库按钮。总之,如果您要使用回发本身,则需要引入一些隐藏字段标志来更好地控制表单。
另一种方法是将表单发布到另一个脚本进行处理,这样可以更好地控制顺序按钮的显示。
答案 2 :(得分:0)
如果你给每个人一个名字,那么点击的名字将作为任何其他输入发送。
<input type="submit" name="first_button" value="Click me">
<input type="submit" name="second_button" value="Click me">
if (isset($_POST['first_button'])) {
//first button action
} else if (isset($_POST['second_button'])) {
//second buttonaction
} else {
//no button pressed
}
见here