Matthias Posted October 28, 2016 Share Posted October 28, 2016 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 More sharing options...
3fingers Posted October 28, 2016 Share Posted October 28, 2016 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 More sharing options...
Matthias Posted October 28, 2016 Author Share Posted October 28, 2016 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 More sharing options...
3fingers Posted October 28, 2016 Share Posted October 28, 2016 Does using "isset()" - mentioned here - change something? if (isset($input->post->list)) { echo 'hello, ' . $input->post->firstname; } Link to comment Share on other sites More sharing options...
Matthias Posted October 28, 2016 Author Share Posted October 28, 2016 --- checking again … Link to comment Share on other sites More sharing options...
BitPoet Posted October 28, 2016 Share Posted October 28, 2016 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)? 2 Link to comment Share on other sites More sharing options...
Matthias Posted October 28, 2016 Author Share Posted October 28, 2016 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 … 1 Link to comment Share on other sites More sharing options...
elabx Posted March 15, 2018 Share Posted March 15, 2018 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?? Link to comment Share on other sites More sharing options...
BitPoet Posted March 16, 2018 Share Posted March 16, 2018 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. 1 Link to comment Share on other sites More sharing options...
szabesz Posted April 20, 2018 Share Posted April 20, 2018 related: Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now