dongliaojing0554 2015-03-10 16:59
浏览 42

关闭if if语句中的语句也可以解决数组问题

Thank you in advance for looking at my code and helping me with the problem i am having. I am trying to build a simple contact form. The thing is when i hit my submit button everything that gets directed to the next page displays everything into an array. How can i go about fixing this?

Also, my if statement is giving me a hard time how can i go about fixing this? Am i using the wrong type of arrays?

contact.php

<?php

session_start();

require_once 'PHPMailerAutoload.php';

$errors = [];

if(isset($_POST['name'], $_POST['email'], $_POST['message'])){

$fields = [
    'name' => $_POST['name'],
    'email' => $_POST['email'],
    'message' => $_POST['message']
];

foreach($fields as $field => $data) {
    if(empty($data)){
        $errors[] = 'The ' . $field . ' field is required.';
    }   
}
if(empty($errors)){

    $m = new PHPMailer;

    $m->isSMTP();
    $m->SMTPAuth = true;


    $m->Host = 'smtp.gmail.com';
    $m->Username = 'none@gmail.com';
    $m->Host = 'Pa$$w0rd1';
    $m->SMTPSecure = 'ssl';
    $m->Port = '465';

    $m->isHTML();

    $m->Subject = 'Contact form submitted';

    $m->Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';

    $m->From = 'Contact';

    $m->AddAddress('none@gmail.com', 'name');

    if($m-> send()){

        header('Location: thankyou.php');
        die();
    } else {

        $errors[] = 'Sorry, could not send email. Try again late.';
    }

}

} else {
$errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: contactform.php');

?>

contactform.php

 <?php

 session_start();

 require_once 'security.php';

 $errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
 $fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];

 ?>

 <!DOCTYPE html>

 <html>
 <head>
 <meta charset="utf-8">
 <title>Contact Us</title>

 <link rel="stylesheet" href="css/website.css">
 </head>
 <center><body>
    <div class="contact">

    <?php if(!empty($errors)): ?>
        <div class="panel">
            <ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
        </div>
    <?php endif; ?>
    <form action="contact.php" method="post">
        <label>
            Your name *
            <input type="text" name="name" autocomplete="off">
        </label>
        <br><label>
            Your email address *
            <input type="text" name="email" autocomplete="off">
        </label>
        <br><label>
            Your message or concern *
            <textarea name="message" rows="8"></textarea>
        </label>

        <input type="submit" value="Send">

        <p class="muted">* means a required field</p>
        </form>
    </div>
</body></center>
</html>

 <?php

 unset($_SESSION['errors']);
 unset($_SESSION['fields']);

 ?>

This is the if statement i am referring to in the above code

  <?php if(!empty($errors)); ?>
        <div class="panel">
            <ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
        </div>
  <?php endif ?>

Please Please Please help me with this.

  • 写回答

2条回答 默认 最新

  • duanbu1421 2015-03-10 17:05
    关注

    Try this:

    <?php if(!empty($errors)): ?>
      <div class="panel">
        <ul><li><?php echo implode(('</li><li>'), $errors); ?></li></ul>
      </div>
    <?php endif; ?>
    

    The proper way to use if ... endif is:

    <?php if (condition): ?>
        // Things to do when condition is true
    <?php endif; ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号