Writing to File - Please Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phplearner2007
Forum Newbie
Posts: 2
Joined: Sun Jun 06, 2010 9:05 pm

Writing to File - Please Help

Post by phplearner2007 »

Hi Everyone,

For the past 2 days I've been struggling on writing a PHP script that takes info entered into a form that then gets posted to a seperate file. This is the form code:

Code: Select all

<form  id="Form" name="Form" method="post" action="writer1.php">
             <input name="filename1" type="text" class="textfield" id="filename1" />
               <input name="filedescription1" type="text" class="textfield" id="filedescription1" />
As you can see the form asks for two pieces of info. This info is then posted to writer1.php - here's the code:

Code: Select all

<?php
$filename1 = $_POST['filename1'];
$filedescription1 = $_POST['filedescription1'];

$my_file = "inter.php";
$fh = fopen($my_file, 'a') or die("can't open file");
$string_data1 = "$filename1";
$string_data2 = "$filedescription1";

fwrite($fh, $string_data1);
fwrite($fh, $string_data2);
echo "File successfully written";
?>
The above code then writes to another php file called inter.php - here's the code for that:

Code: Select all

<?php 
$var1 = html_entity_decode($string_data1);
$var2 = html_entity_decode($string_data2);
?>
Now all of the above works, the problem being in the inter.php file, I can't get those two variables ($var1 and $var2) to work with anything else I put in that file. Echo statements won't work, nor will HTML work, if I put something into that file, the variable information dissapears or the echo/html statements (or whatever I'm trying to enter actually) just won't work. Basically I'd like to be able to use the take the outputs of $var1 and $var2 and put them wherever I want in the page. For example, I'd like to make a table containing the two variables, like this:

Code: Select all

<table align=center width=100%>
<tr><td>$var1</td><td>$var2</td></tr></table>
Which won't work, nor will something like this work:

Code: Select all

<?php
$var1;
echo "<br>";
$var2;
?>
Etc etc...........any ideas on what's wrong? Thanks in advance!
Last edited by Benjamin on Mon Jun 07, 2010 7:47 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: Writing to File - Please Help

Post by markusn00b »

Huh?

Are you trying to write variables' values to inter.php, or have literal variables written to inter.php. For example, 2 variables, $a = 1, $b = 2. Do you want "1 2" to be written to the file, or do you want something like "$a = 1; $b = 2;" having those variables available to inter.php whenever it is parsed by PHP?

You'll have to clarify a little.
phplearner2007
Forum Newbie
Posts: 2
Joined: Sun Jun 06, 2010 9:05 pm

Re: Writing to File - Please Help

Post by phplearner2007 »

Thanks for the reply markusn00b. What I'd like to do is have 1 and 2 (i.e., the values of $var 1 and $var2) show up on the inter.php page. Right no that works, but it will show up as "12" - meaning I can't format the values or put them in a specific part of the page. For example, if I wanted the values to show up like this to a website vistor:

Variable 1 is equal to 1
Variable 2 is equal to 2

I would normally write something like:

Code: Select all

echo "Variable 1 is equal to";
$var1;
echo "<br>";
echo "Variable 2 is equal to";
$var2;
But this won't work. Any ideas?
Last edited by Benjamin on Mon Jun 07, 2010 7:47 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Writing to File - Please Help

Post by Jonah Bron »

Code: Select all

echo "Variable 1 is equal to " . $var1 . "<br />";
echo "Variable 2 is equal to " . $var2;
Post Reply