duanjiu3486 2017-10-03 09:33
浏览 34
已采纳

无法完成简单的联系表格[关闭]

I am a novice coder currently working on my very first php project. A website has a simple php contactform I'm trying to complete but I keep running into unexpected errors. I have tried many things now that I found on Stackoverflow and solved some errors myself, but I am at a loss right now. I hope someone would review my code to help me see what I'm missing here. Any help to get me back on track is much appreciated.

edit: tried to make this post more on topic

What I want this script to do:
- make name, email and message required fields
- make form secure against malicious intent. That is why I added the check_input function, following Securing a contact form script

Complete code as of now:

<?php

$name = isset($_POST['name']) ? check_input($_POST['name']) : ""; 
$email = isset($_POST['email']) ? check_input($_POST['email']) : ""; 
$company = isset($_POST['company']) ? check_input($_POST['company']) : ""; 
$message = isset($_POST['message']) ? check_input($_POST['message']) : ""; 
$from = 'From: ZZZ Reactieformulier';
$to = 'emailadres@mailserver.nl';
$subject = 'Reactie via website';


$body = "Afzender: $name
 E-mailadres: $email
 Bedrijfsnaam: $company
 
Bericht: $message";

function check_input($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}


if ($_POST['submit']) {
        if ($name !='' && $email !='' && $message !='') {
            if (mail ($to, $subject, $body, $from)) {
                echo '<p>Uw bericht is succesvol verzonden!</p>';
            } else {
                echo '<p>Er is iets misgegaan, probeer het alstublieft 
                         opnieuw</p>';
            }
        } else {
            echo '</p>De velden Naam, E-mail en Bericht zijn verplicht</p>';
        }

    }   
?>
  • 写回答

1条回答 默认 最新

  • duanji1902 2017-10-03 09:36
    关注

    Your ternary operator are wrong :

    $name = isset($_POST['name']) ? check_input($_POST['name']);
    

    should be

    $name = isset($_POST['name']) ? check_input($_POST['name']) : "";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码