PHP OOP中未定义的索引错误消息

时间:2017-09-25 19:16:25

标签: php oop

我正在使用PHP OOP编写的CMS项目。对于这个项目,我想添加一个管理员可以添加Telegram Bot并管理它们的部分。所以我在页面telegram_section.php中创建了这个表单:

    <?php 
if(isset($_POST['submit'])){
    $db = new Connection();
    $connection = $db->dbConnect();

    $token = $_POST['token'];
    $cat = $_POST['cat'];
    $ads = $_POST['ads'];
    $key = $_POST['keyboard'];

    $notice = array();

    if(!empty($token)&&!empty($cat)&&!empty($ads))
    {
        for ($i=0; $i < count($this->_ads); $i++)
        {
            for ($j=0; $j < count($this->_key);$j++)
            {
                $tel = new Telegram($token, $cat, $ads[$i], $key[$j]);
                $notice[] = $tel->saveToDb($db); 
            }
        }
    }
}
?>
<div class='content-wrapper'>
    <section class='content-header'>
        <h1>
            Add New Telegram Account
            <small>You can add a new Telegram channel here</small>
        </h1>
        <ol class='breadcrumb'>
            <li class='active'>telegram.php</li>
        </ol>
    </section>
    <?php 
    if($dataSet->GetLevel()==1)
    { echo "
        <section class='content'>
            <div class='row'>
                <div class='col-md-6'>
                    <div class='box box-primary'>
                        <div class='box-header with-border'>
                            <h3 class='box-title'>Required Information</h3>
                        </div>
                        ";
                        if(isset($notice['success_message'])){
                            echo "
                                <div class='alert alert-success'>
                                    <strong>Hey!</strong> ".$notice['success_message'].".
                                </div>
                            ";
                        }
                        echo "
                        <form role='form' method='POST' action=''> 
                            <div class='box-body'>
                                <div class='form-group'>
                                    <label>Token Number</label>
                                    <input type='text' class='form-control' placeholder='Enter token' name='token' required>
                                    <a href='#' style='color:purple;'>Having problem while getting token</a>
                                </div>
                                <div class='form-group'>
                                    <label>Select Category</label>
                                    <select name='cat' class='form-control'>
                                        <option value='empty'>---</option>
                                        <option value='technology'>Technology</option>
                                        <option value='4fun'>Game & Fun</option>
                                        <option value='news'>News</option>
                                        <option value='tools'>Tools</option>
                                        <option value='learning'>Learning</option>
                                        <option value='traditional'>Traditional</option>
                                        <option value='media'>Media</option>
                                    </select>
                                </div>
                                <div class='form-group'>
                                    <div class='radio'>
                                        <label>
                                            <input type='radio' name='ads' id='optionsRadios1' value='on' checked>
                                            Set ads on</br>
                                            <input type='radio' name='ads' id='optionsRadios1' value='off'>
                                            Set ads off
                                        </label>
                                    </div>
                                </div>
                                <div class='form-group'>
                                    <div class='checkbox'>
                                        <label>
                                            <input type='checkbox' name='keyboard' value='with_keyboard'>
                                            Use dedicated keyboard for this bot
                                        </label></br>
                                        <label>
                                            <input type='checkbox' name='keyboard' value='without_keyboard'>
                                            Show keyboard at groups
                                        </label></br>
                                        <label>
                                            <input type='checkbox' name='answer' value='answer_messages_chats' checked>
                                            In private chats, just anwser the pre defined messages
                                        </label></br>
                                        <label>
                                            <input type='checkbox' name='answer' value='answer_messages_groups' checked>
                                            In groups, just answer the pre defined messages
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class='box-footer'>
                                Visit <a href='https://zite.pouyavagefi.com/documentation/telegram.php'>Telegram</a> Social Media Documentation.
                            </div>
                            <div class='box-footer'>
                                <button name='submit' type='submit' class='btn btn-primary'>Submit</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </section> "; 
    }else{
        echo "
        <section class='content'>
            <div class='alert alert-warning'>
                <strong>Access Denied!</strong> You don\'t have permission to access this page.
            </div> 
        </section> "; 
    }
    ?>
</div>

这是我在上一页中调用的类Telegram.class.php

    <?php 
class Telegram
{   
    protected $notice = '';
    private $_token;
    private $_cat;
    private $_ads;
    private $_key;

    public function __construct($token, $cat, $ads, $key)
    {
        $this->_token = $token;
        $this->_cat = $cat;
        $this->_ads = $ads;
        $this->_key = $key;
    }
    public function saveToDb(PDO $con)
    {
        $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads[$i].", ".$key[$i].")");
        $new->bindParam(1,$this->_token);
        $new->bindParam(2,$this->_cat);
        $new->bindParam(3,$this->_ads);
        $new->bindParam(4,$this->_key);
        $new->execute();
        $this->notice['success_message'] = "New Telegram Channel was successfully added";
        return $this->notice;
    }
    public function getNotice()
    {
        return $this->notice;
    }

    public function getToken()
    {
        return $this->_token;
    }

    public function getCat()
    {
        return $this->_cat;
    }

    public function getAds()
    {
        return $this->_ads;
    }

    public function getKey()
    {
        return $this->_key;
    }
}
?>

现在的问题是,每当我尝试提交表单时,都会收到以下错误消息:

未定义的索引:第10行的telegram_section.php中的键盘

以下是telegram_section.php的第10行:

  

$ key = $ _POST [&#39; keyboard&#39;];

获取此用户输入:

<label>
<input type='checkbox' name='keyboard' value='with_keyboard'>
Use dedicated keyboard for this bot
</label></br>
<label>
    <input type='checkbox' name='keyboard' value='without_keyboard'>
    Show keyboard at groups
</label></br>

所以如果你知道如何解决这个问题,请告诉我...提前谢谢!

1 个答案:

答案 0 :(得分:0)

该错误显示您在POST数组中没有该索引,为什么?,因为没有收到复选框,因为帖子中的“false”是这样的。试试这一行:

$ key =!empty($ _ POST ['keyboard'])? $ _POST ['keyboard']:'';

此外,不要“回显”所有表单,只需将其打印为普通HTML。

最后,如果您在输入之前没有显示任何错误标记,请检查您的“echo”,以防您将其标记为已选中。

PS:尝试使用 observation<- hotel %>% + inner_join(dat, by = "word") %>% + count(Scoree) ,以便在尝试调试脚本时检查阵列中的哪些索引。