Roots Discourse

How to create a form controller?

I’ve been working with Sage the last two weeks and I find it very powerful, but as I’m new with Laravel/Blade there are some things that I don’t understand how to do.

I want to create a custom form which will call a 3rd party API. I’ve created a controller called Forms.php, the HTML form and the AJAX call in jQuery. How can I call the controller function in the ‘url’ parameter in the jQuery Ajax call?

In a normal Wordpress I would do a .php to handle the data (validation and send the data) and send a response back to Ajax ‘success’. What’s the best way to do that in Sage?

Thanks in advance for your help!

EDIT
Adding code for better understanding:

Common.js

$('form').submit(function(e){
  e.preventDefault();
  let url = // What to use here?,
  data = $(this).closest('form').serialize();

  $.ajax({
    url: url,
    type: 'post',
    data: data,
    success:function(response){
      alert(response);
    },
   });
  });

Form.php

namespace App\Controllers;
use Sober\Controller\Controller;

class Forms extends Controller
{
    public function processForm()
    {
        return 'hello';
    }
}

I would really appreciate your help.

You post to ajaxurl
Some reading: https://codex.wordpress.org/AJAX_in_Plugins
You dont need the Controller for this at all.

Thank you for your answer Xilonz. You are right, I was trying to make it more difficult than it is.

Thanks so much :slight_smile:

This topic was automatically closed after 42 days. New replies are no longer allowed.