php我想在提交时处理两个功能

时间:2018-07-12 00:55:23

标签: php function phpmailer

我要处理两个功能signuf()outcomeuf()。但是,仅signuf()有效,而resultuf()无效:其他所有引用的文件均有效:testmycode.page.php。 我怎么了?

每当我单击testmycode_part3.php中的Submit时,都会处理signuf()-数据传递到数据库,然后重定向到testmycode.php,但是resultuf不执行任何操作。我怎么了?

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 = 'xxxxx@gmail.com';                 // 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('wxxxx@gmail.com', 'james');
    $mail->addAddress('olxxxxe@gmail.com', 'name of receiver');     // Add a recipient
    $mail->addAddress('axxxx@gmail.com', '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="name@example.com">
</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>

1 个答案:

答案 0 :(得分:0)

如果signuf()成功运行,您将用die();杀死整个脚本,这将停止任何进一步的执行。

您真的不需要使用die它会给您带来更多问题。如果在函数中使用return,它将停止运行函数中的所有其他代码。此外,您的代码位于if else块中;没有其他代码会无意中执行。

mysql错误返回给用户也是一个坏主意。使用error_log之类的东西将这些信息保留给自己。

由于您只想在注册正确执行后运行outcomeuf,请尝试仅在if else块内调用它。

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

现在您不需要runfuncs,只需致电signuf

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