Page 1 of 1

Hope I can get some help with a quick question

Posted: Wed Mar 05, 2008 1:45 am
by jnkrois
I'm new to PHP, and I've been experimenting with some tutorials and such. I wanted to create a text file from an html form, so far I have been successful.
My question is:
Can I establish a variable value as the name of the txt file that I want to create?
I mean: If the html form retireves a person's name, can I use the name as the text file name that PHP can create for me?
Example

***** PLEASE USE THE

Code: Select all

OR

Code: Select all

TAGS *****[/color]

html code:

Code: Select all

<form action="form.php" method="post" name="form1">
  <label>Name
  <input type="text" name="name" id="name" />
  </label>
 
  <label>
  <input type="submit" name="Submit" id="Submit" value="Submit" />
  </label>
</form>
And then the PHP script that process the form:

Code: Select all

<?php
 
$name = "name";
 
$fp = fopen("[b]john_doe.txt"[/b], "w");
 
fwrite($fp, $your_data);
 
fclose($fp);
?>
 
Thanks in advance for taking questions?
Juan Carlos

Re: Hope I can get some help with a quick question

Posted: Wed Mar 05, 2008 2:22 am
by Christopher
You can get the value of name from the form in PHP from the superglobal array $_POST. The value for name will be in $_POST['name'].

Re: Hope I can get some help with a quick question

Posted: Wed Mar 05, 2008 2:02 pm
by jnkrois
Thanks a lot, I appreciate your help, it worked just fine.

Now, what if I want to create a script that writes into an html file, but to a specific part of the file, let's says a div tag into the body tag? Is that possible?

Thanks again and sorry about adding code the wrong way before.

Juan Carlos

Re: Hope I can get some help with a quick question

Posted: Wed Mar 05, 2008 2:59 pm
by John Cartwright
php5: file_get_contents() + str_replace() + file_put_contents()
php4: file_get_contents() + str_replace() + (fopen(), fwrite(), fclose())