adipalmer123 Posted March 28, 2011 Share Posted March 28, 2011 Hi All I have a contact form whcih works great - its has a little validation on it and sends me all the information I need via email. I now want to store all data submitted via the form to be stored in a MySQL database. What is the best way to do this?? Attached is my form as it is, just being emailed to the relevent accounts. Look forward to your replies. Adi [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
gristoi Posted March 28, 2011 Share Posted March 28, 2011 Have you at least attempted to do this yourself? Do you have any tables and a database connection set up. If not then a good place to start is to read up on some basic mysql and php tutorials and at least have a go yourself first. If you have attempted to do this yourself then please post the table structures along with the issues are are having Quote Link to comment Share on other sites More sharing options...
adipalmer123 Posted March 28, 2011 Author Share Posted March 28, 2011 Thanks for the encouragement gristoi. Done some reading and with ease created my table and got the contents of the form inserting nicley. One small problem though. My contact form is on every page of my site - and now a blank form is being sent to my database everytime a link is clicked on the site. Any suggestions to only submit the form once the submit button has been clicked?? Adi Quote Link to comment Share on other sites More sharing options...
gristoi Posted March 28, 2011 Share Posted March 28, 2011 Without seeing how you have written your code I can only guess that you haven't wrapped your code properly . What you need to do is point your form to a processing page ie: <form action=process.php method="post"> Which will send the form to a process.php page. On this page u can capture the form : If($_POST['Submit']{ // process form then redirect back to a page} please note that this is a very raw example and u shouldn't use $_post['submit'] . But should give the submit button a unique name. ( and well done for researching the issue and giving it a go first ) Quote Link to comment Share on other sites More sharing options...
adipalmer123 Posted March 28, 2011 Author Share Posted March 28, 2011 My code is attached The form works great when its sending to email - but now when I've put in the code to store the info in a database its sending blank information everytime a page is clicked. My site works on includes and as you can see in the attached enquiry.php, the form points to index.php I cant see why this is a problem as I dont recieve an email every time a page is clicked. Only get the problem in the database. ______________________________________ <div class="enquiry"> <h2><img src="../images/enquiry.png"/><span>Enquiry Form</span></h2> <p> <?php error_reporting(E_ALL ^ E_NOTICE); /* Subject and Email Variables */ $emailSubject = Example - Customer Enquiry'; $webMaster = "example@gmail.com"; $con=mysql_connect("123.456.450.12", "example123", "password5983") or die ('Cant connect to database because:' . mysql_error()); mysql_select_db("example", $con); $sql="INSERT INTO customers (Name, Address, Phone, Email, Sample, Type_of_Grass, Message) VALUES ('$_POST[name]','$_POST[address]','$_POST[phone]','$_POST','$_POST[sample]','$_POST[type_of_grass]','$_POST[message]')"; if (!mysql_query($sql,$con)) { echo ""; } mysql_close($con); if ($_POST['submit']) { $name = $_POST['name']; $address = $_POST['address']; $phone = $_POST['phone']; $email= $_POST['email']; $message= $_POST['message']; $errorstring = ""; if (!$name) $errorstring = $errorstring."Name<br>"; if (!$address) $errorstring = $errorstring."Address<br>"; if (!$phone) $errorstring = $errorstring."Phone<br>"; if (!$email) $errorstring = $errorstring."Email<br>"; if (!$message) $errorstring = $errorstring."Message<br>"; if ($errorstring=""){ echo "Please fill out the following fields:<br>$errorstring"; }else{ echo ("Thank you $name your enquiry has been sent to us. <p> You will hear back from us shortly. <p> Thank You"); } } /* Gathering Data Variable */ if ($_POST['submit']) { $name = $_POST['name']; $address = $_POST['address']; $phone = $_POST['phone']; $email = $_POST['email']; $sample = $_POST['sample']; $grasstype = $_POST['type_of_grass']; $message = $_POST['message']; $body = <<<EOD <br><hr><br> This is an Enquiry for Everlasting Lawns <br> <br> Name: $name <br> Address: $address <br> Phone Number: $phone <br> Email: $email <br> Request a Sample: $sample <br> Type of Grass: $grasstype <br> Message: $message <br> EOD; $headers = "From: $name\r\n"; $headers .= "Content-type: text/html\r\n"; ini_set("sendmail_from","info@example.co.uk"); $success = mail($webMaster,$emailSubject,"$body",$headers); } ?> <form id="form1" name="form1" method="post" action="../index.php"> <input type="hidden" name="submitted" value="true"/> <p> <label>Your Name<br /> <input name="name" type="text" id="name" size="30" /> </label> </p> <p> <label>Your Address<br /> <input name="address" type="text" id="address" size="30"/> </label> </p> <p> <label>Your Number<br /> <input name="phone" type="text" id="phone" size="30" /> </label> </p> <p> <label>Email<br /> <input type="text" name="email" id="email" size="30"/> </label> </p> <p> <label>Would you like a Free Sample?<br /> </label> <label> <input type="radio" name="sample" value="Yes" id="sample_0" /> Yes</label> <br /> <label> <input type="radio" name="sample" value="No" id="sample_1" /> No</label> </p> <p> <label>Which type of grass would you like?<br /> <select name="type_of_grass" id="type_of_grass"> <option>Please Select</option> <option>Adventure</option> <option>Boat10</option> <option>Decor</option> <option>Party</option> <option>Scenic</option> <option>Step</option> <option>Virgin</option> </select> </label> </p> <p> <label>Message<br /> <textarea cols="23" rows="6" id="message" name="message" value="<?php echo $message ?>"/>Feel Free to ask us a question</textarea> </label> </p> <p> <label> <input type="submit" name="submit" id="submit" value="Submit" /> </label> </form> <p> </p> </div> </div> </div> Does this help any?? Adi Quote Link to comment Share on other sites More sharing options...
adipalmer123 Posted March 28, 2011 Author Share Posted March 28, 2011 Hey all Thanks for your time, but I've been working on it all afternoon and I've managed to do it. Thanks for your help. Adi Quote Link to comment 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.