Jump to content

no $input data when post form with ajax


Matthias
 Share

Recommended Posts

Hi there together! :)

Im trying to do the following:
 

Having a simple template (page) form.php (form):

<form id="my-form" method="post" action="<?php echo $config->urls->root; ?>proceed">
  <input type="text" name="firstname" value="john doe">
  <button type="submit">submit</button>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function() {
      $("#my-form").submit(function(e){
        var form = $(this);
        e.preventDefault();
        $.post(
          form.attr("action"),
          form.serialize(),
          function(result){
            form.html(result);
          });
        });
    });
</script>

posting "to" a simple proceeding template (page) proceed.php (proceed):
 

<pre>
<?php

if($config->ajax) {
  echo 'proceed.php via ajax called!';
}

if($input->post->firstname) {
  echo 'hello, ' . $input->post->firstname;
}

?>
</pre>

Everything works fine after hitting "submit" except that the POST data in $input is not found. So the result is:

proceed.php via ajax called!

and not as wished / expected:
 

proceed.php via ajax called!
hello, john doe


Any idea why? Because of the serializing? Or maybe i have overseen something?

Thanks a lot!

Matthias






 

Link to comment
Share on other sites

Hi Mattiash,

it' not working because the second "if" statement is outside the first one that is checking if the form was submitted via ajax.

Maybe this:

if($config->ajax) {
  echo 'proceed.php via ajax called!';
    if($input->post->firstname) {
      echo 'hello, ' . $input->post->firstname;
    }
}

// or

if($config->ajax && $input->post->firstname) {
  echo 'proceed.php via ajax called!';
  echo 'hello, ' . $input->post->firstname;
}

 

Link to comment
Share on other sites

Thanks for your reply, but it's still not working. Which is obvious i guess, because the code outside the if statement is called in any case, if not ajax or if ajax, too, of course. The config-ajax check was more meant as test if the page "proceed" is really called and working.

Link to comment
Share on other sites

8 minutes ago, BitPoet said:

Is the form data actually sent (what does the browser's network console say)? Is any 301 redirection happening for the request to the proceed page (and if yes, does adding a trailing slash help)?

Thank you. The missing trailing slash in the form action argument was indeed the problem, causing a 301. (see screenshot attached)

Now it works fine!

So this is correct:
 

<form id="my-form" method="post" action="<?php echo $config->urls->root; ?>proceed/">
  … etc …

 

 

Bildschirmfoto 2016-10-28 um 15.19.51.PNG

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
11 hours ago, elabx said:

Can this be altered to don't matter in a global way?

I'm not sure. You could add some htaccess/rewrite trickery to avoid redirects, but that would mean that all template settings have to be identical. If somebody changed the trailing slash setting for just one template, that would fail even worse (like end in an endless redirect loop).

It might be possible to avoid his kind of error if the backend issued a redirect with a 307 status code instead of 301 for slash url redirects, as that would preserve the request method and POST data for the repeated request, but there has been a lot of back and forth about 307 and308 methods, browser implementation details, compatibility with older browsers and possible security issues which I haven't been following up on. Perhaps @ryan himself might be able to say whether redirecting with 307 would be an option.

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...