Search code examples
phpckeditorckeditor4.x

Ckeditor: saving data besides the < textarea >


I tried using Ckeditor for a project. The < textarea > can now be added to database but when I tried to add an < input > tag as shown below, I couldn't save it. Only the < textarea > is saved in the database:

                <div class="form-group"> 
                    <label for="exampleInputEmail1">Name</label>
                    <input type="text" name ="text" class="form-control" id="name"  placeholder="Name">
                </div>


                <div class="form-group"> 
                    <label for="exampleInputEmail1">Editor</label>
                    <textarea name ="text" class="form-control" id="text" rows="3" placeholder="Textarea"></textarea>
                </div>
                 <input type="submit" class="btn btn-primary" name="submit" value ="Submit">
            </form>

This is the Php:

 <?php 


                if (isset($_POST['submit'])) 
                {   
                    $text=$_POST['text'];
                    $name=$_POST['name'];
                    $con=mysqli_connect('localhost','root','','ckeditor') or die(ERROR);
                    $query=mysqli_query($con,"INSERT INTO content(content,author) VALUES ('$text','$name')");


                    if($query==1)  
                           {  
                            echo "<script>
                        window.location.href='./ask_question.php';
                        alert('Success');

                        </script>";
                          exit;
                           }  
                        else  
                           {  
                              echo'<script>alert("Failed To Insert")</script>';  
                           }  



                }
                ?>

Solution

  • This is because you have duplicated the name attribute ("text") in both elements. When doing form submission, name is used instead of id.

    A slight modification should suffice:

                <div class="form-group"> 
                    <label for="exampleInputEmail1">Name</label>
                    <input type="text" name="name" class="form-control" id="name"  placeholder="Name">
                </div>
    
    
                <div class="form-group"> 
                    <label for="exampleInputEmail1">Editor</label>
                    <textarea name ="text" class="form-control" id="text" rows="3" placeholder="Textarea"></textarea>
                </div>
                 <input type="submit" class="btn btn-primary" name="submit" value ="Submit">
            </form>