Jump to content

PHP with Javascript HELP!!


blang

Recommended Posts

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!";

}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.