php我想在提交时处理两个函数

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (58)

我想处理两个函数signuf()和outcomeuf()。但是,只有signuf()工作,但outcomeuf()没有:每个其他引用的文件工作:testmycode.page.php。我错了什么?

每当我在testmycode_part3.php中单击提交时,会处理signuf() - 数据被传递到数据库之后,它会重定向到testmycode.php但是,outcomeuf没有做任何事情。我错了什么?

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 - document name

<?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>
提问于
用户回答回答于

如果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();
} 
?>

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励