Home jQuery Ajax Sending a Post Data to a Method in Laravel Controller Class
Reply: 0

jQuery Ajax Sending a Post Data to a Method in Laravel Controller Class

user8475
1#
user8475 Published in April 22, 2018, 4:57 am

Am trying to send data to a Controller method in Laravel Project am working on, the request is a POST as i specified in the Route file of the project and the url structure is as http://www.globfinances.com/mail-walker.

The Controller class has a method that will process the post data and send email with it and am making the post request using jquery $.post ajax function to send the data which i got from data-** attribute of the button that triggers the event.

Here is My code in the laravel Route file:

/* Ninja Management Route List */
Route::post('mail-walker',['as'=>'mail-walker','uses'=>'UserController@mailWizard']);

Here is the method that handles the Request (in the UserController Class)

/******** CONSTRUCTING THE MAIL RUNNER WIZARD FUNCTION ************/
public function mailWizard(Request $request){
    $thisUser = User::findOrFail($request->id);
    // HERE LETS CHECK REQUEST PARAMETERS AND KNOW WHAT TO PASS TO MAIL
    if($request->type == "dpo" || $request->type == "wun"){
        // Mail data
        $val = [
          'amount' => $request->amount,
            'currency_sign' => $request->c_sign,
            'payment_type' => $request->p_type,
            'type' => $request->type,
            'method' => $request->method,
            'message' => $request->message,
            'userid' => $id,
        ];
        // Now lets send email
        $this->mailRunnerAdmin($val, $thisUser);
        $this->mailRunnerUser($val, $thisUser);
    }
    else{
        // Mail data
        $val = [
          'amount' => $request->amount,
            'currency_sign' => $request->c_sign,
            'payment_type' => $request->p_type,
            'type' => $request->type,
            'userid' => $id,
        ];
        // Now lets send email
        $this->mailRunnerAdmin($val, $thisUser);
        $this->mailRunnerUser($val, $thisUser);
    }
}

/********** CONSTRUCTING THE MAIL RUNNER FUNCTION *************/
public function mailRunnerAdmin($mailData, $thisUser)
{
    $general = GeneralSetting::first();

        $mailData['site_title'] = $general->title;
        $mailData['site_footer'] = $general->$general->footer_bottom_text;
        $mailData['email'] = $thisUser['email'];
        $mailData['name'] = $thisUser['name'];
        $mailData['g_email'] = $general->email;
        $mailData['g_title'] = $general->title;
        $mailData['subject'] = 'Request for payment from '.ucfirst($thisUser['name']);

        Config::set('mail.driver','mail');
        Config::set('mail.from',$general->email);
        Config::set('mail.name',$general->title);
        Mail::send('auth.mailRunnerAdmin', $mailData, function ($m) use ($mailData) {
            $m->from($mailData['g_email'], $mailData['g_title']);
            $m->to($mailData['g_email'], $mailData['g_title'])->subject($mailData['subject']);
        });

}


public function mailRunnerUser($mailData, $thisUser)
{
    $general = GeneralSetting::first();

        $mailData['site_title'] = $general->title;
        $mailData['site_footer'] = $general->$general->footer_bottom_text;
        $mailData['email'] = $thisUser['email'];
        $mailData['name'] = $thisUser['name'];
        $mailData['g_email'] = $general->email;
        $mailData['g_title'] = $general->title;
        $mailData['subject'] = 'Payment Request Confirmation';

        Config::set('mail.driver','mail');
        Config::set('mail.from',$general->email);
        Config::set('mail.name',$general->title);
        Mail::send('auth.mailRunnerUser', $mailData, function ($m) use ($mailData) {
            $m->from($mailData['g_email'], $mailData['g_title']);
            $m->to($mailData['email'], $mailData['name'])->subject($mailData['subject']);
        });

}

Here is the JQuery Code that Makes the Ajax Request

$(".mailManTrigger").click(function(e){
e.preventDefault();
// Now lets delay as we call ajax function
// Setting the trigger variables
// We begin processing
var id = $(this).attr("data-id");
var amt = $(this).attr("data-amount");
var currency_sign = $(this).attr("data-c_sign");
var payment_type = $(this).attr("data-p_type");
var type = $(this).attr("data-type");
var url = $(this).attr("data-url");
var token = $(this).attr("data-token");
var method = $(this).attr("data-method");
var message = $(this).closest('form').find('textarea[name="message"]').val();

// Lets call our mail man to deliver message
// Lets check type to know how to send
    if(type!="dpo" || type!="wun"){
        $.post(
                    url,
                    {
                       _token: token,
                       amount : amt,
                       c_sign : currency_sign,
                       p_type : payment_type,
                       type : type,
                       id : id
                     },
                    function(data) {
                       // Now lets return
                       $(this).closest('form').submit();
                    }
           );
    }
    else{
        $.post(
                    url,
                    {
                       _token: token,
                       amount : amt,
                       c_sign : currency_sign,
                       p_type : payment_type,
                       type : type,
                       id : id,
                       method : method,
                       message : message
                     },
                    function(data) {
                       // Now lets return
                        $(this).closest('form').submit();
                    }
           );
    }
  });

Here is the form button that triggers the event (look at the data- Attribute)**

  <button class="btn btn-danger btn-block btn-icon icon-left mailManTrigger" data-id="7" data-amount="0.524" data-c_sign="NGN" data-p_type="Credit Card Payment" data-type="ccd" data-url="http://www.globfinances.com/mail-walker" data-token="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q"><i class="fa fa-send"></i>Add Fund Now</button>

Here is form the Jquery code should submit on ajax success

  <form method="POST" action="http://www.globfinances.com/stripe-preview" accept-charset="UTF-8"><input name="_token" type="hidden" value="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q">
                                        <input type="hidden" name="amount" value="0.52">
                                        <input type="hidden" name="fund_id" value="16">
                                        <input type="hidden" name="transaction" value="180112q7mok811">
                                        <input type="hidden" name="charge" value="12">
                                            <div class="form-group">
                                                <div class="col-sm-6 col-sm-offset-3">
                                                    <button class="btn btn-danger btn-block btn-icon icon-left mailManTrigger" data-id="7" data-amount="0.524" data-c_sign="NGN" data-p_type="Credit Card Payment" data-type="ccd" data-url="http://www.globfinances.com/mail-walker" data-token="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q"><i
                                                                class="fa fa-send"></i>Add Fund Now</button>
                                                </div>
                                            </div>
                                        </form>

Problem

The problem is that when i click the button nothing happens, i dont get email message i intend the code to send, the form is not submitted which means that the ajax call was not successful. I dont know what am doing wrong, and i need assistance to fix this.

What i want to achieve

I want to send an email to the user and the admin when the button is clicked and then submit the form which i prevented from submitting in the beginning of the jQuery function.

Any assistant will be highly appreciated!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301284 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO