Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

writing to file

Status
Not open for further replies.

saphiroth

Programmer
Dec 15, 2004
34
0
0
US
Hello,
I am trying to have a user enter some data in a form and then save that data to a text file localy on the server. But the code I have does not work. It says that the variables for the text fields I have defined do not exist. I turned the global variables on and it still says the same thing. Please help! Thank you.
Code:
<?php  
  // Open the file and erase the contents if any
  $fp = fopen('C:test.txt', 'a');  
  // Write the data to the file  
  fwrite($fp, $_POST['$FirstName']);
  //fwrite($fp, $_POST['$LastName']);
  fwrite($fp, "------------------------------");
  // Close the file  
  fclose($fp);
 ?> 



<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form action=="<?=$_SERVER['PHP_SELF']?>"  method="post">
Type your first name: 
<input type="text" name="FirstName" size="20">
<br>Type your last name: 
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit" name="submit">
<a href="Education/EductionMain.htm"></a>
</form>
 
try this instead. you were not testing to see whether the user had submitted the form and so the code was running each time the page loaded even though the form data had not been posted.

Code:
<?php  
if (isset($_POST['submit'])):
  // Open the file and erase the contents if any
  $fp = fopen('C:\test.txt', 'a');  
  // Write the data to the file  
  fwrite($fp, $_POST['$FirstName']);
  //fwrite($fp, $_POST['$LastName']);
  fwrite($fp, "------------------------------");
  // Close the file  
  fclose($fp);
endif;
 ?> 



<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form action="<?=$_SERVER['PHP_SELF']?>"  method="post">
Type your first name: 
<input type="text" name="FirstName" size="20">
<br>Type your last name: 
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit" name="submit">
<a href="Education/EductionMain.htm"></a>
</form>
 
Thank you for your help! But nothing is being writen to the files stil. I am new to php so I'm not sure if the syntax is correct or the variables are defiend correctly. If the global variable option was not set up how would I use the variables?
Thank you
 
Sorry -didn't check your code thoroughly enough. remove the dollar signs from within the square brackets in the fwrite statements.

form fields are retrievable through the $_POST or $_GET (or $_REQUEST) superglobals (depending on action type). they are addressable by $_POST['formfieldname'] syntax. you were referencing an element called $_POST['$formfieldname'] which does not exist.
 
Thank you very much for your help. The program works fine. If I wanted to have a thankyou page come up after the user preses the submit button, where would I put the code and would it be like this:
Code:
<a href="thankyou.htm" target="_self"></a>

Thanks again!
 
what you currently have is a self-processing page. that is to say that the code to process the form and the form itself are on the same page.

with php you can conditionally display the form or any other text you wish, or redirect to a second page. using an anchor, as you suggest, would not work as it is something with which the client must interact, not the server.

example 1: conditional display
Code:
<?php  
if (isset($_POST['submit'])):
  // Open the file and erase the contents if any
  $fp = fopen('C:\test.txt', 'a');  
  // Write the data to the file  
  fwrite($fp, $_POST['$FirstName']);
  //fwrite($fp, $_POST['$LastName']);
  fwrite($fp, "------------------------------");
  // Close the file  
  fclose($fp);

  echo "Thank you for submitting your information";
  $_POST = array(); //reset the post data
  echo "<br/>";
  echo "Click <a href=\"".$_SERVER['PHP_SELF']."\"> here </a> to go back to the form";

else: //and this is the conditional element

 ?> 



<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form action="<?=$_SERVER['PHP_SELF']?>"  method="post">
Type your first name: 
<input type="text" name="FirstName" size="20">
<br>Type your last name: 
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit" name="submit">
<a href="Education/EductionMain.htm"></a>
</form> 
<?
endif;
?>

example 2 - redirect on success
Code:
<?php  
if (isset($_POST['submit'])):
  // Open the file and erase the contents if any
  $fp = fopen('C:\test.txt', 'a');  
  // Write the data to the file  
  fwrite($fp, $_POST['$FirstName']);
  //fwrite($fp, $_POST['$LastName']);
  fwrite($fp, "------------------------------");
  // Close the file  
  fclose($fp);

  header "Location:[URL unfurl="true"]http://www.domain.com/newpage.php");[/URL] //enter the url of the receiving page

else: //and this is the conditional element

 ?> 



<p>To sign up for the Excel workshop please fill out the form below:
</p>
<form action="<?=$_SERVER['PHP_SELF']?>"  method="post">
Type your first name: 
<input type="text" name="FirstName" size="20">
<br>Type your last name: 
<input type="text" name="LastName" size="20">
<br>
<input type="submit" value="submit" name="submit">
<a href="Education/EductionMain.htm"></a>
</form> 
<?
endif;
?>
 
Any reason why the php code would not work when linking to the php page containing the from and code from another page?

Thank you jpadie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top