1

I am currently using php in my html page and there is an action in the same page which gets executed upon form submission. Currently whole form gets reloaded while I want the php action to be run in the background with out reloading. How do I do it. I would also want to clear the text box after submit button is clicked. Following is the code I am currently using

<html> <head> </head> <body> <form action="index.php" method="post" role="form"> <input type="email" placeholder="Enter email" name="email_address"> <button class="btn btn-primary custom-button red-btn">Sign Up</button> </form> <?php if(isset($_POST['email_address']) !(trim($_POST['email_address']) == '')) // do some action } ?> </body> </html> 

Note: Following code worked for me

<script type="text/javascript"> $('#contactForm').submit(function () { $.post("mailer.php",$("#contactForm").serialize(), function(data){ }); return false; }); </script> 
1
  • You should redirect user to other page like thanks.. Commented Apr 21, 2014 at 11:25

3 Answers 3

3

You need to use AJAX for this, for without reloading. Or, if you want to do it without disturbing the page, you need to set target.

Using AJAX

$("form").submit(function(){ $.post($(this).attr("action"), $(this).serialize()); return false; }); 

Using target

<form action="index.php" method="post" role="form" target="_blank"> 
Sign up to request clarification or add additional context in comments.

2 Comments

I would use return false; cautiously - typically preferring e.preventDefault() because returning false will also stop event propagation, might affect other functions if you rely on event bubbling.
$('#contactForm').submit(function () { $.post("mailer.php",$("#contactForm").serialize(), function(data){ }); return false; }); worked for me
1

Using ajax

HTML

<html> <head> </head> <body> <form id="myform"><!--changge--> <input type="email" placeholder="Enter email" name="email_address"> <button class="btn btn-primary custom-button red-btn" id="signup">Sign Up</button> </form> </body> </html> 
<script> $(document).ready(function() { $('#signup').click(function() { $.ajax({ url:'index.php', method:'post', data : $('#myform').serialize(), success:function() { } )}; ); }); </script> 

1 Comment

it gave me syntax error while this worked $.post("mailer.php",$("#contactForm").serialize(), function(data){ });
0

You can try this

<html> <head> </head> <body> <form id="myform" action="index.php"><!--changge--> <input type="email" placeholder="Enter email" name="email_address"> <button class="btn btn-primary custom-button red-btn" id="signup"> Sign Up</button> </form> <script> $(document).ready(function(){ $('#signup').click(function(){ $.post($(this).attr("action"), $("#myform").serialize(),function(response){ alert(response) // you can get the success response return by php after submission success }); )}; }); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.