tags:

views:

129

answers:

1

Hi Everyone, I'll try to explain this as best as I can.

I have a form that accepts multiple fields, and in the end, e-mails all the fields to a specific e-mail address.

So for example, I have three text boxes, one list box and two submit buttons.

Two of the text boxes are first name, and e-mail address

The third text box is used to populate the list box. So if I enter, NIKE, into the third text box and push the first submit button. Nike will now be in the listbox.

I want to be able to populate the list box with as many entries as needed, then push the second submit button to send all information (first name, e-mail address and all items in list box).

The problem is, pushing the first submit button always triggers the e-mail sent, since I'm "POST"ing.

I have everything working right now. The third text box submits the new data to a table in mysql, and then retrieves all the data and puts it in the list box.

What's the best way to fix this scenario? Could I stop the Post variable from validating, until the second submit button is used?

Also, I'd like to avoid Javascript, thanks

+1  A: 

Make sure the two submit buttons have names. I.E: <input type="submit" name="command" value="Add"> and <input type="submit" name="command" value="Send">. Then you can use PHP to determine which one was clicked:

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: For extra credit, make the commands variables so they can be easily changed, and map them to functions...

<?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);
}
Josh
Of course all that code is untested... but the basic principle is sound.
Josh
Worked perfectly! Thanks!Do you know of an article (website) I can read, that explains your "make commands variables, map to functions"?
Idealflip
See the "bonus" section I posted. That is, the commands stored in a PHP variables, and they map to functions. (Well the other way around, but you get the idea)
Josh