doutizha7526 2014-07-24 18:39
浏览 48
已采纳

如果!isset($ _ POST ['submit'])或不是,使表单显示不同的内容

I am trying to build a contact form based on the Zendesk API. What I'm trying to achieve is simple. Display the form if the $_POST['submit'] is not set (if a form is not submitted) and a success message if the $_POST['submit'] is set. I'm trying with the following code but it's not working. What am I doing wrong?

        <?php
        define("ZDAPIKEY", "");
        define("ZDUSER", "");
        define("ZDURL", "");

        function curlWrap($url, $json, $action)
        {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
            curl_setopt($ch, CURLOPT_URL, ZDURL.$url);
            curl_setopt($ch, CURLOPT_USERPWD, ZDUSER."/token:".ZDAPIKEY);
            switch($action){
                case "POST":
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
                    break;
                case "GET":
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
                    break;
                default:
                    break;
            }
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
            curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            $output = curl_exec($ch);
            curl_close($ch);
            $decoded = json_decode($output);
            return $decoded;
        }
        if (!isset($_POST['submit'])) { 
        ?>
        <html>
        <div id="box_form">
        <form id="zFormer" method="POST" action="contact.php" name="former">
        <p>
        Your Name:<input type="text" value="James Duh" name="z_name">
        </p>
        <p>
        Your Email Address: <input type="text" value="duh@domain.com" name="z_requester">
        </p>
        <p>
        Subject: <input type="text" value="My Subject Here" name="z_subject">
        </p>
        <p>
        Description: <textarea name="z_description">My Description Here</textarea>
        </p>
        <p>
        <input type="submit" value="submit" id="submitter">
        </p>
        </form>
        </div>
        </html>
        <?
        } elseif  (isset($_POST['submit'])) { 

        foreach($_POST as $key => $value){
            if(preg_match('/^z_/i',$key)){
                $arr[strip_tags($key)] = strip_tags($value);
            }
        }
        $create = json_encode(array('ticket' => array('subject' => $arr['z_subject'], 'comment' => array( "body"=> $arr['z_description']), 'requester' => array('name' => $arr['z_name'], 'email' => $arr['z_requester']))));
        $return = curlWrap("/tickets.json", $create, "POST");

        ?>
        The form has been submitted!
        <? 
        }
        ?>
  • 写回答

1条回答 默认 最新

  • dongyinpan9284 2014-07-24 18:46
    关注

    Your submit button isn't named

    <input type="submit" value="submit" id="submitter">
    

    do

    <input type="submit" value="submit" id="submitter" name="submit">
    

    since both of your conditional statements rely on it

    if (!isset($_POST['submit']))
    

    and

    elseif  (isset($_POST['submit']))
    

    Add error reporting to the top of your file(s) right after your opening <?php tag:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    that would've yielded an undefined index error.

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。