Jump to content

Reading from text file


mappers

Recommended Posts

I'm rather new to PHP so be gentle  :)

 

This is probably very simple but it seems to be eluding me and is somewhat frustrating!

 

I'm trying to write a couple of pages that on the first page there is a simple form with a couple of drop down boxes to select a text file containing product information and then the second to select it's location.

 

In the text file the word location appears and I want this to be replaced by whatever is selected in the second drop down box.

 

I've tried to write it just to display the information on screen but I'm just getting a blank page.

 

Here's my initial form:

 

<form action="mp2.php" method="post">
  <p>File
    <select name="file" id="file">
      <option value="bulbs.txt">Bulbs.txt</option>
      <option value="bulbs2.txt">Bulbs2.txt</option>
      </select>
  </p>
  <p>Location
    <select name="loc" id="loc">
      <option>BIN001</option>
      <option>BIN002</option>
      <option>BIN003</option>
      <option>BIN004</option>
    </select>
</p>
  <p>Submit
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>

 

Heres the second php page:

 

<?php
$filename="$file";
$tempvar = fopen($filename,"r");
$content=fread($tempvar, filesize ($filename));
fclose($tempvar);

$oldWord = "location";
$newWord = "$loc";
$content = str_replace($oldWord , $newWord , $content);
echo "$content";
?>

 

The text files are in the same directory as the other files.

 

I eventually want to be able to write out a new text file with the "location" text changed to what is selected on the first page.

 

I'm learning (albeit rather slowly) and any help would be appreciated.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/49730-reading-from-text-file/
Share on other sites

Unless register globals is on (it probably isn't, and it should stay that way), $file and $loc will not be assigned.

 

Since this form uses the post method you would access them via $_POST....

 

So, to set $loc, for example, you would do something like $loc = $_POST['loc']; since the name of the drop down is loc and you wish to assign it to the loc variable.  The same thing goes with $filename being assigned to the drop down box named 'file'.  As for assigning things, when ever you wish to set one variable to another, quotes are not necessary.  ($x = $y;)

Archived

This topic is now archived and is closed to further replies.

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