首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >php我想在提交时处理两个函数

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

Stack Overflow用户
提问于 2018-07-12 08:55:23
回答 1查看 158关注 0票数 0

我想处理两个函数signuf()outcomeuf()。然而,只有signuf()有效,而outcomeuf()无效:所有其他引用的文件都是worked:testmycode.page.php。我到底搞错了什么?

每当我在testmycode_part3.php中单击submit时,signuf()就会被处理--数据被传递到数据库,之后被重定向到testmycode.php。然而,outcomeuf什么也不做。我到底搞错了什么?

testmycode_f.php:

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-12 09:30:26

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

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

此外,将mysql错误返回给用户也不是一个好主意。使用像error_log这样的东西来保留这些信息。

因为您只想运行query,如果您正确地注册并执行它的outcomeuf,那么尝试只在if else块中调用它。

代码语言:javascript
复制
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

代码语言:javascript
复制
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") { 
    signuf();
} 
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51295956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档