Hello,

I wonder why I keep failing saving data. I keep getting this message:

"please fill the title and content article" - eventhough I already fill them in.

article.blade.php

     <div class="backend-form create-article-form" style="display:none;">
                                <div class="form-top">
                                    <div class="form-left">
                                        <h4>Create Article</h4>
                                    </div>
                                    <div class="form-right">
                                        <a href="#" class="btn-save-article setting_save"></a>
                                        <a href="#" class="btn-cancel-article setting_cancel"></a>
                                    </div>
                                </div>
                                <div class="form-body">
                                    <div class="form-group">
                                        <span>Tittle</span><input type="text" name="tittle" id="tittle" class="tittle" required/>

                                    </div> 

                                    <div class="form-group">
                                        <span>Content</span>
                                    </div>                                                    
                                                <textarea rows="10" maxlength="1000" id="content" class="content"></textarea>
                                </div>
                            </div>

     $(".btn-save-article").click(function(e){
        e.preventDefault();
        var tittle = $("#tittle").val(),
        content = $("#content").val();

        $.ajax({
            method: "POST",
            url: '{{action("AjaxController@postCreateArticle")}}',
            data: { tittle : tittle, content : content },

            success: function (response) {
                if (response=='"error"') {
                    pesanErr("please fill the title and content article");
                }else{ 
                pesanOk("Article created");
                setTimeout(function(){location.reload()},1000);
                };
            },
        })

   });

AjaxController.php

 public function postCreateArticle(Request $request)
{
    $domain = $_SERVER['SERVER_NAME'];
    $user = User::where('domain', $domain)->first();

    if ($user) {
        $domain = $_SERVER['SERVER_NAME'];
        $user = User::where('domain', $domain)->first();

        $tittle = $request->input("tittle");
        $content = $request->input("content");

        if (empty($tittle) or empty($content)) {
            $message = "error";
        } else {

            date_default_timezone_set("Asia/Jakarta");
            $article = new Timeline;
            $article->user_id = $user['id'];
            $article->social_id = $user['id'];
            $article->tittle = $tittle;
            $article->content = $content;
            $article->status = "0";
            $article->updated_at = ("0000-00-00 00:00:00");
            $article->social = "article";
            $act = $article->save();

            if ($act) {
                $message = "success";
            } else {
                $message = "error";
            }                
        }
        echo json_encode($message);
    }
   }

Recommended Answers

All 2 Replies

Hi,

the AJAX request seems fine and the server side too. It could be a mistype, I see in the form you set tittle with two Ts, are you sure it's the same in the database table?

$article->tittle = $tittle;

If that's okay, see if Laravel error log helps.

In addition to what cereal said, have you tried outputting tittle and content to the console, just to be sure they are not empty there?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.