使用CKEditor发布文本区域的值

时间:2013-11-16 16:04:32

标签: php sql ckeditor

我正在尝试发布通过CKEditor保存的数据,但总是会出现“你没有填写所有必填字段!”。它在文档中说,字符串需要与您要替换的字符串相同。但是当字符串是'description'时,编辑器没有出现,只有当它被命名为'editor1'时才显示在浏览器中的表单上,如下所示:

CKEDITOR.replace( 'editor1', {

但它需要描述。因为当我在下面的代码中显示字符串'description'时。它没有出现。表单需要提交由编辑器输入和输出的html,但我似乎无法得到它。

这是我的代码:

    <?php 
    /* A few words about this submission handler:
    This handler requires following info: name, description, paypal (must be email!), first-name, last-name, thumbnail_file (must be FILE!), preview_file (MUST be a file!), main_file (MUST be a file!)
    - thumbnail file must be 80x80px and JPEG/PNG format if not, its declined
    - preview file must be 820x30px and JPEG format if not, its declined
    - Main file must be a ZIP format
    - Demo URL is pinged if there is no web site on the url, we decline form (optional field)
    - Price must be numeric if not, its declined (optional field)
    - Category must be in database if not, its declined

    Processing of this form might be slow because it does a lot of checking.
    */


    // Options
    $returnMail = 'server@rocketraiser.com';            // This is where email will be sent from
    $adminMail  = 'enquiries@harrisonjamesfreeman.com';                 // Email where notifications will be sent to

    // Other variables (don't edit)
    $safe = 'mysql_real_escape_string';
    $strip = 'strip_tags';


    // Verify form, save etc...
    if (isset($_POST['name'])) {

        $_POST = array_map('trim', $_POST);

        if (empty($_POST['name']) OR empty($_POST['description']) OR empty($_POST['paypal']) OR empty($_POST['first-name'])
            OR empty($_POST['last-name']) OR empty($_FILES['thumbnail_file']['name']) OR empty($_FILES['preview_file']['name'])) {

            $msg = error('You did not fill out all required fields !');

        } else if (!isValidEmail($_POST['email']) OR !isValidEmail($_POST['paypal'])) { // Invalid email address        

            $msg = error('Invalid email address format !');

        } else if (!empty($_POST['price']) AND !is_numeric($_POST['price'])) { // Price not empty and not int format!

            $msg = error('The price you entered is in invalid format!');

        } else if (getEXT($_FILES['thumbnail_file']['name']) != 'jpg' AND getEXT($_FILES['thumbnail_file']['name']) != 'png' AND getEXT($_FILES['thumbnail_file']['name']) != 'jpeg') { // Thumb ext not correct

            $msg = error('Thumbnail file has incorrect extension (accepted: png, jpg and jpeg)!');

        } else if (getEXT($_FILES['preview_file']['name']) != 'jpg' AND getEXT($_FILES['preview_file']['name']) != 'jpeg') { // Preview file ext not correct

            $msg = error('Preview file has incorrect extension (accepted: jpg and jpeg)!');

        } else {

            $thumbsize = getimagesize($_FILES['thumbnail_file']['tmp_name']);
            $prevsize  = getimagesize($_FILES['preview_file']['tmp_name']);
            $getcat = @mysql_fetch_assoc(mysql_query("SELECT value FROM categories WHERE id = '" . mysql_real_escape_string($_POST['category_id']) . "'"));

            if (!empty($_POST['demo_url']) AND !pingURL($_POST['demo_url'])) {  // Demo url is not empty, but web site link doesn't exist!

                $msg = error('Provided demo URL could not be found!');

            } else if ($thumbsize[0] != '80' OR $thumbsize[1] != '80') { // Thumbnail size doesn't match 80x80px

                $msg = error('Thumbnail size is not 80 x 80 px!');

            } else if ($prevsize[0] != '820' OR $prevsize[1] != '300') { // Preview size doesn't match 820x300px

                $msg = error('Thumbnail size is not 820 x 300 px!');

            } else if (empty($getcat['value'])) { // Hack or something, category id isn't in database

                $msg = error('ERROR: Selected category doesn\'t exist!');

            } else { // Save item to database, notify user & admin

                // Add to database
                $insert = mysql_query("INSERT INTO 
                    items (name, description, firstname, lastname, paypal, contactmail, price, demo_url, video_url, category_id, status, referrer) VALUES
                    ('{$safe($_POST['name'])}' , '{$safe($strip($_POST['description']))}' , '{$safe($_POST['first-name'])}' , '{$safe($_POST['last-name'])}' , 
                    '{$safe($_POST['paypal'])}' , '{$safe($_POST['email'])}' , '{$safe($_POST['price'])}' , '{$safe($_POST['demo_url'])}' , '{$safe($_POST['video_url'])}' , '{$safe($_POST['category_id'])}' , '0', '{$safe($_POST['referrer'])}')
                    ");

                if ($insert) {

                    $last = mysql_fetch_array(mysql_query("SELECT * FROM items ORDER BY id DESC LIMIT 1"));

                    // Prepare before we move files
                    $folder = 'media/' . $last['id'] * 2;
                    $folder2 = $web['main_folder_name'] . '/' . (($last['id'] * 2) * 5);

                    // Check if directories exist
                    if(!is_dir($folder)) { mkdir($folder); }
                    if(!is_dir($folder2)) { mkdir($folder2); }

                    // Set filenames
                    $thumb = $folder . '/' . basename($_FILES['thumbnail_file']['name']);
                    $prev  = $folder . '/' . basename($_FILES['preview_file']['name']);
                    $main  = $folder2 . '/' . basename($_FILES['main_file']['name']);

                    // Try to move files
                    $err = 0;

                    if(!move_uploaded_file($_FILES['thumbnail_file']['tmp_name'], $thumb)) { $err = 1; }
                    if(!move_uploaded_file($_FILES['preview_file']['tmp_name'], $prev)) { $err = 1; }
                    if(!empty($_FILES['main_file']['name']) AND !move_uploaded_file($_FILES['main_file']['tmp_name'], $main)) { $err = 1; }

                    // Check if files moved successfully
                    if ($err == 0) {

                        mysql_query("UPDATE items SET thumbnail='{$thumb}', preview='{$prev}', main_file='{$main}' WHERE id='{$last[id]}'");

                        // Send email to admin
                        email(array(
                            'to'        =>  $adminMail,
                            'from'      =>  $_POST['email'],
                            'subject'   =>  "Item {$_POST['name']} notification on {$web['url']}",
                            'msg'       => template('includes/emails/email_new_item_admin.txt', $_POST)
                        ));

                        // Send email to user
                        email(array(
                            'to'        =>  $_POST['email'],
                            'from'      =>  $returnMail,
                            'subject'   =>  "Item {$_POST['name']} notification on {$web['url']}",
                            'msg'       => template('includes/emails/email_new_item_user.txt', $_POST)
                        ));

                        // Redirect user to new submission page
                        header("Location: {$web[url]}item/{$last[id]}");
                        $msg = success('Item was uploaded successfully, you may now leave this page!');
                        unset($_POST);                      

                    } else {

                        $msg = error('There has been an issue while trying to save uploaded files! <br />Please try again later!');

                    }


                } else {

                    $msg = error('ERROR while saving your item! <br />Please try again later!' . mysql_error());

                }

            }

        }

    }
?>

<section class="dashboard content">
    <div class="container bg-color white">
        <?php if (isset($msg)) echo $msg; ?>
        <div class="row">
            <form name="contactform" action="/submit" method="POST" enctype="multipart/form-data">
                <div class="box-padding">
                    <div class="span6">
                        <h4>Submit Item</h4>
                        <div class="control-group">
                            <label class="control-label">Required fields marked with(*)</label>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Item name *</label>
                            <div class="controls">
                                <input type="text" name="name" class="input-block-level" value="<?php echo $_POST['name']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Description *</label>
                            <div class="controls">
                                <textarea class="input-block-level" rows="5" name="description"><?php echo $_POST['description']; ?></textarea>
                                <script>
    CKEDITOR.replace( 'description', {
    on: {
        instanceReady: function( ev ) {
            // Output paragraphs as <p>Text</p>.
            this.dataProcessor.writer.setRules( 'p', {
                indent: false,
                breakBeforeOpen: true,
                breakAfterOpen: false,
                breakBeforeClose: false,
                breakAfterClose: true
            });
        }
    }
}, {
    on: {
        instanceReady: function( ev ) {
            // Output paragraphs as <p>Text</p>.
            this.dataProcessor.writer.setRules( 'p', {
                indent: false,
                breakBeforeOpen: true,
                breakAfterOpen: false,
                breakBeforeClose: false,
                breakAfterClose: true
            });
        }
    }
})
;
</script> 
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Paypal Email Address *</label>
                            <div class="controls">
                                <input type="text" name="paypal" class="input-block-level" value="<?php echo $_POST['paypal']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">First Name *</label>
                            <div class="controls">
                                <input type="text" name="first-name" class="input-block-level" value="<?php echo $_POST['first-name']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Last Name *</label>
                            <div class="controls">
                                <input type="text" name="last-name" class="input-block-level" value="<?php echo $_POST['last-name']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Contact Email Address* </label>
                            <div class="controls">
                                <input type="text" name="email" class="input-block-level" value="<?php echo $_POST['email']; ?>">
                            </div>
                        </div>
                    </div>
                    <div class="span5">
                        <div class="control-group">
                            <label class="control-label">Price</label>
                            <div class="controls">
                                <div class="input-prepend input-append"> <span class="add-on"><?php echo decode_currency($web['currency']); ?></span>
                                    <input id="appendedPrependedInput" name="price" type="text" value="<?php echo $_POST['price']; ?>">
                                    <span class="add-on">.00</span> </div>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Demo url</label>
                            <div class="controls">
                                <input type="text" name="demo_url" class="input-block-level" value="<?php echo $_POST['demo_url']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Demo video url</label>
                            <div class="controls">
                                <input type="text" name="video_url" class="input-block-level" value="<?php echo $_POST['video_url']; ?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Category *</label>
                            <div class="controls">
                                <select name="category_id" class="input-block-level">
                                    <option value=""></option>
                                    <?php

                                        $get_cats = mysql_query("SELECT * FROM categories ORDER BY id");

                                        if(mysql_num_rows($get_cats)>0) {

                                            while($cat = mysql_fetch_array($get_cats)) {

                                                if ($_POST['category_id'] == $cat['id']) { $cat['id'] .= '" selected="selected'; }
                                                echo '<option value="'.$cat[id].'">'.$cat[value].'</option>';

                                            }

                                        }

                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Referrer</label>
                            <div class="controls">
                                <input type="text" name="referrer" class="input-block-level" value="<?php


              echo $_POST["referrer"];
?>">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Thumbnail (Select image with size 80x80 and format JPEG or PNG) *</label>
                            <div class="controls">
                                <input type="file" name="thumbnail_file">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Preview image (Select image with size 820x300 and format JPEG) *</label>
                            <div class="controls">
                                <input type="file" name="preview_file">
                            </div>
                        </div>
                        <div class="control-group">
                            <label class="control-label">Main file (Select main file with format ZIP)</label>
                            <div class="controls">
                                <input type="file" name="main_file">
                            </div>
                        </div>
                        <button type="submit" value="submit" class="btn btn-primary pull-right">Submit</button>
                    </div>
                </div>
                <br />
            </form>
        </div>
    </div>
</section>

请帮忙。我知道这可能只是一个简单的修复。我希望它也可以帮助其他用户。

我基本上只需要能够将'editor1'重命名为'description',但它只是在命名描述时不会显示。

1 个答案:

答案 0 :(得分:0)

这个评论太长了,如果它没有帮助,我会删除这个答案。

首先,如果您使用单词description或输出只是空,您是否看到CKEditor打开?因为如果输出为空,那就是预期的。 CKEditor创建一个替换textarea的iframe,不使用实际的textarea。您可能需要做的是在提交之前更新textarea。

您可以像这样更新(来自Ckeditor update textarea

for(var instanceName in CKEDITOR.instances)
    CKEDITOR.instances[instanceName].updateElement();

您的代码看起来有点奇怪,但替换上似乎没有任何错误。从您的页面查看JavaScript控制台,我猜测那里会有线索。还可以浏览现成的HTML源代码。你有一个在线的链接 - 这可能会有很大帮助吗?我在jsfiddle中尝试了名称描述和你的替换方法,它运行良好。

另外,您是否尝试过清除缓存?由于您的代码是内联的,因此这听起来并不像缓存问题。另外,你使用什么浏览器?