dsf23223 2013-11-16 16:04
浏览 56
已采纳

使用CKEditor发布文本区域的值

I'm trying to post the data which is saved via CKEditor but it always comes up with 'You did not fill out all required fields !'. It says in it's documentation that the string needs to be the same name with the one you are replacing. But the editor doesn't show up when the string is 'description' it only shows up on the form in browser when it's named 'editor1' like below:

CKEDITOR.replace( 'editor1', {

But it needs to be description. Because when I write the string 'description' in- shown in the code below. It doesn't appear. The form needs to submit the html entered and outputted by the editor but I can't seem to get it.

This is my code:

    <?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>

Please can you help. I know it's probably just a simple fix. I hope it can help other users too.

I basically just need to be able to rename 'editor1' to 'description' but It simply doesn't show up when named description.

  • 写回答

1条回答 默认 最新

  • dousui8263 2013-11-19 07:46
    关注

    This was too long to be a comment, I will delete this Answer if it doesn't help.

    First of all, do you even see the CKEditor opening if you use the word description or is the output just empty? Because if the output is empty, that is expected. CKEditor creates a iframe that replaces the textarea, the actual textarea is not used. What you might need to do is update the textarea before submitting.

    You can update like this (from Ckeditor update textarea)

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

    Your code looks a bit weird, but there doesn't seem to be anything too wrong with the replace. Have a look at the JavaScript console from your page, I'm guessing there will be clues there. Also browse through the ready source HTML for anything obvious. Do you have a link where this is online - that might help a lot? I tried it in jsfiddle with the name description and your replace method and it worked fine.

    Also, have you tried clearing the cache? This doesn't really sound like a cache problem though since your code is inline. Also, what browser do you use?

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制