Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Controller :
- public function newCategory(Request $request) {
- $category = new Category;
- $name = $request->name;
- $url = $request->url;
- $meta = $request->meta;
- $timestamp = date('Y-m-d H:i:s');
- $category->name = $name;
- $category->url = $url;
- $category->meta = $meta;
- $category->created_at = $timestamp;
- $category->updated_at = NULL;
- $addCat = $category->save();
- if($addCat) {
- return response()->json(array('status' => 200, 'message' => array('title' => 'Success', 'msg' => 'Category has been added')));
- } else {
- return response()->json(array('status' => 500, 'message' => array('title' => 'Failed', 'msg' => 'Failed adding Category, Try again later.')));
- }
- }
- Routes :
- Route::group(['prefix' => 'category'], function(){
- Route::get('/', 'HomeController@category')->name('category');
- Route::get('add', 'HomeController@addCategory');
- Route::post('add/go', 'HomeController@newCategory');
- });
- Form :
- <form method="POST" id="catNew">
- @csrf
- <div class="row">
- <div class="col-md-2">
- <label for="title">Name:</label>
- </div>
- <div class="col-md-10">
- <input type="text" class="form-control" name="catname" placeholder="Category Name"/>
- </div>
- </div>
- <div class="row" style="margin-top: 10px;">
- <div class="col-md-2">
- <label for="title">url:</label>
- </div>
- <div class="col-md-10">
- <input type="text" class="form-control" name="curl" placeholder="ex: lorem-ipsum"/>
- </div>
- </div>
- <div class="row" style="margin-top: 10px;">
- <div class="col-md-2">
- <label for="title">Meta:</label>
- </div>
- <div class="col-md-10">
- <input type="text" class="form-control" name="catmeta" placeholder="The Meta Description"/>
- </div>
- </div>
- <div class="row" style="margin-top: 10px">
- <div class="col-md-12">
- <button type="submit" class="btn btn-success" style="margin-left: 20px; margin-right: 10px">Submit</button>
- <button onclick="window.location.href='/admin/category';" class="btn btn-danger">Cancel</button>
- </div>
- </div>
- </form>
- Ajax :
- <script>
- $(document).on('submit', '#catNew', function(e) {
- e.preventDefault();
- var fd = new FormData($('#catNew')[0]);
- $.ajax({
- url: '/admin/category/add/go/',
- type: 'POST',
- data: fd,
- processData: false,
- contentType: false,
- dataType: 'json',
- async: true,
- error: function(xhr, status, message){
- console.log(xhr.responseText);
- }}).done(function(data) {
- switch(data.status) {
- case 200:
- alertify.success(data.message.title, data.message.msg);
- window.location.href = '/admin/category';
- break;
- default:
- alert(data.message.title, data.message.msg);
- }
- });
- });
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement