0

Is it very bad to set form method to ajax when submitting the form with jquery ajax like so?

HTML:

<form method="ajax" action="scriptName">
    <input type="submit" name="submit" value="submit">
</form>

JS:

$("form[method=ajax]").submit(function(e){  //  <--
    e.preventDefault();
    /*  ...  */
});

It works, but there's a lot of things that works, but not necessary should be done just because it seems to be ok.

The ajax script is executing a script.exe.php that will handle the request. This file detects both $_GET['script'] and $_POST['script'] requests, and then includes the corresponding script from a white-list.

The very same file, script.exe.php, is also included at the begining of index.php.

if($_REQUEST['script']){  include 'script.exe.php';  }

?script= (not shown above) is optional in the form action attribute on ajax request. But not for non-ajax request. But by adding it to both could potentially prevent total failure if javascript is not activated.

Because if something fails, and for some reason the form is submited without ajax, I suppose the form would submit and cause a page redirect to whatever is in the action attribute. Which would be ?script=scriptName.
And since this action will get detected by the $_REQUEST['script']. The form would get submitted as expected?

I could of course supply a class for the javascript to detect instead of using the form attribute. Like normal folks do.

<form class="ajax" action="?script=scriptName">

$("form.ajax").submit(function(e){

Here I'm just asking. I would gladly know what danger this could have in practice. Or if it's not a big deal at all...

ThomasK
  • 2,210
  • 3
  • 26
  • 35
  • Only `get` and `post` are "valid" values for `method` (if you're being strict). Given that you're not using either, you could remove it (I'm not sure if it's required). You would normally leave it there for graceful degradation - see this post for more info: https://stackoverflow.com/questions/2550431/what-is-the-difference-between-progressive-enhancement-and-graceful-degradation – freedomn-m Oct 31 '18 at 09:55
  • That's correct. `get` and `post` is the only valid values. But since the `method`-attribute is omited in a lot of toturials dealing with ajax, I was curious if it was possible to use the same attribute as a jquery identifier instead. – ThomasK Oct 31 '18 at 10:37

0 Answers0