Form controller and button-element

Hi,

does anybody know why I can’t use a controller for a form with a button-submit-element?
The controller react only if I use an input-element as button.

Cheers

Is the form submitted if you click the button? Could you post the form and the controller?

Is this a custom form or one based on Uniform?

Hi,

its a custom form. But I hav this problem yet on every form with a controller.
The controller is like:

    return function($site, $pages, $page, $modules) {
        if(r::is('post') && get('submit')) {

            $token = $_POST['csrf'];
            if(csrf($token) === true) {
…

and the Button:

<button type="submit" name="submit" id="submit-185" class="btn btnPrimary span-8 push-2" style="">Send posterregister request</button>

Again, is the form submitted? Can you see it happen in dev tools?

Is the post sent as a POST request?

For testing, you can also add this code to your controller

$post = [];
if(r::is('post') && get('submit')) {
  $post = $_POST;

}
dump($post);

Does the url of the form‘s action exist?

My form have no action because the controller is sending the form through go().
This work perfect with input sumit buttons but not with real buttons.
So I have wrote a simplified testform:

testform.php template:

<?php snippet('head') ?>
<main class="grid-wrap">
<form class="paypal" method="post" >
    <input type="text" name="name" />
    <input type="email" name="mail" />
    <textarea name="mesage" row="8">
    </textarea>
    <input type="submit" name="testsubmit" value="test abschicken" />
    <button type="submit" name="testsubmit">test abschicken</button>
   </form>
</main>
<?php snippet('footer') ?>

testform.php controller:

<?php
return function($site, $pages, $page, $modules) {

if (r::is('post') && get('testsubmit')) {
    go('https://www.google.de');
}
}
?>

Also wenn ich $_POST mit dump ausgeben lasse ist das Array leer.
in den Devtools steht unter Form Data aber entsprechendes drinnen.

If you add a value to your button, it should work:

<button type="submit" name="testsubmit" value="test abschicken">test abschicken</button>

Or remove && get('testsubmit') from the controller, otherwise, your condition will evaluate to false.

The action attribute is in fact not required, as the default is that the request is sent to the same page. But it is not

sent through go(),

that is something that happens after the form has already been submitted.

The action attribute must of course be set if you want some other script to do the form processing, or if a form is used on multiple pages (for example a search form in.the header or footer of your site), unless the form is submitted via JS.

1 Like

Why is it a problem if testsubmit is empty in the send data?

And your right I was not precise. go is what I want to to after the form is send.
The data of the form is saved in a $_SESSIONto use it on the site I have mentioned in the go().

The problem is not primarily that the string is empty, the problem is that an empty string returns a boolean false so that the second part of your condition

if(r::is('post') && get('submit')) {

i.e. get('submit') returns false and as a consequence, the code within the if statement is not executed.

Thanks for your explanation!.