doutizha7526 2014-07-24 10: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 10: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部