Jump to content
Matthias

no $input data when post form with ajax

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






 

Share this post


Link to post
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;
}

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Does using "isset()" - mentioned here - change something? 

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

Share this post


Link to post
Share on other sites

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)?

  • Like 2

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Why is this mandatory? Does it have to do with template configuration of trailing slashes?? Can this be altered to don't matter in a global way??

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...