dqndc26628 2014-08-22 00:42
浏览 24
已采纳

除非填写所有字段,否则联系表格不会发送,需要字段是否可选?

This is the code for my contact form, its pretty short, the site uses java and php, if that matters to mention. Anyways, "Email" and "Name" fields need to be optional, but it will not send unless they are filled, what needs to be changed down below? (Im not a good php guy, so example or copying it in your answer will help) thanks!

<?php
error_reporting(0);
$from = 'NewFeedBack@ReviewBM.com';
$to = 'Mike@gmail.com';
if ($_POST['sender_subject'] and $_POST['sender_message'] and $_POST['sender_name'] and $_POST['sender_email']) {
    $message = "Complainers email: " . $_POST['sender_email'] . "
Complainers name: " . $_POST['sender_name'] . "
Complainers message: " . $_POST['sender_message'] . "
";
    $headers .= "From:" . $from;
    mail($to, $_POST['sender_subject'], $message, $headers);
    $data = array('status'=>0);
    echo(json_encode($data));
}else{
    $data = array('status'=>1);
    echo(json_encode($data));
}
return;
  • 写回答

1条回答 默认 最新

  • douqianrou9079 2014-08-22 00:59
    关注

    You can just remove and $_POST['sender_name'] and $_POST['sender_email']

    and do:

    if($_POST['sender_subject'] && $_POST['sender_message']){
    

    Or, you can also use:

    if(!empty($_POST['sender_subject']) && !empty($_POST['sender_message'])){
    

    !empty() checks if the fields were not empty.


    Sidenote: && has precedence over AND, just a quick FYI.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥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 支付宝网页转账系统不识别账号