Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  68] [ 4]  / answers: 1 / hits: 29248  / 10 Years ago, mon, april 21, 2014, 12:00:00

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>

More From » php

 Answers
17

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>

[#71368] Friday, April 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;