-2

I have completed everything i need for my phonebook. But the only thing i cant get right is this.

I need this form to have 2 buttons, it has the one which adds the new user into the database but i need another button which emails the new user details to a email address.I need the email button to show first. then after its been sent the submit button must appear. I have spent about 2 hours looking through different tutorials about mail() and mail functions but i dont understand it. Im still pretty new to html and php.

Heres my code so far with the one working button.

<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>
Eternal J
  • 51
  • 7

3 Answers3

2

You can use two different submit buttons. Take a look at the following example:

<?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>

Here is a more advanced example of doing two actions in the same page. This will require multiple redirects.

<?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; ?>
Peter
  • 8,776
  • 6
  • 62
  • 95
1

When you post a form back to itself, it helps if you set a flag with a hidden form field to indicate the form has been submitted. You can then run specific code on the page only once the form has been submitted. This can validate the data and show a Send E-mail button. You can then set a second flag that on postback makes the form display an add user to database button. In summary, if you are going to use posting back on itself, you need to introduce some hidden field flags to control the form better.

The alternative is to make the form post to another script(s) for processing, and that gives you better control for the sequential button display.

Atrix
  • 299
  • 2
  • 6
0

If you give each one a name, the clicked one will be sent through as any other input.

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

as seen here

Angel ofDemons
  • 1,267
  • 8
  • 13