February 5, 201313 yr hi everybody i have a question, im designing a site and on the contact us page ive got a contact form and a newsletter sign up. the newsletter letter sign is almost working, ive got it saving the info to a database and using php to deal with that. im using jquery for the response message on it which is almost there it works if the sign up is succsessful. my main problem is that my contact form is using an external php script for handling and verification etc. is there any way i can set up jquery to use the external form to provide an alert with either ya or nah without the leaving the comtact us page? im still new to this and learning out of books etc while i got along.
February 5, 201313 yr The best way to handle this would be with Ajax. Use jQuery to remotely post to your external php file, and have that return a json response. Here's a pseudo coded example: $.post({ url: '/external.php', data: formdata, sucesss: function(response){ if (response == 'success') { alert('success'); } }, failure: function(){ alert('oops'); } })
February 5, 201313 yr Author thanks very much how can introduce this to my page? this is the contact page sorry for being a dunce lol. <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Contact Us</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content=""> <meta name="author" content=""> <!-- Le styles --> <link href="css/bootstrapcontact.css" rel="stylesheet"> <link href="css/docscontact.css" rel="stylesheet"> <link href="js/google-code-prettify/prettify.css" rel="stylesheet"> <style> body { padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */ } article, aside, figure, footer, header, hgroup, menu, nav, section { display: block; } .element { font-size: 36px; font-family: "Comic sans", cursive; width: ; color: #F00; margin-top: 18px; margin-right: auto; margin-left: auto; margin-bottom: auto; font-style: italic; } .shadow { text-shadow: #FF0 2px 2px 4px; /* color, h-offset, v-offset, blur */ } </style> <link href="css/bootstrap-responsive.css" rel="stylesheet"> <!-- HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!-- Fav and touch icons --> <link rel="apple-touch-icon-precomposed" sizes="144x144" href="ico/apple-touch-icon-144-precomposed.png"> <link rel="apple-touch-icon-precomposed" sizes="114x114" href="ico/apple-touch-icon-114-precomposed.png"> <link rel="apple-touch-icon-precomposed" sizes="72x72" href="ico/apple-touch-icon-72-precomposed.png"> <link rel="apple-touch-icon-precomposed" href="ico/apple-touch-icon-57-precomposed.png"> <link rel="shortcut icon" href="ico/favicon.png"> <style type="text/css"> body,td,th { color: #FF0000; } </style> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> </head> <body> <?php # Script 9.3 - register.php //This script preforms an INSERT query to add a record to the users table. include ('include/navbar.php'); //Check for form submission: if ($_SERVER['REQUEST_METHOD'] == 'POST') { require ('../mysqli_connect.php'); //Connect to the db. $errors = array(); //Initialize an error array. //Check for first name: if (empty($_POST['name'])) { $errors[] = 'You forgot to enter your first name.'; } else { $fn = mysqli_real_escape_string($dbc, trim($_POST['name'])); } //Check for email address: if (empty($_POST['email'])) { $errors[] = 'You forgot to enter you email address.'; } else { $e = mysqli_real_escape_string($dbc, trim($_POST['email'])); } if (empty($errors)) { //Register the database... //make the query: $q = "INSERT INTO subscribers (name, email, registration_date) VALUES ('$fn', '$e', NOW() )"; $r = @mysqli_query ($dbc, $q); //Run the Query. if ($r) { echo "<script>alert('Thank You, You are now registered for the newsletter');</script>"; } else { //public message: echo "<script>alert('<h1>SYSTEM ERROR</h1> <p>You could not be registered due to system error. We apologize for any inconvenience.</p>)' <p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q .'</p></script>"; } } else { echo "<script>alert(<h1>ERROR!</h1> <p>The following error(s) accurred:<br />)</script>"; foreach ($errors as $msg) { echo '<script>alert(" Please enter a valid Email Address and/or Name")</script>'; } echo "<script>alert(</p><p>Please try again.</p><p><br /></p>)</script>"; } mysqli_close($dbc); } ?> <!--Navbar with collapse feature for smaller screens--> <h1 class="element shadow">CONTACT US</h1> <br> <br> <p>You can contact us using any of the methods below and we will do our to respond as soon possible.</p> <br> <a href="http://www.facebook.com/funkymonkeysplay"><img src="img/facebook.png"></a> <br> <a href="http://twitter.com/funkysplay"><img src="img/twitter.png"></a> <br> <p>TELEPHONE:- 01236 435 516</p> <br> <p>CLICK <a href="mailto:enquiries@funkymonkeysplay.co.uk">CONTACT US</a> TO USE YOU OWN E-MAIL OPTIONS</p> <br> <div class="container-fluid"> <form name="htmlform" method="post" action="contacthandler.php"> <table width="450px" border="0" bordercolor="#000000" align="left"> </tr> <tr align="centre"> <td colspan="2"> <h3>CONTACT FORM</h3> </td> </tr> <tr> <td valign="top"> <label for="first_name">First Name *</label> </td> <td valign="top"> <input type="text" name="first_name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top"> <label for="last_name">Last Name *</label> </td> <td valign="top"> <input type="text" name="last_name" maxlength="50" size="30"> </td> </tr> <tr> <td valign="top"> <label for="email">Email Address *</label> </td> <td valign="top"> <input type="text" name="email" maxlength="80" size="30"> </td> </tr> <tr> <td valign="top"> <label for="telephone">Telephone Number</label> </td> <td valign="top"> <input type="text" name="telephone" maxlength="30" size="30"> </td> </tr> <tr> <td valign="top"> <label for="comments">Message *</label> </td> <td valign="top"> <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit"> <p>* indicates required fields</p> </td> </tr> </table> </form> <form action="contact.php" method="post" > <table align="centre"> <tr align="centre"> <td colspan="2"> <h3>NEWSLETTER SIGNUP</h3> </td> </tr> <tr> <td align="right"> Name: </td> <td> <input type="text" name="name" size="15" maxlength="20" /> </td> </tr> <tr> <td align="right"> Email Address: </td> <td> <input type="text" name="email" size="20" maxlength="60" /> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" name="submit" value="Subscribe" /> </td> </tr> </table> </form> </div> <br> <p>A nice handy map incase you need directions.</p> <div class="container-fluid" align="center"> <iframe width="640" height="480" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/?ie=UTF8&t=h&ll=55.862537,-4.023979&spn=0.00289,0.006866&z=17&output=embed"></iframe><br /><small><a href="https://maps.google.com/?ie=UTF8&t=h&ll=55.862537,-4.023979&spn=0.00289,0.006866&z=17&source=embed" style="color:#0000FF;text-align:centre">View Larger Map</a></small> <p>Address: 1 Muiryhall Street, Coatbridge, ML5 3EA</p> </div> <?php include ('include/footer.php'); ?> </body> </html>
February 5, 201313 yr Not a problem, we all have to start somewhere. I've quickly put this together for you, add it to the bottom of your page (before the </body>) <script> // Catch the form being submitted, I added an ID value (contact_form) to the form, as there are two forms on this page $('#contact_form').submit(function(){ // get the data from the form var first_name = $('input[name=first_name]', '#contact_form').val(); var last_name = $('input[name=last_name]', '#contact_form').val(); var email = $('input[name=email]', '#contact_form').val(); var telephone = $('input[name=telephone]', '#contact_form').val(); var comments = $('input[name=comments]', '#contact_form').val(); // build the data json object to send to the server var data = { first_name: first_name, last_name: last_name, email: email, telephone: telephone, comments: comments } // Send the request to the server $.ajax({ type: 'POST', url:'/contacthandler.php', // Your end point data: data, // our data from the form as json success: function(response){ // If the server response with success do this... // response is the 'returned' data, whatever the page would have rendered alert('Success Message'); } }) .fail(function(response){ // if something went wrong do this... alert("Oops, something went wrong"); }); // stop the form from being submitted return false; }); </script> Edited February 5, 201313 yr by tommy647
Create an account or sign in to comment