JayFrog
JayFrog

Reputation: 11

OctoberCMS using Ajax API with no-script fallback in page php section

My current base for a simple contact form is:

A page containing the form and the form validation in the php section with a native function like so:

function onStart(){if(Request::isMethod('post')) /* do stuff */}

Above is my "no-script" solution.

Now, when I want to enable OctoberCMS' Javascript API I have to call a self defined function in the php section, for example:

function onSubmit(){/* do stuff */}

How could I combine both in one function so that /* do stuff */ triggers, regardless if the request was send via Ajax or pure php?

Upvotes: 0

Views: 125

Answers (2)

JayFrog
JayFrog

Reputation: 11

I found the answer.

OctoberCMS provides a native method to let a form trigger a custom onMyfunction(), just by using another way of implementing the form.

For anybody who might get into the same problem, here's the code:

<?php
function onMyfunction(){}
?>
==
<html>
{{ form_open({ request: 'onMyfunction', id: 'my-form' }) }}
</html>

This will send the post data to the php handler, regardless if Javascript is enabled or not. Now you only need to initiate your Ajax API with:

<script>
$('#my-form').submit(function(evt){
    evt.preventDefault();
    $(this).request('onMyfunction',{})
});
</script>

Upvotes: 1

Pettis Brandon
Pettis Brandon

Reputation: 865

Could you use the html <noscript> tag?? In your css you would put .noscript {display:none;} .script {display:block;} then in your pages, component template, layout etc you could have two forms that change depending on if the user is using script or not. They can use the same onSubmit function.

Any user interaction with any of the sites I have made assume some might not use Javascript so I default to use standard forms and not js or ajax.

Upvotes: 0

Related Questions