Geoff_S
Geoff_S

Reputation: 5105

Returning stored procedure output through ajax

I currently have a working process where an ajax call is made in my blade which makes a call through the controller and that function hits a stored procedure call using PDO. This call is successful and my stored procedure executes/inserts properly and is set to return back my output. My only question now is:

How can I take the output from the stored procedure and pass it back into my blade for a hidden input? There is no page refresh, so when the AJAX call is successful then I want to put my output from the service file into a hidden input in the blade. How can I properly do this?

BLADE:

$.ajax({

   type:'POST',
   url:'campaigns/createCampaign',
   data:{campaignName:campaignName, attribute:attribute},
    _token: '{{ csrf_token() }}',
   success:function(data){
        intro_modal.hide();
   }
});

CONTROLLER:

public function createCampaign(Request $request)
{
    $campaignName = $request->campaignName;
    $attribute = $request->attribute;

    $campaignService = new CampaignService();
    $createCampaign = $campaignService->createCampaign($campaignName, (int) $attribute);

    //return response()->$campaignService;
}

SERVICE:

function createCampaign($campaignName, $attribute){

    $stmt = \DB::connection('odbc')->getPdo()->prepare('CALL PROCEDURES.INSERT_CAMPAIGN(?,?,?)');

    $stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
    $stmt->bindValue(2,$attribute, $attribute==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
    $stmt->bindParam(3,$out2, PDO::PARAM_INT);

    $stmt->execute();

}

Upvotes: 1

Views: 864

Answers (1)

loic.lopez
loic.lopez

Reputation: 2103

In your createCampaign

function createCampaign($campaignName, $attribute){

    $stmt = \DB::connection('odbc')->getPdo()->prepare('CALL PROCEDURES.INSERT_CAMPAIGN(?,?,?)');

    $stmt->bindValue(1,$campaignName, PDO::PARAM_STR);
    $stmt->bindValue(2,$attribute, $attribute==0 ? PDO::PARAM_NULL : PDO::PARAM_INT);
    $stmt->bindParam(3,$out2, PDO::PARAM_INT);
    $stmt->execute();

    return $out2;
}

In your Controller

Use these class:

use Illuminate\Support\Facades\Response;
use Illuminate\Http\Response as HttpResponse;

Return a JSON response:

public function createCampaign(Request $request)
{
    $campaignName = $request->campaignName;
    $attribute = $request->attribute;

    $campaignService = new CampaignService();
    $createdCampaignId = $campaignService->createCampaign($campaignName, (int) $attribute);

    return Response::json(["campaign_id" => $createdCampaignId)
            ->setStatusCode(HttpResponse::HTTP_OK);
}

In your Blade template

$.ajax({

   type:'POST',
   url:'campaigns/createCampaign',
   data:{campaignName:campaignName, attribute:attribute},
    _token: '{{ csrf_token() }}',
   success:function(data){
        intro_modal.hide();
       // data.campaign_id will contains the new campain id
   }
});

After you will just need to insert the value of the data.campaign_id with jQuery for example.

Upvotes: 1

Related Questions