Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

laravel - 3 ajax modal forms on one page not working

I want to have on one page 3 modal forms to allow user to send email to admin using ajax.

First of all I tested it without ajax. I have 3 buttons. Every button opens one form in modal window using alpinejs. After click on submit button POST request is sent to route specified in form. Then email is sent. And then current page is reloaded. So user stays on the same page, but he still see that page is reloaded. Code in controller responsible for POST route is like that

  public function callback(CallbackRequest $request) {
        Mail::to( config('mail.to.address') )->send(new Callback());
        return back()->with([
      'status'      => 'success',
      'messageHeader'   => 'Your question sent!', 
      'messageContent'  => 'We will contact you soon'
          ]);
  }

Without ajax all 3 modal forms work perfectly.

Then I want to introduce ajax and I have many problems:

  1. X button in top right corner of modal window is not working => code @click="callback = false". Though code @click.away="callback = false" is working when you click outside modal window.
  2. Cancel button is not working as expected. I have @click="callback = false" on this button, but it doesn't try to close modal. Instead it tries to submit form.
  3. When I submit callback form first I see Form sucessfully submitted! in the modal window, then I receive email message and in Chrome Devtools Network tab I see code 200 in General section and in Response section I see status: success, messageHeader:..., messageContent:... But when I submit zamer or eskiz form I still see Form sucessfully submitted! in the modal window, but I don't receive email message and in Chrome Devtools Network tab I see code 302 in General section and nothing in Response section.

Link to code is https://codepen.io/schel4ok/full/wvowMxK

Code in controller like that

  public function callback(CallbackRequest $request) {

        Mail::to( config('mail.to.address') )->send(new Callback());
        return response()->json([
      'status'      => 'success',
      'messageHeader'   => 'Your question sent!', 
      'messageContent'  => 'We will contact you soon'
          ]);
  }
question from:https://stackoverflow.com/questions/65927540/3-ajax-modal-forms-on-one-page-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...