TheJoey Posted August 25, 2009 Share Posted August 25, 2009 Hi im very new to this php coding, ive mostly done just html and xhtml. although i know its not the safest way to store information. The situation is that i have a html form with javascript validation on it to make sure it reaches certain validation points such as proper email etc... i just want to place the information on sumbit into a .txt file threw php. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/ Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Firstly, don't *rely* on JavaScript for your validation. JS can be disabled and manipulated by the user, which makes it completely insecure. Always validate with PHP (although feel free to validate with JS as well). Read up on how to use the fwrite function to write the data to a text file. There's good explanation and examples on the PHP manual (see the link). Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905827 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 Thank you for the quick reply. Yeah i know javascript has it major flaws but im just learning, i had a look at the fwrite before i posted here just didnt understand it to well. Say i was going to use a php to store  <form action="text.php" method="post"> <table> <tr> <td>Email:</td> <td><input name="email" value="" type="text" /></td> </tr> </table> <input type="submit" value="Submit" onclick="clientside();return false;" />  Its just if i have a slight example how to use i may be able to try it i tried using the example before and it was just giving me a blank page (example on fwrite) Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905831 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Well just a quick example similar to in the manual:  if (isset($_POST['email'])) {   // some form of email validation   $email = validateEmail($_POST['email']);   // write-to file   $filename = 'path/to/emails.txt';   // attempt to open the file   if ($handle = fopen($filename, 'a'))   {     // attempt to write to the file     if (!fwrite($handle, $email))     {       echo 'Cannot write to file';     }     else     {       echo 'Written to file';     }   }   fclose($handle); } Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905840 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 sorry but may i ask why validateemail is before the post? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905848 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 When i submit my form it just gives me a blank page. Am i doing something wrong? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905856 Share on other sites More sharing options...
jsschmitt Posted August 25, 2009 Share Posted August 25, 2009 Can you provide us with the code you are using? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905858 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 well here it is the code for php im using is the one above. ill add to it once i get it up and going <?php if (isset($_POST['email'])) {   // some form of email validation   $email = validateEmail($_POST['email']);   // write-to file   $filename = 'text.txt';   // attempt to open the file   if ($handle = fopen($filename, 'a'))   {     // attempt to write to the file     if (!fwrite($handle, $email))     {       echo 'Cannot write to file';     }     else     {       echo 'Written to file';     }   }   fclose($handle); } ?>  and in my html file im using <form action="text.php" method="post">  <table>    <tr>     <td>Email:</td>       <td><input name="email" value="" type="text" /></td>    </tr>  </table>  <input type="submit" value="Submit" onclick="clientside();return false;" /> </form>  there is more but its just repeditive code, i can post it if you wish.   Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905866 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 'validateEmail()' isn't actually a built-in PHP function, just with the many, many ways of validating email available out there on the net, I didn't want to complicate the code.. which I guess I actually did. I'm assuming that $handle = fopen($filename, 'a') returned false, or that $_POST['email'] wasn't set - but that's less likely looking at the form HTML. Check the write permissions on the file & directory where your text file is, and add an 'else' clause to see if there's an error returned whilst trying to open the file:    // attempt to open the file   if ($handle = fopen($filename, 'a'))   {     // attempt to write to the file     if (!fwrite($handle, $email))     {       echo 'Cannot write to file';     }     else     {       echo 'Written to file';     }   }   else   {     echo 'Unable to open file';   }  Should point out what's going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905867 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 im having the same problem, when i submit im just getting a blank white page. No error no anything on it. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905873 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Try adding the following to the top of the PHP script: Â error_reporting(E_ALL); ini_set('display_errors', '1'); Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905880 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 Does the website have to be hosted for this to work  and nah the previous code did nothing. Is there another way to go about this? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905886 Share on other sites More sharing options...
Adam Posted August 25, 2009 Share Posted August 25, 2009 Well it needs to be run from a web server that has PHP installed, yeah... And no, you need a server-side programming language capable of file operations. You could install PHP and a web server on your computer quite easily though with some of the packages out there.. LAMP, XAMPP, etc. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905887 Share on other sites More sharing options...
TheJoey Posted August 25, 2009 Author Share Posted August 25, 2009 Told you i was new haha. Ill have to download this tommorrow. Ill tell you how i go. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-905888 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 Parse error: syntax error, unexpected T_ELSE in C:\xampplite\htdocs\part2\text.php on line 25 Â im getting this error with this code <?php $filename = 'text.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/171784-solved-html-forms-php/#findComment-906677 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 There are no 'else' statements (that aren't commented out) in that code. Is that definitely as you have it on the version you're testing? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906687 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 Sorry ive acted like a idiot i was running the wrong script. Fatal error: Call to undefined function validateEmail() in C:\xampplite\htdocs\part2\text1.php on line 5 Â i am know using the script that u first gave me. Since the other one did work im sorry ive been away from this web programming for tolong lol. thanks for your patience Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906691 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 validateEmail as I said before isn't actually a function, I literally just threw it in as an example (in an effort to try to reduce confusion lol). Loads of functions & regular expressions available on the net you can incorporate into the code. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906694 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 ok beautiful it works well. Just know is it possible to make it enter in format such as Email : Email then next time i sumbit its on another line. and is it possible to refer so that it says Email added on my html page. or is that not possible. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906697 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 is it possible to make it enter in format such as Email : Email then next time i sumbit its on another line  Sorry?  and is it possible to refer so that it says Email added on my html page. or is that not possible.  Unless you specify PHP to parse HTML pages it won't, however it's pretty easy to set it up that way. A quick search on Google will find you many, many tutorials I imagine on how to do it. You may need to re-work the code slightly. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906706 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 mr adam i dont think i can use this fwrite as i have more variables then it can take     // attempt to write to the file     if (!fwrite($handle, $email, $fname, $lname, $age, $address, $city)) im getting this error  Warning: fwrite() expects at most 3 parameters, 7 given in C:\xampplite\htdocs\part2\text1.php on line 24 Cannot write to file Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906708 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 Take a look at how it's used in the manual:  http://uk.php.net/fwrite  Basically concatenate the variables before writing to the file, or call fwrite multiple times. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906711 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 do i just you if statements for multiple fwrite or else if's? Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906720 Share on other sites More sharing options...
TheJoey Posted August 26, 2009 Author Share Posted August 26, 2009 Â Â Â Â if (!fwrite($handle, $email) if (!fwrite($handle, $fname) if (!fwrite($handle, $lname) if (!fwrite($handle, $age) if (!fwrite($handle, $ddress) if (!fwrite($handle, $city) Â only getting errors with that Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906734 Share on other sites More sharing options...
Adam Posted August 26, 2009 Share Posted August 26, 2009 Best bet in your case is to join the strings and enter as a single fwrite; which you could then test the success of quite easily. Quote Link to comment https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/#findComment-906741 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.