Jump to content

handley

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Everything posted by handley

  1. Hi, ive tested the code that you modified now and it works great! thanks alot for the help it is much appreciated! I have only just started with php but been coding html for ages so I can understand what the code is doing but I haven't fully figured out how to modify certain things! Thanks again!
  2. Cheers for the help! Yeah I uncommented the captcha parts while I posted this thread, cheers for commenting them out! Will test this in the morning! Thanks again!
  3. Im trying to modify the script so that when there is an validation error it flags that error but instead of stopping on the 'die' and just showing the one error it runs the whole script and then posts all errors to the user. I guess i'm looking for a replacement for the 'die'? Sorry for not making it clear! been a long day!
  4. Hi all! Im trying to modify a script so that when a user submits data on a form if there are any errors it flags all errors and not just the first one the script comes to and 'dies'. Heres the form code <?php session_start(); ?> <!--//-----------------------------------------------------------------------------------------------+ | // Sample AJAX web form By: Codex-m | // http://www.php-developer.org | // You are free to use and improve this script provided you link to author web site: http://www.php-developer.org | //--------------------------------------------------------------------------------------------------------------> <html> <head> <title>Sample AJAX Web Form</title> <script type="text/javascript" src="prototype.js"></script> <script type="text/javascript"> function sendRequest() { new Ajax.Request("ajaxvalidate.php", { method: 'post', parameters: 'name='+$F('name')+'&phonenumber='+$F('phonenumber')+'&age='+$F('age')+'&captcha='+$F('captcha'), onComplete: showResponse }); } function showResponse(req){ $('show').innerHTML= req.responseText; } </script> <style type="text/css"> P.yellow {background-color: #ffff00;} </style> </head> <body> <h3>This is a sample PHP web form with AJAX validation</h3> Please complete the form below and then press "Submit button".<br /> <br /><br /> <form action="/ajaxwebform/ajaxvalidate.php" method="post" onsubmit="return false;"> Enter your Full Name, First name and Last name ONLY (Example: John Doe)<br /> <input style="background-color: #FFFFC0" type="text" name="name" id="name" size="50"> <br /><br /> Enter your Phone number (numbers only, no dashes and spaces)<br /> <input style="background-color: #FFFFC0" type="text" name="phonenumber" id="phonenumber" size="35"> <br /><br /> Enter your Age (Numbers only)<br /> <input style="background-color: #FFFFC0" type="text" name="age" id="age" size="50"> <br /><br /> <img src="/ajaxwebform/captcha.php" /> <br /> Enter the Captcha as shown above: <br /> <br /> <input style="background-color: #FFFFC0" type="text" name="captcha" id="captcha" size="10"> <br /> <br /> <input type="submit" value="Submit" onClick="sendRequest()"> </form> <br /> This form will validate your data entry using AJAX. If you are correct with your data entry, the entire information entered will be shown in this page without reloading. <br /> <br /><br /> <p class="yellow" id="show"></p> </body> </html> Heres the php validating code <?php session_start(); ?> <!--//-----------------------------------------------------------------------------------------------+ | // Sample AJAX web form By: Codex-m | // http://www.php-developer.org | // You are free to use and improve this script provided you link to author web site: http://www.php-developer.org | //--------------------------------------------------------------------------------------------------------------> <?php //start session to recover captcha answers //connect to database $username = "root"; $password = ""; $hostname = "localhost"; $database = "newdb"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db($database,$dbhandle) or die("Could not select $database"); //extract form inputs $name =$_POST['name']; $phonenumber =$_POST['phonenumber']; $age =$_POST['age']; $usercaptchaanswer =$_POST['captcha']; $correctcaptcha = $_SESSION['answer']; //sanitize early for any possible MySQL entry $name = mysql_real_escape_string(stripslashes(trim($name))); $phonenumber = mysql_real_escape_string(stripslashes(trim($phonenumber))); $age = mysql_real_escape_string(stripslashes(trim($age))); $usercaptchaanswer = mysql_real_escape_string(stripslashes(trim($usercaptchaanswer))); $correctcaptcha = mysql_real_escape_string(stripslashes(trim($correctcaptcha))); Validate captcha entry if ($correctcaptcha != $usercaptchaanswer) { echo 'ERROR: You have entered wrong captcha code'; die (); } //Step 2 of data validation: check for blank entries if (empty($name)) { //name field is blank echo 'ERROR: The name field is empty.'; die (); } if (empty($phonenumber)) { //phone field is blank echo 'ERROR: The phone field is empty.'; die (); } if (empty($age)) { //age field is blank echo 'ERROR: The age field is empty.'; die (); } if (empty($usercaptchaanswer)) { captcha field is blank echo 'ERROR: The captcha field is empty.'; die (); } //Step 3 of data validation: Ensuring correct format //Step 3.1 validate full name, presence of first name and last name $mystring = $name; $findme = ' '; $pos = strpos($mystring, $findme); $actualposition =$pos + 1; //count strings $count =strlen($mystring); //count the number of characters for the first name $countfirstname = $actualposition - 1; //count the number of characters for the last name $countlastname = $count - $actualposition; //detect if full name has middle name $posmid= strpos($mystring,$findme,$actualposition) + 1; $middleadjust = $posmid - $actualposition; $purealpha = str_replace(" ", "x", $mystring); if ($actualposition==1) { echo 'ERROR: You either forgot your first name or your last name.'; die (); } if ($countfirstname <2) { echo 'ERROR: You are using an invalid first name, it should contain more than one character.'; die (); } if ($countlastname <2) { echo 'ERROR: You are using an invalid last name, it should contain more than one character.'; die (); } if ($middleadjust >= 2) { echo 'ERROR: You should not be using a middle name, please use only first name and last name.'; die (); } if (!(ctype_alpha($purealpha))) { echo 'ERROR: Full name can only consist of alphabetic characters.'; die (); } //Step 3.2 Validate phone number if (!(ctype_digit($phonenumber))) { echo 'ERROR: A phone number should consist of numerical digits only and no spaces between numbers'; die (); } //Step 3.3 Validate age if (!(ctype_digit($age))) { echo 'ERROR: Your age should consist of numerical digits only and no spaces between numbers'; die (); } //Step 4. If all data entry is correct, show results in another page, assigned all values to sessions echo 'Thank you for the correct data entry, below are the information entered:'; echo '<br />'; echo 'Your full name is: '.$name; echo '<br />'; echo 'Your phone number is: '.$phonenumber; echo '<br />'; echo 'Your age is: '.$age; echo '<br />'; echo '<a href="/ajaxwebform/ajaxwebformtest.php">Click here to enter another data.</a>'; $_SESSION = array (); session_destroy (); mysql_close($dbhandle); ?>
  5. Works perfectly!! Thanks alot for the help, its much appreciated!
  6. I've changed the code but im getting a parse error on this line include('birthdays_insert_record.php'): Ive checked to see if the filename is correct which it is in all files. Any ideas?
  7. Hi, I've got a basic sign up form but I want a drop down list which will list different catergories that relate to different tables which when selected will input the sign up information into that table which was selected from the catergory drop down. This is the signup form <html><head><title>Birthdays Insert Form</title> <style type="text/css"> td {font-family: tahoma, arial, verdana; font-size: 10pt } </style> </head> <body> <table width="300" cellpadding="5" cellspacing="0" border="2"> <tr align="center" valign="top"> <td align="left" colspan="1" rowspan="1" bgcolor="64b1ff"> <h3>Insert Record</h3> <form method="POST" action="test.php"> <? print "Enter Company Name: <input type=text name=company_name size=30><br>"; print "Enter Contact Name: <input type=text name=contact_name size=30><br>"; print "Enter Telephone: <input type=text name=telephone size=20><br>"; print "Enter Fax: <input type=text name=fax size=30><br>"; print "Enter Email: <input type=text name=email size=30><br>"; print "Enter Address: <input type=text name=address1 size=20><br>"; print "Enter Address: <input type=text name=address2 size=30><br>"; print "Enter Postcode: <input type=text name=postcode size=30><br>"; print "Enter Town / City: <input type=text name=town_city size=20><br>"; print "Enter Website: <input type=text name=website size=30><br>"; print "Enter Company Type: <select name='table'> <option>stationary</option><option>reception</option></select><br>"; print "<br>"; print "<input type=submit value=Submit><input type=reset>"; ?> </form> </td></tr></table> </body> </html> This is the part which I can't figure out and is probably totally wrong! Im trying to use this script to sort the drop down list to then run the correct script to insert the form data. <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Hello!</title> </head> <body> <?php if($_POST['table']=='stationary' 'birthdays_insert_record.php') else if($_POST['table']=='reception' 'insert_reception.php') ?> </body> </html> This is the script which works! that inserts the form data into a specific table <html><head><title>Birthdays Insert Record</title></head> <body> <? /* Change db and connect values if using online */ $company_name=$_POST['company_name']; $contact_name=$_POST['contact_name']; $telephone=$_POST['telephone']; $fax=$_POST['fax']; $email=$_POST['email']; $address1=$_POST['address1']; $address2=$_POST['address2']; $postcode=$_POST['postcode']; $town_city=$_POST['town_city']; $website=$_POST['website']; $db="myflawlesswedding"; $link = mysql_connect('localhost', 'root' , ''); if (! $link) die(mysql_error()); mysql_select_db($db , $link) or die("Select Error: ".mysql_error()); $result=mysql_query("INSERT INTO reception (company_name, contact_name, telephone, fax, email, address1, address2, postcode, town_city, website) VALUES ( '$company_name', '$contact_name', '$telephone', '$fax', '$email', '$address1', '$address2', '$postcode', '$town_city', '$website')") or die("Insert Error: ".mysql_error()); mysql_close($link); print "Record added"; ?> <form method="POST" action="birthdays_insert_form.php"> <input type="submit" value="Insert Another Record"> </form> <br> <form method="POST" action="birthdays_dbase_interface.php"> <input type="submit" value="Dbase Interface"> </form> </body> </html> I hope somebody can help me out here! or can point me in a better way to sort this problem! Thanks for any advice!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.