0

First I will put the code here.

This is my ajax code:

//View para crear proyecto
$("#myFormProject").submit(function(e){
    e.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
        url:'/admin/projects/postUpload',
        type:'POST',
        data: formData,
        success: function(){
            $("#formcrearproyecto").fadeOut(1000);
            $("#formcreartraducciones").fadeIn(1000);
        },
        cache: false,
        contentType: false,
        processData: false
    });
    return false;
});

My controller function look like this:

public function storeProject(Request $request)
    {
    $clients = DB::table('clients')->orderBy('name','asc')->get();
    $projects = DB::table('projects')->get();
    $project = new Project();
    $project->slug = $request->input("slug");
    $project->order = DB::table('projects')
                  ->where('order', DB::raw("(select max(`order`) from projects)"))
                  ->first()
                  ->order + 1; 
    $project->public = 0;
    $imagen = getimagesize($request->file('pathheader'));
    $ancho = $imagen[0];
    $altura = $imagen[1];
    $maxancho = 1930;
    $minancho = 1910;
    $maxaltura = 822;
    $minaltura = 802;
    $imagen2 = getimagesize($request->file('pathhome'));
    $ancho2 = $imagen2[0];
    $altura2 = $imagen2[1];
    $maxancho2 = 778;
    $minancho2 = 358;
    $maxaltura2 = 355;
    $minaltura2 = 335;
    if ($ancho<$maxancho && $ancho>$minancho && $altura<$maxaltura && $altura>$minaltura &&
    $ancho2<$maxancho2 && $ancho2>$minancho2 && $altura2<$maxaltura2 && $altura2>$minaltura2){
    \Storage::disk('projects')->makeDirectory($project->slug);
    $project->pathheader = \Storage::disk('projects')->putFileAs($project->slug, $request->file('pathheader'),'header.jpg');
    $project->pathhome = \Storage::disk('projects')->putFileAs($project->slug, $request->file('pathhome'),'home.jpg');
    $project->save();
    File::put(resource_path('views/projects/').$project->slug.'.blade.php','');
    }
    else{
        Session::flash('warning','Las medidas de almenos una de las 2 imagenes no es la correcta.');
            return view('cms.public.views.projects.menu',['projects' => $projects, 'clients' => $clients]);
    }

}

The problem is when I submit a picture then don't enter in IF and go to ELSE equally make the fadeout and fadein.

How can I make a condition inside Ajax to check whether to enter in or not, and make fadeout and fadein or just show the session::flash?

Thanks a lot.

2
  • you mean you want to enter when image is there in request right? Commented Sep 7, 2017 at 8:01
  • I want to fadeOut div and Fadein div just when image have the correct size. So when enter in if yes, make the div fadeout and fadein but if go to else just make the session::flash Commented Sep 7, 2017 at 8:03

1 Answer 1

2

I think you can make use of json return type like below -

// this code is for backend
if($ancho2<$maxancho2 && $ancho2>$minancho2 && $altura2<$maxaltura2 && $altura2>$minaltura2)
{
  // your code here
   ......

   $project->save();
   File::put(resource_path('views/projects/').$project->slug.'.blade.php','');
   // returns json string to the client
   return json_encode(['stauts'=>'ok']);
}
else{
     $data = array('status' => 'error',
                   'warning' => 'Las medidas de almenos una de las 2 imagenes no es la correcta.');
      return json_encode($data);
}

Now you need to access this in client side in javascript..

 $("#myFormProject").submit(function(e){
    e.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
       url:'/admin/projects/postUpload',
       type:'POST',
       data: formData,
       success: function(responseText){
            // this will convert json string to json object
            $responseData = JSON.parse(responseText);
            if($responseData.status == 'ok')
            {
                 $("#formcrearproyecto").fadeOut(1000);
                 $("#formcreartraducciones").fadeIn(1000);
            }
            else if($responseData.status == 'error'){
                  alert('Handled error and the warning message is '+$responseData.warning);
             }
            else{
                alert('Error happened in backend,but not handled');
            }
       },
       cache: false,
       contentType: false,
        processData: false
    });
    return false;
});
Sign up to request clarification or add additional context in comments.

2 Comments

Works perfectly and easy to understand. Let me ask you something, alerts I think it's dirty, how can I make to return session flash? Thanks.
you can use bootstrap alert or notification plugins like Bootstrap Growl Notifications, Toastr Notification,Notific8 Notification. If you need help feel free

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.