Novice
Novice

Reputation: 393

Laravel button, onclick function without change URL

I need create button in blade without any change of URL, im totaly lost, dont know what im doing wrong.

When i press the button, my url is changed how route is set. What is correct way to set buttons in Laravel?

This is my try:

public function sendMessageButton()
{
    $phoneNumber = 123456789;
    $phoneMessage = 'Hi';

    return SendTextMessage::SendMessage($phoneNumber, $phoneMessage);
}

sendMessageButton is very simple function, SendTextMessage is custom class with connection to GSM gateway.

blade

<button type="button" id="sendText" name="sendText" onclick="window.location='{{ route('sendSms') }}'">

I found on internet, that i need set Route to controller.

Route::get('/','SendController@sendMessageButton')->name('sendSms');

And there is my problem, '' this part of route, changing my URL, how can i avoid this ? i just need, press the button and get return SendTextMessage::SendMessage($phoneNumber, $phoneMessage); and stay on my URL where im.

Upvotes: 0

Views: 1890

Answers (1)

Novice
Novice

Reputation: 393

@cinameng nice tip

route

Route::get('/send','SendController@sendMessageButton')->name('sendSms');

controller

change this

return SendTextMessage::SendMessage($phoneNumber, $phoneMessage);

to

SendTextMessage::SendMessage($phoneNumber, $smsCode, $smsExpirationTime);

return back();

Upvotes: 1

Related Questions