Jump to content

[SOLVED] Can't write form fields...


buntus00

Recommended Posts

Hi,

 

I'm a php newbie. I've looked everywhere for a solution. This is killing me.

The idea is to write form data to a file without using a database (flat

file database model)

 

I'm using 3 files:  File 1 (html form) writes to file 2 (php), and file 2 writes

to a 3rd file -an html file (not a .txt file).

 

Problem: While the form works -the raw info from the fields shows up ....

but the field names won't.  I'm using fwrite to do it.

Doubtless the code is bad. But I don't know the code that will do it.

Looked into arrays and stuff, but that's beyond where I am right now. 

 

Any help would be appreciated.

 

File 1: jobform.html

 

 

<form action="postjob.php" method="post">

 

Job title: <input type="text" name="jobtitle">

 

Job Type: <input type="text" name="jobdescription">

 

<input type="submit" value="Send">

 

</form>

 

---------------------------------------------------------------------

File 2: postjob.php

 

<?php

 

$fn = "jobs/body.html";

$file = fopen($fn, "a+");

$size = filesize($fn);

$space = "<br />\r\n";

 

if($_POST['jobtitle']) fwrite($file, $_POST['jobtitle']);

if($_POST['jobdescription']) fwrite($file, $_POST['jobdescription']);

 

$text = fread($file, $size);

fwrite ($file, $space);

fwrite ($file, "");

fclose($file);

 

?>

Link to comment
Share on other sites

instead of

 

$text = fread($file, $size);

fwrite ($file, $space);

fwrite ($file, "");

 

do this:

 

fseek($file, 0, SEEK_END);

fwrite($file, $space);

fwrite($file, "");

 

but you probably don't even need the seek.. since you're opening in append mode

Link to comment
Share on other sites

Thanks for that, Russell. Unfortunately it hasn't worked. If I can be clear. The problem is the names of the field titles (in spite of how I expressed it in the subject line). I can get them to echo to file2.  The problem is that they don't make it over to file 3. That's the bummer. Thanks for trying, though. I appreciate it.

Link to comment
Share on other sites

oh.. lmao use a foreach on post

 

Hi,  lmao. Thanks. As I said, I'm a beginner, but I did manage to resolve this, as I had already working along certain lines. I wasn't familiar with the implementation of the 'foreach' solution you mentioned and you didn't explain. I will check that out though, for a more elegant solution.

 

What I have is doubtless not exactly pretty or elegant, but nevertheless did the trick, until I get to the next level.

This writes directly to the 3rd and last file in the series. I also had a problem with apostrophes appearing in the text as "\"  then I discovered the 'striplashes' solution implemented below. buntus00.

 

<?php

 

$filename = "jobs/body.html";

 

$field1="<h3>Job Number:";

$field2="<h3>Job title:";

 

$fp = fopen ($filename, "a+");

$size = filesize($fn);

 

if ($fp) {

 

fwrite ($fp, $field1);

if($_POST['jobnumber']) fwrite($fp, stripslashes($_POST['jobnumber']));

 

fwrite ($fp, $field2);

if($_POST['jobtitle']) fwrite($fp, stripslashes($_POST['jobtitle']));

 

fclose ($fp);

 

}

 

?>

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.