elledante Posted April 2, 2008 Share Posted April 2, 2008 Hey guys, so i have two pages one with the form, the other with validation... im having a problem with the validation side of things. all the fields are needed. Any help of what ive done wrong or what i need to do to get it working will be a joy and much appreciated.. Ok so the form code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome Form</title> </head> <body> <form> <form action="welcomeform.php" method="post"> Title: <label> <select name="title" id="title"> <option>Dr</option> <option>Mr</option> <option>Master</option> <option>Mrs</option> <option>Ms</option> <option>Miss</option> </select> </label> <option></option> <br /><br /> First Name: <input type="text" name="firstname" /><br /> Last Name: <input type="text" name="lastname" /><br /><br /> Address 1: <input type="text" name="address1" /><br /><br /> Address 2: <input type="text" name="address2" /><br /><br /> Address 3: <input type="text" name="address3" /><br /><br /> City: <input type="text" name="city" /><br /><br /> County: <input type="text" name="county" /><br /><br /> Postcode: <input type="text" name="postcode" /><br /><br /> Email: <input type="text" name="email" /><br /><br /> Phone: <input type="text" name="phone" /> <br /> <br /><br /> Company Information<br /><br /> Job Title: <input type="text" name="jobtitle" /><br /><br /> Company Name: <input type="text" name="companyname" /><br /><br /> Type Of Business: <label> <select name="business" id="business"> <option>Government Agency</option> <option>Local Government</option> <option>Health</option> <option>Education</option> <option>Manufacturing</option> <option>Financial Services</option> <option>Retail</option> <option>IT</option> <option>Textiles</option> <option>Professional Services</option> <option>Construction</option> </select> </label> <option></option><br /><br /> Number of Employees: <label> <select name="empoyees" id="employees"> <option>10 or fewer</option> <option>11 - 50</option> <option>51 - 10</option> <option>101 - 500</option> <option>501 - 1000</option> <option>1001 - 5000</option> <option>More than 5000</option> </select> </label> <option></option><br /><br /> Purchase Timescale: <label> <select name="empoyees" id="employees"> <option>Immediate</option> <option>3 months</option> <option>6 months</option> <option>1 year</option> <option>more than 1 year</option> </select> </label> <option></option><br /><br /> <input type="submit" value="Submit" > </form> </body> </html> ok and the validation thing ive started but not finished with the fields ( casue i cant get it to work is) <html> <form> <form action="welcomeform.php" method="get"> <?PHP $result .= "First Name: " . $firstname . "\r\n"; $result .= "Last Name: " . $lastname . "\r\n"; $result .= "address1: " . $address1 . "\r\n"; $result .= "address2: " . $address2 . "\r\n"; $result .= "address3: " . $address3. "\r\n"; $result .= "city: " . $city . "\r\n"; $result .= "county: " . $county . "\r\n"; $result .= "postcode: " . $postcode . "\r\n"; //below are rules- need to be completed if (!isset ($_POST ['awelcomeform']) ¦¦ empty($_POST['awelcomeform'])) $errmsg .= "<p>You cannot Submit a blank form. Please fill in the required fields to continue.</p>"; if (!isset($_POST['firstname']) ¦¦ empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>"; if (!isset($_POST['lastname']) ¦¦ empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>"; }else { echo "The form has been submitted. Thankyou!"; h2>Cookies</h2> <?= all_keys($_COOKIE) ?> </form> </form> </html> you can email me directly if you like. elledante@hotmail.co.uk thanks guys Ashleigh x Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/ Share on other sites More sharing options...
Guardian-Mage Posted April 2, 2008 Share Posted April 2, 2008 Simple problem. name has been depreciated, use the id attribute instead for all of your forms. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507486 Share on other sites More sharing options...
elledante Posted April 2, 2008 Author Share Posted April 2, 2008 Sorry, im not quite sure what you mean... i am really stupid and it will probably be one of those things where i kick myself aftewards. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507493 Share on other sites More sharing options...
Cep Posted April 2, 2008 Share Posted April 2, 2008 The name attribute (name="something") should now be the id attribute (id="something") on your elements where it is needed. That is what he is saying. By the way please use the code tags. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507499 Share on other sites More sharing options...
elledante Posted April 2, 2008 Author Share Posted April 2, 2008 ah ok. i think i've done it now. However when i hit the submit button when i test it, it goes back to the form again rather than submitting error messages etc...not sure what im missing... appologise for not using the code tags also Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507511 Share on other sites More sharing options...
Cep Posted April 2, 2008 Share Posted April 2, 2008 Can you post the code for welcomeform.php? Both your HTML forms are pointing to it. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507611 Share on other sites More sharing options...
elledante Posted April 2, 2008 Author Share Posted April 2, 2008 yeah the code for the welcome.php is <html> <form> <form action="welcomeform.php" method="get"> <?PHP $result .= "First Name: " . $firstname . "\r\n"; $result .= "Last Name: " . $lastname . "\r\n"; $result .= "address1: " . $address1 . "\r\n"; $result .= "address2: " . $address2 . "\r\n"; $result .= "address3: " . $address3. "\r\n"; $result .= "city: " . $city . "\r\n"; $result .= "county: " . $county . "\r\n"; $result .= "postcode: " . $postcode . "\r\n"; //below are rules- need to be completed if (!isset ($_POST ['awelcomeform']) ¦¦ empty($_POST['awelcomeform'])) $errmsg .= "<p>You cannot Submit a blank form. Please fill in the required fields to continue.</p>"; if (!isset($_POST['firstname']) ¦¦ empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>"; if (!isset($_POST['lastname']) ¦¦ empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>"; }else { echo "The form has been submitted. Thankyou!"; h2>Cookies</h2> <?= all_keys($_COOKIE) ?> </form> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-507887 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 ok i have added a bit more to this but it's stil not doing what i need it to. It should come up with error message when there is a blank field. When it is filled in fully then it is supposed to have a thankyou message and store the information submitted. I've added a bit more to my code hence reposting of it but i do still have the same problem where all it does is keep submitting the form again so i know i must have gone wrong somewhere. Anyway my revised code is below... thanks for your help guys. <?php $result .= "First Name: " . $firstname . "\r\n"; $result .= "Last Name: " . $lastname . "\r\n"; $result .= "address1: " . $address1 . "\r\n"; $result .= "address2: " . $address2 . "\r\n"; $result .= "address3: " . $address3. "\r\n"; $result .= "city: " . $city . "\r\n"; $result .= "county: " . $county . "\r\n"; $result .= "postcode: " . $postcode . "\r\n"; ?> <html> <form> <form action="welcomeform.php" method="get"> <?PHP if (!isset ($_POST ['awelcomeform']) ¦¦ empty($_POST['awelcomeform'])) $errmsg .= "<p>You cannot Submit a blank form. Please fill in the required fields to continue.</p>"; if (!isset($_POST['firstname']) ¦¦ empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>"; if (!isset($_POST['lastname']) ¦¦ empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>"; if (!isset($_POST['address1']) ¦¦ empty($_POST['address1'])) $errmsg .= "<p>Please enter your address fully</p>"; if (!isset($_POST['address2']) ¦¦ empty($_POST['address2'])) $errmsg .= "<p>Please enter your address fully</p>"; if (!isset($_POST['postcode']) ¦¦ empty($_POST['postcode'])) $errmsg .= "<p>Please enter your postcode</p>"; if (!isset($_POST['phone']) ¦¦ empty($_POST['phone'])) $errmsg .= "<p>Please enter your phone number</p>"; if (!isset($_POST['email']) ¦¦ empty($_POST['email'])) $errmsg .= "<p>Please enter your email address </p>"; if (!isset($_POST['company name']) ¦¦ empty($_POST['companyname'])) $errmsg .= "<p>Please enter your company name</p>"; }else { echo "The form has been submitted. Thankyou!"; h2>Cookies</h2> <?= all_keys($_COOKIE) ?> </form> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508279 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 try this <?php $result .= "First Name: " . $firstname . "\r\n"; $result .= "Last Name: " . $lastname . "\r\n"; $result .= "address1: " . $address1 . "\r\n"; $result .= "address2: " . $address2 . "\r\n"; $result .= "address3: " . $address3. "\r\n"; $result .= "city: " . $city . "\r\n"; $result .= "county: " . $county . "\r\n"; $result .= "postcode: " . $postcode . "\r\n"; ?> <html> <!-- //No need for the form! <form> <form action="welcomeform.php" method="get"> --> <?php $errmsg = ""; if (empty($_POST['awelcomeform'])) $errmsg .= "<p>You cannot Submit a blank form. Please fill in the required fields to continue.</p>"; if (empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>"; if (empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>"; if (empty($_POST['address1'])) $errmsg .= "<p>Please enter your address fully</p>"; if (empty($_POST['address2'])) $errmsg .= "<p>Please enter your address fully</p>"; if (empty($_POST['postcode'])) $errmsg .= "<p>Please enter your postcode</p>"; if (empty($_POST['phone'])) $errmsg .= "<p>Please enter your phone number</p>"; if (empty($_POST['email'])) $errmsg .= "<p>Please enter your email address </p>"; if (empty($_POST['companyname'])) $errmsg .= "<p>Please enter your company name</p>"; if(!empty($errmsg)) { echo $errmsg; }else { echo "The form has been submitted. Thankyou!"; } ?> <h2>Cookies</h2> <?php echo "<pre>";print_r($_COOKIE);echo "</pre>"; ?> <!-- //Again why the form/. </form> </form> --> </html> Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508416 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 it still goes back to displaying just the form with no error messages. Am i missing anything off the form at all? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome Form</title> </head> <body> <form> <form action="welcomeform.php" method="post"> Title: <label> <select name="title" id="title"> <option>Dr</option> <option>Mr</option> <option>Master</option> <option>Mrs</option> <option>Ms</option> <option>Miss</option> </select> </label> <option></option> <br /><br /> First Name: <input type="text" id="firstname" /><br /> Last Name: <input type="text" id="lastname" /><br /><br /> Address 1: <input type="text" id="address1" /><br /><br /> Address 2: <input type="text" id="address2" /><br /><br /> Address 3: <input type="text" id="address3" /><br /><br /> City: <input type="text" id="city" /><br /><br /> County: <input type="text" id="county" /><br /><br /> Postcode: <input type="text" id="postcode" /><br /><br /> Email: <input type="text" id="email" /><br /><br /> Phone: <input type="text" id="phone" /> <br /> <br /><br /> Company Information<br /><br /> Job Title: <input type="text" id="jobtitle" /><br /><br /> Company Name: <input type="text" id="companyname" /><br /><br /> Type Of Business: <label> <select name="business" id="business"> <option>Government Agency</option> <option>Local Government</option> <option>Health</option> <option>Education</option> <option>Manufacturing</option> <option>Financial Services</option> <option>Retail</option> <option>IT</option> <option>Textiles</option> <option>Professional Services</option> <option>Construction</option> </select> </label> <option></option><br /><br /> Number of Employees: <label> <select name="empoyees" id="employees"> <option>10 or fewer</option> <option>11 - 50</option> <option>51 - 10</option> <option>101 - 500</option> <option>501 - 1000</option> <option>1001 - 5000</option> <option>More than 5000</option> </select> </label> <option></option><br /><br /> Purchase Timescale: <label> <select name="empoyees" id="employees"> <option>Immediate</option> <option>3 months</option> <option>6 months</option> <option>1 year</option> <option>more than 1 year</option> </select> </label> <option></option><br /><br /> <input type="submit" value="Submit" > </form> </body> </html> i have a deadline for this tomorrower and im just no good with php. ive spent weeks on this lol. i think i should go back to pen and paper hahah Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508427 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 form needs the name set NOT just ID's Simple problem. name has been depreciated, use the id attribute instead for all of your forms. since when has name been depreciated ? name has its own use and id has it own use many complex forms CSS & javascript use both! i put all the code into one files try this <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Welcome Form</title> </head> <body> <?php $showform = true; if(isset($_POST['Submit'])) { $errmsg = ""; if (empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>"; if (empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>"; if (empty($_POST['address1'])) $errmsg .= "<p>Please enter your address fully</p>"; if (empty($_POST['address2'])) $errmsg .= "<p>Please enter your address fully</p>"; if (empty($_POST['postcode'])) $errmsg .= "<p>Please enter your postcode</p>"; if (empty($_POST['phone'])) $errmsg .= "<p>Please enter your phone number</p>"; if (empty($_POST['email'])) $errmsg .= "<p>Please enter your email address </p>"; if (empty($_POST['companyname'])) $errmsg .= "<p>Please enter your company name</p>"; if(!empty($errmsg)) { echo "<p>Please fill in the required fields to continue.</p>".$errmsg; }else { $showform = false; $result = ""; $result .= "First Name: " . $_POST['firstname'] . "\r\n"; $result .= "Last Name: " . $_POST['lastname'] . "\r\n"; $result .= "address1: " . $_POST['address1'] . "\r\n"; $result .= "address2: " . $_POST['address2'] . "\r\n"; $result .= "address3: " . $_POST['address3']. "\r\n"; $result .= "city: " . $_POST['city'] . "\r\n"; $result .= "county: " . $_POST['county'] . "\r\n"; $result .= "postcode: " . $_POST['postcode'] . "\r\n"; echo "The form has been submitted. Thankyou!"; echo nl2br($result); } } if($showform){ ?> <!-- action="welcomeform.php" // removed this as i'm using single file! --> <form method="post"> Title: <label> <select name="title" id="title"> <option>Dr</option> <option>Mr</option> <option>Master</option> <option>Mrs</option> <option>Ms</option> <option>Miss</option> </select> </label> <option></option> <br /><br /> First Name: <input type="text" name="firstname" /><br /> Last Name: <input type="text" name="lastname" /><br /><br /> Address 1: <input type="text" name="address1" /><br /><br /> Address 2: <input type="text" name="address2" /><br /><br /> Address 3: <input type="text" name="address3" /><br /><br /> City: <input type="text" name="city" /><br /><br /> County: <input type="text" name="county" /><br /><br /> Postcode: <input type="text" name="postcode" /><br /><br /> Email: <input type="text" name="email" /><br /><br /> Phone: <input type="text" name="phone" /> <br /> <br /><br /> Company Information<br /><br /> Job Title: <input type="text" name="jobtitle" /><br /><br /> Company Name: <input type="text" name="companyname" /><br /><br /> Type Of Business: <label> <select name="business" id="business"> <option>Government Agency</option> <option>Local Government</option> <option>Health</option> <option>Education</option> <option>Manufacturing</option> <option>Financial Services</option> <option>Retail</option> <option>IT</option> <option>Textiles</option> <option>Professional Services</option> <option>Construction</option> </select> </label> <option></option><br /><br /> Number of Employees: <label> <select name="empoyees" id="employees"> <option>10 or fewer</option> <option>11 - 50</option> <option>51 - 10</option> <option>101 - 500</option> <option>501 - 1000</option> <option>1001 - 5000</option> <option>More than 5000</option> </select> </label> <option></option><br /><br /> Purchase Timescale: <label> <select name="empoyees" id="employees"> <option>Immediate</option> <option>3 months</option> <option>6 months</option> <option>1 year</option> <option>more than 1 year</option> </select> </label> <option></option><br /><br /> <input type="submit" name="Submit" value="Submit" > </form> </body> </html> <?php } ?> <h2>Cookies</h2> <?php echo "<pre>";print_r($_COOKIE);echo "</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508462 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 im still having the same problem. I dont know if its my computer or what lol. i tested it using wamp , i tried to test it in ie but it wont even open lol... Technology tut tut... Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508465 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 create a new file called test.php (for example) now paste the code above in (NO EDITS) if should give you a form click submit (get the error) fill ing the form submit (get the responce) try that and post what fails Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508469 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 ok i did that- chreated new page . called it test.php copied and pasted it all with no changes... tested it straight from dreamweaver- displayed th html form ok but also displayed all the php code on the webpage also as if it was regular text... put it on wamp and tested it that way and it does the same. ( as if it isnt recognising the php side of it) i will try add a screenshot in a mo. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508477 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 how are you opening it in the browser ? it should be like this http://localhost/test.php and NOT like this file://blar/test.php Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508478 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 i normally go via firefox and type c:wamp\www\test.php which loads it... if i have http at the begining it wont load. oh thanks for being patient with me i am really gratefully. Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508483 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 in the systems tray (bottom right near the clock) you should see a icon (bit like a speed'o'meter ) thats WAMP it should be all white if its yellow or red theirs a problem with WAMP, (if you hover the mouse over it, it should say something like WAMP5 all services running) now left click it and a contextual menu should appear, click localhost this should start the web browser at "http://localhost/" left click it again and select "www directory" that should open a folder ie "C:\wamp\www", now in that folder create the test.php file then goto the browser "http://localhost/" and refresh you should now see the file test.php, click that and it should work (wow thats alot of "should"s ) Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508486 Share on other sites More sharing options...
elledante Posted April 3, 2008 Author Share Posted April 3, 2008 Im kicking myself - (im naturally blonde).. not an excuse but man i love you lol... it all works except for the cookies array at the bottom but i can just take that out i suppose haha... at least that problems solved... is there a way of storing the information that is submitted... I've not even researched on it yet ... got to go to work in a mo so i may log off till later... again thanks for your help you've been great... i owe ya 1 lol Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508493 Share on other sites More sharing options...
MadTechie Posted April 3, 2008 Share Posted April 3, 2008 normally i would suggest writing to a database but then you have to setup one (see myphpadmin (also in the contextual menu)) you could just write to a file <?php $filename = 'test.txt'; $somecontent = "Add this to the file\n"; // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // In our example we're opening $filename in append mode. // The file pointer is at the bottom of the file hence // that's where $somecontent will go when we fwrite() it. if (!$handle = fopen($filename, 'a')) { echo "Cannot open file ($filename)"; exit; } // Write $somecontent to our opened file. if (fwrite($handle, $somecontent) === FALSE) { echo "Cannot write to file ($filename)"; exit; } echo "Success, wrote ($somecontent) to file ($filename)"; fclose($handle); } else { echo "The file $filename is not writable"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99187-solved-new-to-php-need-form-help/#findComment-508505 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.