Search code examples
phpfunctionphpmailer

php I want to process two functions upon submission


I want to process two functions signuf() and outcomeuf(). However, only signuf() worked, but outcomeuf() didn't: Every other referenced file worked:testmycode.page.php. What are I getting wrong?

Whenever I click submit in testmycode_part3.php, signuf() is processed - data is passed to the database afterwhich it redirected to testmycode.php however, outcomeuf does not do anything. What am I getting wrong?

testmycode_f.php:

<?php session_start();?>
<?php include "DBconnection1.php";
 use Dompdf\Dompdf; 
 use PHPMailer\PHPMailer\PHPMailer;
 use PHPMailer\PHPMailer\Exception;
?>
<?php

function signuf() {
    global $connection; 

    $username  =  test_input($_POST["username"]);
    $password  =  $_POST["password"];
    $name      =  test_input( $_POST["name"]);
    $lname     =  test_input( $_POST["lname"]);
    $email     =  test_input($_POST["email"]);
    $message   =  test_input($_POST["comment"]);
    $telephone =  test_input($_POST["telephone"]);

    $query = "INSERT INTO codetesting  (username, password, name, lastname, email, telephone, comment) 
              VALUES ('$username', '$password', '$name', '$lname', '$email', '$telephone', '$message')";   
    if (mysqli_query($connection, $query)) {         
        $_SESSION['cat_id'] = $cat_id; 
        header("location: testmycode.php");       
        die();        
    } else {   
        die("<p> There is a problem signing you up.</p>" . mysqli_error($connection));
    } 
}

function outcomeuf() {       
    global $connection;    
    require_once "dompdf/autoload.inc.php";
    require 'PHPMailer/src/PHPMailer.php';
    require 'PHPMailer/src/SMTP.php';
    require 'PHPMailer/src/Exception.php';

    //initialise dompdf class
    $document = new Dompdf();
    // get the htmlpage
    ob_start();
    require("testmycode_page.php");
    $page = ob_get_contents();
    ob_end_clean();
    $document = new Dompdf();
    $document->loadHtml($page);
    // set paper orientation
    $document->setPaper('A4', 'portrait');
    // Render the HTML as PDF
    $document->render();

    // Output the generated PDF to Browser

    //1 = download
    //0= preview
    $document->stream("test.pdf", array("Attachment"=>0));
    $fileupload = $document->output();

    // setup email 

    $message = "Please, find attached the the contract";
    $filename = "contract.pdf";
    $mail = new PHPMailer;                             

    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'xxxxx2';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'james');
    $mail->addAddress('[email protected]', 'name of receiver');     // Add a recipient
    $mail->addAddress('[email protected]', 'computer'); 

    //Attachments
    $mail->addStringAttachment($fileupload, "testmycode.pdf", base64);         // Add attachments

    //Content

    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Testmycode';
    $mail->Body    = $message;
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    if ($mail->send()) {
        echo 'Message has been sent';
    } else {
        echo 'Message could not be sent. Mailer Error';
    }
}
?>  

testmycode_part3.php

<?php include "testmycode_f.php"; ?>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") { 
    function runFuncs()   {
        signuf();
        outcomeuf();    
    }
    runFuncs();
}
?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Contact Form</title>

  </head>    
  <body>

      <div class="container">

   <!-- <h1>Contact Form</h1>

          <p> <?php echo $error; ?> </p>

          </div> -->

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" enctype="multipart/form-data">

<div class="form-group">
      <label>username</label>
      <input type="text" name="username" class="form-control" />
</div>

<div>
      <label>Password</label>
      <input type="password" name="password" class="form-control" />
</div>

<div>
      <label>Confirm Password</label>
      <input type="password" name="password1" class="form-control" />
</div>

<div class="form-group">
      <label for="name">Name</label>
      <input type="text" name="name" class="form-control" id="name" placeholder="Oladele James">
</div>

<div class="form-group">
      <label for="lname">Last Name</label>
      <input type="text" name="lname" class="form-control" id="name" placeholder="Oladele James">
</div>

<div class="form-group">
      <label for="exampleFormControlInput1">Email address</label>
      <input type="email" class="form-control" name="email" id="email" placeholder="[email protected]">
</div>

<div class="form-group">
      <label for="Telephone">Telephone</label>
      <input type="number" class="form-control" id="telephone" name="telephone" placeholder="+44 -77456 - 12134">
</div>

<div class="form-group">
      <label for="exampleFormControlInput1">Subject</label>
      <input type="text" class="form-control" name="subject" id="subject" placeholder="Title of what you want to talk about">
</div>

<div class="form-group">
      <label for="Textarea">What will you like to ask us</label>
      <textarea class="form-control" id="comment" name="comment" rows="3"></textarea>
</div>

<!--<input type="file" name="supload" /> -->

<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
</form>

      </div>
</body>
</html>

Solution

  • If signuf() runs successfully you kill the whole script with die(); This stops any further execution.

    You really don't need to use die its causing more problems for you. If you use return inside a function it'll stop all other code inside a function from running. Further your code is in a if else block; No other code will inadvertently execute.

    Also its a bad idea to return a mysql error back to the user. Use something like error_log to keep that information to yourself.

    Since you only want to run outcomeuf if you signup correctly executes it's query try only calling it inside the if else block.

    function signuf() {
      global $connection; 
    
      $username = test_input($_POST["username"]);
      $password =  $_POST["password"];
      $name =       test_input( $_POST["name"]);
      $lname =       test_input( $_POST["lname"]);
      $email    =     test_input($_POST["email"]);
      $message =           test_input($_POST["comment"]);
      $telephone =         test_input($_POST["telephone"]);
    
      $query = "INSERT INTO codetesting  (username, password, name, lastname, email, telephone, comment) VALUES ('$username', '$password', '$name', '$lname', '$email', '$telephone', '$message')";   
      if (mysqli_query($connection, $query)) {         
        $_SESSION['cat_id'] = $cat_id;
        outcomeuf();
      } else {   
        error_log(mysqli_error($connection));
        echo "<p> There is a problem signing you up.</p>";
      }
    }
    

    Now you don't need runfuncs just call signuf

    <?php
    if($_SERVER["REQUEST_METHOD"] == "POST") { 
        signuf();
    } 
    ?>