blang Posted December 8, 2011 Share Posted December 8, 2011 New to PHP/Java. Need some help!!! I need the textarea of a site to save what is entered into a text file. I also need javascript to use a pre-validate pop up box, if no data is entered. I can't get both of them to work at the same time. Below is my code. <html> <head> <link rel="stylesheet" type="text/css" href="style.css"/> <script type="text/javascript"> <!-- <!CDATA[ function validateForm() { var x=document.forms["myForm"]["email"].value; var atpos=x.indexOf("@"); var dotpos=x.lastIndexOf("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("Not a valid e-mail address"); return false; } } // ]]> --> </script> </head> <form name="form1" method="post" action="index2.php?saving=1"> <textarea name="data" cols="100" rows="10"> Username: Password: --------------------------------------------- </textarea> <br> </form> <form name="myForm" method ="post" action="index2.php?saving=1" onsubmit="return validateForm();"> <tr> <td align="right" width="10%"> Email: </td> <td><input type="text" name="email" id="email" size ="40" /> </td> </tr> <input type="submit" value="Submit"> </form> <p> <a href="index.html">Home</a> </html> PHP CODE: <?php $saving = $_REQUEST['saving']; if ($saving == 1){ $data = $_POST['data']; $file = "data.txt"; $fp = fopen($file, "a") or die("Couldn't open $file for writing!"); fwrite($fp, $data) or die("Couldn't write values to file!"); fclose($fp); echo "Saved to $file successfully!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/ Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 So what's the problem? Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295569 Share on other sites More sharing options...
blang Posted December 8, 2011 Author Share Posted December 8, 2011 If no text is filled in, the java pop up will work...but when you feel the text in, it will not save to the text file...I get an error ( ! ) Notice: Undefined index: data in C:\wamp\www\test\index2.php on line 4 Call Stack # Time Memory Function Location 1 0.0006 369224 {main}( ) ..\index2.php:0 Couldn't write values to Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295571 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 You have no field named data. Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295572 Share on other sites More sharing options...
blang Posted December 8, 2011 Author Share Posted December 8, 2011 Do you mind elobrating a bit more....Where is this missing?? It is supposed to save to a text file I have named data.txt Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295574 Share on other sites More sharing options...
scootstah Posted December 8, 2011 Share Posted December 8, 2011 Oh, now I see. You have two forms. Why do you have two forms? You have a data field in the first form but you are submitting the second form, so the data field is never sent. Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295576 Share on other sites More sharing options...
blang Posted December 8, 2011 Author Share Posted December 8, 2011 I really don't know :-\ This has been driving me crazy! I just want it some how to save to the text file when information is entered and if it's not then to when a user clicks submit, the java pop up opens Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295577 Share on other sites More sharing options...
blang Posted December 8, 2011 Author Share Posted December 8, 2011 Thanks!!!!! I think I just figured it out and got it to work.....Wish I would have came here earlier!! Quote Link to comment https://forums.phpfreaks.com/topic/252716-php-with-javascript-help/#findComment-1295579 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.