Form submit inside div with empty action

Just my problem is that I want to submit the form in a div with an empty action as a PHP script on the same page, a simple example to illustrate, since my page is too long

<html> <head> <script> $(document).ready(function() { $("#myDiv").delegate("form", "submit", function(event) { $.ajax({ data: $(this).serialize(), type: $(this).attr('method'), url: $(this).attr('action'), success: function(response) { $("#myDiv").slideUp(500, function() { $("#myDiv").html(response); $("#myDiv").slideDown(500); }); } }); return false; }); }); </script> <head> <form method="post" action=""> <p> enter your name</p> <input type="text" name="name" /> <input type="submit" name="save" /> </form> <?php if (isset($_POST['save'])) { echo $_POST['name']; } ?> </html> 

The problem is that the action of the form is empty, and therefore, when I click the "Submit" button, nothing happens, so does anyone know how to solve the problem? PS: it is too difficult to extract the php script into the extrnak file, I am looking for an alternative solution.

+4
source share
4 answers

For .delegate to work, you need to wrap #myDiv around the selector you want to target, since targeting is equivalent:

 $('#myDiv').find('form').on('submit', ... 

In other words, your form should be wrapped inside #myDiv .

+1
source

Tried like this?

 <?php if (isset($_POST)) { echo $_POST['name']; } ?> 
+1
source
 <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 

The current script address will be placed in the action field.

0
source

try using a hash symbol. action="#"

0
source

All Articles