menios Posted December 9, 2007 Share Posted December 9, 2007 My form uses php_self to validate, i also have the query to add the content of the form to my database but how can i add it when the user hits submit and the form is validated ??? <form method="post" action="<?php echo $PHP_SELF; ?>"> code.. <input type="submit" name="Submit" value="Submit"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/80885-form-submit/ Share on other sites More sharing options...
pr0-g33k Posted December 9, 2007 Share Posted December 9, 2007 Im not sure what u mean but is it so that, its just one page for the form and for it to submit? so when the user clicks submit it will use the same page to post the form? if it is then your looking for something like this <?php if(isset($_POST['Submit'])) { // Put the post to database stuff here } else { // Form Goes Here } ?> hope ive helped you mate Quote Link to comment https://forums.phpfreaks.com/topic/80885-form-submit/#findComment-410298 Share on other sites More sharing options...
menios Posted December 9, 2007 Author Share Posted December 9, 2007 well i need to validate my page before submiting it to the database The question is how to submit it after vlidating it? The code for the form quite simple <?php // Create an empty array to hold the error messages. $arrErrors = array(); $pattern = "/^[A-z0-9\._-]+" . "@" . "[A-z0-9][A-z0-9-]*" . "(\.[A-z0-9_-]+)*" . "\.([A-z]{2,6})$/"; //Only validate if the Submit button was clicked. if (!empty($_POST['Submit'])) { // Each time there's an error, add an error message to the error array // using the field name as the key. if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['firstname'])) $arrErrors['fname']= 'Please provide your First Name.'; if (!preg_match("/^[a-zA-Z\-\'\ ]+$/u", $_POST['lastname'])) $arrErrors['lname']= 'Please provide your Last Name.'; if (!preg_match("/^[\w\ \+\-\'\"]+$/",$_POST['address'])) $arrErrors['address_format']= 'Please provide your Address.'; if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['town'])) $arrErrors['town']= 'Please provide your Town.'; if (!preg_match ("/^[a-zA-Z\-\'\ ]+$/u", $_POST['county'])) $arrErrors['county']= 'Please provide your County.'; if (!preg_match ("/^[a-zA-Z0-9_]{6}$/", $_POST['postcode'])) $arrErrors['postcode']= 'Please provide your Post Code.'; if (!preg_match ("/^[0-9_]{10}$/", $_POST['phone'])) $arrErrors['phone']= 'Please provide your Phone Number.'; if (!preg_match($pattern, $_POST['email'])) $arrErrors['email_format'] = 'A valid email format is required.'; if (count($arrErrors) == 0) { // If the error array is empty, there were no errors. // Insert form processing here. } else { // The error array had something in it. There was an error. // Start adding error text to an error string. $strError = '<div class="formerror1"><p><h4><img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt="">Please check the following and try again:</p></h4>'; } } ?> <?php echo $strError; ?> <!-- For every form field, we do the following... Check to see if there's an error message for this form field. If there is, add the formerror class to the surrounding paragraph block. The formerror class contains the highlighted box. Insert the contents of what the user submitted bak into the form field. Check again to see if this field has an error message. If it does, show the error icon and the error message next to the field. --> <form method="post" action="<?php echo $PHP_SELF; ?>" onreset="return confirm('Do you want to reset the form?')"> <tr ><td width="20%"> </td><td ><p<?php if (!empty($arrErrors['fname'])) echo ' class="formerror"'; ?>> <label for="firstname">First Name:</label> <input name="firstname" type="text" id="firstname" size="30" maxlength="50" value="<?php echo $_POST['firstname'] ?>"> <?php if (!empty($arrErrors['fname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['fname'].'</span>'; ?></td> <td width="20%"> </td></tr> </p><br> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['lname'])) echo ' class="formerror"'; ?>> <label for="lastname">Last Name:</label> <input name="lastname" type="text" id="lastname" size="30" maxlength="50" value="<?php echo $_POST['lastname'] ?>"> <?php if (!empty($arrErrors['lname'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['lname'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['address_format'])) echo ' class="formerror"'; ?>> <label for="address"> Address:</label> <input name="address" type="text" id="address" size="40" maxlength="70" value="<?php echo $_POST['address'] ?>"> <?php if (!empty($arrErrors['address_format'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['address_format'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['town'])) echo ' class="formerror"'; ?>> <label for="town"> Town:</label> <input name="town" type="text" id="town" size="30" maxlength="32" value="<?php echo $_POST['town'] ?>"> <?php if (!empty($arrErrors['town'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['town'].'</span>'; ?></td> <td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['county'])) echo ' class="formerror"'; ?>> <label for="county"> County:</label> <input name="county" type="text" id="county" size="30" maxlength="32" value="<?php echo $_POST['county'] ?>"> <?php if (!empty($arrErrors['county'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['county'].'</span>'; ?></td> <td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['postcode'])) echo ' class="formerror"'; ?>> <label for="postcode"> Post Code:</label> <input name="postcode" type="text" id="postcode"size="10" maxlength="10" value="<?php echo $_POST['postcode'] ?>"> <?php if (!empty($arrErrors['postcode'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['postcode'].'</span>'; ?></td><td width="20%"> </td> </tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"'; ?>> <label for="phone">Phone:</label> <input name="phone" type="text" id="phone" value="<?php echo $_POST['phone'] ?>"> <?php if (!empty($arrErrors['phone'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['phone'].'</span>'; ?></tr><td width="20%"> </td></tr> </p> <tr><td width="20%"> </td><td><p<?php if (!empty($arrErrors['email_format'])) echo ' class="formerror"'; ?>> <label for="email">Email:</label> <input name="email" type="text" id="email" size="40" maxlength="80" value="<?php echo $_POST['email'] ?>"> <?php if (!empty($arrErrors['email_format'])) echo '<img src="../images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['email_format'].'</span>'; ?></tr><td width="20%"> </td></tr> </p> <p> <tr><td colspan="2" align="right"> <input id="reset" type="reset" value="Reset"> <input type="submit" name="Submit" value="Submit"></tr></td> </p> </form> and for inserting the data to the db <?php require_once('../inc/global.inc.php'); if (!$conn) { die('Could not connect: ' . mysql_error()); } mysql_select_db("test", $conn); // MySQL DB Name $firstname=mysql_real_escape_string($_POST['firstname']); //This value has to be the same as in the HTML form file $lastname=mysql_real_escape_string($_POST['lastname']); $email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file $phone=mysql_real_escape_string($_POST['phone']); $town=mysql_real_escape_string($_POST['town']); $address=mysql_real_escape_string($_POST['address']); $postcode=mysql_real_escape_string($_POST['postcode']); $county=mysql_real_escape_string($_POST['county']); $sql="INSERT INTO tbl_order (firstname,lastname,email,phone,town,county,address,postcode) VALUES ('$firstname','$lastname','$email','$phone','$town','$county','$address','$postcode')"; /*Customers is the name of the MySQL table where the form data will be saved.*/ if (!mysql_query($sql,$conn)) { die('Error: ' . mysql_error()); } echo "Your Details have been succesfully added to our System."; header("Refresh:200; url=index.php"); echo ("If You are not redirected automatically press here"); mysql_close($con); ?> hope this helps Thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/80885-form-submit/#findComment-410300 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.