Hey

Im currently working on a users database, witch will allow user to enter some data, and store it in a database, this works fine.
But i desided that i would be a nice touch to have the script send them a mail when they submit the form, but then i ran into a problem, how the heck, do i allow the script to have 2 actions ?

i have

<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">

and

<FORM method="POST" action="mail.php">

I want both actions excuted when i press the submit button.

Is this possible, and in that case, how ?
Thanks in advance

    You combine the actions into 1 script instead of two. When script 1 finishes adding stuff to the database, have the code from script 2 work its magic (the emailing) and you're all set.

      Thanks for the reply, it sounds logic enough that i should combine the 2 scripts to one, and tried, but some how i cant get both functions to work together, i know they work else.
      the 2 files are theese:

      <?
      session_start(); 
      include("database.php");
      
      /**
       * Returns true if the username has been taken
       * by another user, false otherwise.
       */
      function usernameTaken($username){
         global $conn;
         if(!get_magic_quotes_gpc()){
            $username = addslashes($username);
         }
         $q = "select username from users where username = '$username'";
         $result = mysql_query($q,$conn);
         return (mysql_numrows($result) > 0);
      }
      
      /**
       * Inserts the given (username, password) pair
       * into the database. Returns true on success,
       * false otherwise.
       */
      function addNewUser($rname, $username, $password, $email, $contry, $age){
         global $conn;
         $q = "INSERT INTO users VALUES ('$username', '$rname', '$password', '$email', '$contry', '$age')";
         return mysql_query($q,$conn);
      }
      
      /**
       * Displays the appropriate message to the user
       * after the registration attempt. It displays a 
       * success or failure status depending on a
       * session variable set during registration.
       */
      function displayStatus(){
         $uname = $_SESSION['reguname'];
         if($_SESSION['regresult']){
      ?>
      
      <h1>Registered!</h1>
      <p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>
      
      <?
         }
         else{
      ?>
      
      <h1>Registration Failed</h1>
      <p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
      Please try again at a later time.</p>
      
      <?
         }
         unset($_SESSION['reguname']);
         unset($_SESSION['registered']);
         unset($_SESSION['regresult']);
      }
      
      if(isset($_SESSION['registered'])){
      /**
       * This is the page that will be displayed after the
       * registration has been attempted.
       */
      ?>
      
      <html>
      <title>Registration Page</title>
      <body>
      
      <? displayStatus(); ?>
      </body>
      </html>
      
      <?
         return;
      }
      
      /**
       * Determines whether or not to show to sign-up form
       * based on whether the form has been submitted, if it
       * has, check the database for consistency and create
       * the new account.
       */
      if(isset($_POST['subjoin'])){
         /* Make sure all fields were entered */
         if(!$_POST['user'] || !$_POST['pass']){
            die('You didn\'t fill in a required field.');
         }
      
         /* Spruce up username, check length */
         $_POST['user'] = trim($_POST['user']);
         if(strlen($_POST['user']) > 30){
            die("Sorry, the username is longer than 30 characters, please shorten it.");
         }
      
         /* Check if username is already in use */
         if(usernameTaken($_POST['user'])){
            $use = $_POST['user'];
            die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one.");
         }
      
         if(!$_POST['email']){
            die('You didn\'t fill in the email field');
         }
      
        if(!$_POST['contry']){
            die('You didn\'t fill in the contry field');
         }
      
         /* Add the new account to the database */
         $md5pass = md5($_POST['pass']);
         $_SESSION['reguname'] = $_POST['user'];
         $email = $_POST['email'];
         $contry = $_POST['contry'];
         $age = $_POST['age'];
         $_SESSION['regresult'] = addNewUser($rname, $_POST['user'], $md5pass, $email, $contry, $age);
         $_SESSION['registered'] = true;
         echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
         return;
         }
      else{
      /**
       * This is the page with the sign-up form, the names
       * of the input fields are important and should not
       * be changed.
       */
      ?>
      
      <html>
      <title>Registration Page</title>
      <body>
      <h1>Register</h1>
      <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
      <table align="left" border="0" cellspacing="0" cellpadding="3">
      <tr><td>Realname:</td><td><input type="text" name="rname" maxlength="30"></td></tr>
      <tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
      <tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
      <tr><td>Email:</td><td><input type="text" name="email" maxlength="50"></td></tr>
      <tr><td>Contry:</td><td><input type="text" name="contry" maxlength="30"></td></tr>
      <tr><td>Age:</td><td><input type="text" name="age" maxlength="10"></td></tr>
      <tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
      </table>
      </form>
      </body>
      </html>
      <?
      }
      ?>
      

      and the mail

      <?
      $nu = time();
      $vandaag = date("d. M. Y.", $nu);
      
      $message = "Welcome this is a test mail";
      
        $msg = "Hello ";
        $msg .= $username;
        $msg .= "\n\n";
        $msg .= "$message"; 
        $msg .= "\n\n"; 
      
      
        $recipient = "$email"; 
        $subject = "Welcome to the test site"; 
      
      
        $mailheaders = "From: Maller \n"; 
        $mailheaders .= "Reply-To: [email]support@domain.com[/email]\n\n"; 
      
        mail($recipient, $subject, $msg, $message, $mailheaders);
      
      ?>
      

      any ideas, on how to combine it to 1 ?

        alright, on the page that you have your form, youre going to do something like

        <form method="POST" action="form_processor.php">
        .
        .
        .
        .
        <input type="submit" name="Save" value="Save">
        <input type="submit" name="Email" value="Email">

        </form>

        then, on your form_processor.php you will have something like this

        <?php
        if($_POST['Save'] && $_POST['Save'] == "Save")
        {
           // go through the database saving code
        } 
        if($_POST['Email'] && $_POST['Email'] == "Email")
        {
           // run through the mail code
        }
        

        It's actually been awhile since I've done multibuttoned forms, so the code might need a bit of tweaking. But that is the general idea

          The idea given above is basic and can work for you.

          Well on the otherside you can handle the Forms using Javascript But Javascript is client end technology and one can easly bypass it so it would be better that you use server side technology.

          Well In javascript you have to write two Function and have to change the <input type="Submit"> to <input type="button">

          Than onclick event you have to call the function. And that function will tell which action to perform

          
          function form1(){
           document.formname.action = "page1.php";
           formname.submit();
          }
          
          
          function form2(){
           document.formname.action = "page2.php";
           formname.submit();
          }
          

          I dint test this code though it will work.

          Cheers,
          yousaf FAYYAZ.

            Thanks for all your help, i figured it out by combining the 2 scripts into 1 🙂

              2 years later

              I am having the same problem with you
              I have created a php form to add data in mysql
              the same time I would like to send an email with the data to the user
              You said tha you found the solution
              Could you help
              Attached you can find the php form

              Thanks alot
              I have to mention that I am absolute begginner in php and mysql

                Write a Reply...