learningPHP1 Posted February 6, 2014 Share Posted February 6, 2014 Hello, I'm working on a small form script which submits the form without refreshing the whole page. its a email form which basically calles sendemail.php and returns either "done" or "failed". NOt yet scripted to send, just testing. I been at it for 3 days with no success and wondering if someone can help me fix the problem. Any help you can provide would be greatly appreciated... Thanks <!doctype html> <html> <head> <title>Submit form without refreshing the page</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" </script> <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script> $(function() { $("#send").click(function(e) { e.preventDefault(); $.ajax( { type: "POST", url: "sendemail.php", data: $("#myform").serialize(), success: function(response) { if(response == "done") { alert("Form submitted successfully!"); } else { alert("Form submission failed!"); } } }); }); }); </script> </head> <body> <form id="myform" > Name: <br /><input type="text" name="name" id="name" /><br /> Email: <br /><input type="text" name="email" id="email" /> <br /> Message: <br /><textarea name="msg" id="msg"></textarea> <br /> <input type="submit" id="send" class="send" value="Submit" /> </form> </body> </html> small php script <?php $name = $_POST['name']; $message = $_POST['msg']; if(!empty($name) && !empty($message)) { //Do your MySQL or whatever you wanna do with received data //Do not forget to echo "done" when action was completed successfully. echo "done"; } else { echo "fail"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/285994-submit-a-form-without-refresh/ Share on other sites More sharing options...
gristoi Posted February 6, 2014 Share Posted February 6, 2014 you have said what you are trying to do, but what is the actual issue you are having? Quote Link to comment https://forums.phpfreaks.com/topic/285994-submit-a-form-without-refresh/#findComment-1467961 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.