faqts : Computers : Programming : Languages : PHP : Common Problems : Forms and User Input : URLs and Post or Get Methods

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

10 of 16 people (63%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

Is it possible to have one form send its information to two places, such as a payment processor and an e-mail address?

May 11th, 2001 17:12
Philip Olson, Jon Thomas,


Yes.  You can do anything with the data.  For example :
  <form action="form_processor.php" method="POST">
    <br>Email :<input type="text" name="email">
    <br>Name  :<input type="text" name="name">
    <br><input type="submit" name="submit" value="submit me!">
  </form>
That's our form.  It'll submit variables 'email' and 'name' to our form 
processor via the POST method.  Let's process it, here's a breakdown of 
what we'll be doing today :
  1a. Get data from predefined php variable HTTP_POST_VARS
  1b. We'll strip slashes (see magic_quotes_gpc setting info)
  1c. Eliminate beginning and trailing whitespace with trim() :
  2a. Make string $name to lowercase with strtolower()
  2b  UpperCase Words In String $name With ucwords()
  3a. Create SQL statement
  3b. Use addslashes() on $name (O\'reilly) - make it db friendly. 
  3c. Insert data into MySQL table 'tablename'
  4a. If data makes it into database then $email $name.
  4b. \n adds linebreaks, variables such as $name are interpreted ...
  4c. Email our custom message
  5. Else an error existed as data did not make it into db
  <?php
    // form_processor.php
    // 1. get post data from form, stripslashes in case
    //    setting magic quotes are on.
    $email = trim(stripslashes( $HTTP_POST_VARS['email']) );
    $name  = trim(stripslashes( $HTTP_POST_VARS['name']) );
    // 2. mess with data (you should validate/check your data ...)
    $name = ucwords(strtolower($name));
    // 3. create query, put data into a database (store data)
    $sql    = "INSERT INTO tablename (id,email,name)
               VALUES (NULL,'$email','". addslashes($name) ."')";
    // 4. if our result holds true (data went into db) then email them.
    //    essentially, mysql_query return true, not false.
    if ($result = mysql_query($sql)) {
    // 5. create a custom message, email it to $name
      $mesg  = "Hi $name!\n\nGlad to see you like this example!\n\n";
      $mesg .= "Well, \n\n\t Have A Nice Day!\n\nregards,\n-me";
      mail($email, "Hi $name", $mesg, 'From: me@example.com');
      echo 'success: data made it into db!';
    // 6. else we have no result, show error.  although you'll want
    //    to hide such info from user.
    } else {
	  echo 'data did not go into db : '. mysql_error();
    }
  ?>
The above is a simple example as yes, one can do a lot to/with the 
data, the possibilities are endless.  The manual has more information 
on the functions used above, plus more examples and different ways to 
do just about everything.  It's also important to know about as the 
settings vary from server to server :
  http://www.php.net/manual/function.get-magic-quotes-gpc.php
  http://www.php.net/manual/en/configuration.php#ini.register-globals
Good luck! :-)