drjtua5953 2015-11-06 19:21
浏览 23
已采纳

重定向页面是用户提交空字段

I'm breaking my head here. First time toying with PHP. I have a form with several fields that are required. If a user tries to send an empty field, it needs to be caught. If I type a space, the form goes through. Any help will be truly appreciated.

This is the code for one field

if( isset($_POST['first_name']) and !empty($_POST['first_name']) and trim($_POST['first_name']) != '' ) $first_name = filter_var($_POST['first_name'], FILTER_SANITIZE_MAGIC_QUOTES);
else {
  $redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList' and header ( "Location: $redirect" );
}
  • 写回答

2条回答 默认 最新

  • douguanci9158 2015-11-06 21:09
    关注

    You are setting $redirect to what the expression

    $redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList' and header ( "Location: $redirect" );
    

    evaluates to. When you do

    header ( "Location: $redirect" );
    

    $redirect has not been set yet.

    For example:

    php > $x = 'a' && 'a' . $x;
    PHP Notice:  Undefined variable: x in php shell code on line 1
    PHP Stack trace:
    PHP   1. {main}() php shell code:0
    

    You need to just break it up:

    $redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList';
    header("Location: $redirect");
    

    or make it evaluate the expression before the and first:

    ($redirect = SITE_URL . substr($section, -4) . '/' . substr($section, 0, -4) . '/' . $referPage . 'first_name=' . $first_name . '&last_name=' . $last_name . '&email=' . $email . '&phone' . $phone . '&country=' . $country . '#mailingList') and header ( "Location: $redirect" );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计