Jump to content

Where is the random number coming from?


megarad

Recommended Posts

I am a total noob and I created this form that would gather info from a form and display it on a page while writing all the info to a txt file.  The display part works fine and everything is written to the file ok but the php below that writes to the txt file will always output a random 2 digit number on the display page.  What am I doing wrong?  Thanks!

 

<?php
$file=fopen("response.txt","a+") or exit("Unable to open file!");
//Input a line of the file until the end is reached
echo fputs($file, $_POST['name'] . "|" . $_POST['email']. "|" . $_POST['title'] . "|" . $_POST['institute'] . "|" . $_POST['dept'] . "|" . 
$_POST['address1'] . "|" . $_POST['address2'] . "|" . $_POST['city'] . "|" . $_POST['state'] . "|" . $_POST['code'] . "|" . $_POST['csscheme'] . "|" . 
$_POST['csother'] . "|" . $_POST['csexplain']. "|" . $_POST['pshome'] . "|" . $_POST['psexplain'] . "|" . $_POST['realstd'] . "|" . 
$_POST['realother'] . "|" . $_POST['realdescribe'] . "|" . $_POST['calibdate'] . "|" . $_POST['calibrateps'] . "|" . $_POST['calibratecs'] . "|" . $_POST['tracetrans'] . "|" . 
$_POST['dosemeas']. "|" . $_POST['doseexplain'] . "|" . $_POST['doseexplainmore'] . "|" . $_POST['cstype'] . "|" . 
$_POST['csgeom'] . "|" . $_POST['cstemp'] . "|" . $_POST['cstime'] . "|" . $_POST['uncertk2'] . "|" . $_POST['uncertlist'] . "|" . $_POST['uncertps'] . "|" . 
$_POST['uncertrate']. "|" . $_POST['uncerttemp'] . "|" . $_POST['uncertfield'] . "|" . $_POST['uncertco'] . "|" . 
$_POST['uncerttimer'] . "|" . $_POST['uncertother'] . "|" . $_POST['addcomment'] . "\n");
fclose($file);
?>

Link to comment
https://forums.phpfreaks.com/topic/104881-where-is-the-random-number-coming-from/
Share on other sites

You are echoing the fputs() function, which returns the number of bytes written to the file. Just remove the echo.

 

Another thing, your code is awfully repetitive. If you want to write every POSTed value to the file, with a "|" between each and in the same order as the input fields appeared in the HTML, use implode():

 

<?php
$file=fopen("response.txt","a+") or exit("Unable to open file!");
fputs($file, implode('|', $_POST) . "\n");
fclose($file);
?>

Oh, just saw you wanna display what is written - explains the echo :)

 

Just store the data in a variable, then write it to the file, and echo it to the page:

 

<?php
$file=fopen("response.txt","a+") or exit("Unable to open file!");
$data = implode('|', $_POST) . "\n";
fputs($file, $data);
fclose($file);
echo $data;
?>

 

If you want me to explain something better, just ask.

You can unset() the submit element from the array:

 

<?php
$file=fopen("response.txt","a+") or exit("Unable to open file!");
unset($_POST['submit']);//assuming your submit button has the name submit
$data = implode('|', $_POST) . "\n";
fputs($file, $data);
fclose($file);
echo $data;
?>

There was one more thing that I was trying to do with this project.  I wanted the data to write to a file and display (that is solved above thanks), but I was also hoping to have the data forwarded by email to someone.  Through tutorials, and only through separate php files, I was able to send email with data and write to a file, but I can't seem to do both in one. 

 

A simple display of the form data in the above code is not good enough for the display because the way I want it set up is with a question from the form and the answer near it (the goal of the page is to display the data again for the respondent and ask them to review and verify it).  So I set the form to go to a php page that does this and writes to a file (per the code above).  Is there any way to read the data that was just put in the file and email it to me when the respondent clicks the verify button on the data display page?

 

Thanks again,

M

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.