Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,486 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!




Saving the contents of text boxes to a file

2 Pages V  1 2 >  
Reply to this topicStart new topic

Saving the contents of text boxes to a file

aj32
1 Feb, 2008 - 06:24 AM
Post #1

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
I've been working on a way to have a php page save the contents of some text boxes to a file. Here's what I have done so far:
CODE
function save()
{
<?php

$fh = fopen("settings.ini", "w");

if($fh==false)
    die("unable to create file");

$filename = "settings.ini";
$somecontent = "";

if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    
    if (fwrite($handle, $somecontent) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    
    fclose($handle);
}

else
{
    echo "The file $filename is not writable";
}
?>

}


all of this works fine, but I can't find a way to get the contents of the text boxes, i've tried $textbox1.value, that just saves ".value" to the file. If anyone could tell me how to do this, I would greatly appreciate it (i've been trying to figure this out for 2 days now.)

Thanks!

This post has been edited by aj32: 1 Feb, 2008 - 12:22 PM
User is offlineProfile CardPM
+Quote Post

maryaan
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 11:54 AM
Post #2

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 5


My Contributions
hi wink2.gif it's very easy to understand, add these 2 lines to your script
CODE
var_dump($_POST);
var_dump($_GET);


and submit your form to the server. Server puts all received "variables" to the superglobal array $_POST (or $_GET - depending on form method). That your variable looks like $_POST['textbox1']. wink2.gif
User is offlineProfile CardPM
+Quote Post

aj32
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 12:24 PM
Post #3

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
I didn't get a chance to try that, but won't that save all the text fields to a global variable? I need to save them individually with data parsing codes so the file can be read at a later time.

Thanks!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 01:57 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
The values will be put in an "array". Then you could access each value individually as you need them in your script. It will put all fields into the array yes, but then your script for parsing and processing can choose which items get saved and or parsed and which go to file.

For instance... if I have three html fields...

CODE

<input type="text" name="username" size="30">
<input type="password" name="pass" size="30">
<textarea name="somecomments"></textarea>


The global array will have assuming you used the POST method in your form....

CODE

$_POST["username"] = username they typed
$_POST["pass"] = password they typed
$_POST["somecomments"] = comments they typed


Then your script can write which ever field you want to the file, parse whatever content you want. The rest will be thrown away.

CODE

<?php

$handle = fopen("myfile.txt","a");
fwrite($handle,$_POST["somecomments"]);
fclose($handle);
?>


Here we are just writing the comments to file. The username and password are not used and will be thrown away at the end of the script's execution.

P.S. Make sure always validate content that comes in through a super global array like this. Make sure the content is of the right type and contains no malicious characters. That way they can't inject SQL queries or anything that will get around security measures. Read up on SQL injection for more details.

Hope that makes some sense. Enjoy! smile.gif

This post has been edited by Martyr2: 1 Feb, 2008 - 01:58 PM
User is online!Profile CardPM
+Quote Post

aj32
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 02:51 PM
Post #5

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
Do I still need to put the
var_dump($_POST);
and
var_dump($_GET);
into the file, and if I do, where exactly?

Thanks, I'm trying to teach myself php, this is like the hardest thing I've come to yet blink.gif !
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 04:04 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
QUOTE(aj32 @ 1 Feb, 2008 - 02:51 PM) *

Do I still need to put the
var_dump($_POST);
and
var_dump($_GET);
into the file, and if I do, where exactly?

Thanks, I'm trying to teach myself php, this is like the hardest thing I've come to yet blink.gif !



The var_dump is for printing the contents of the array so you can see what is being included. It is meant only as a debugging tool and I think maryann was just trying to tell you that if you use the command you can see what is being included. You certainly don't need it in your script. It only shows you what is in the $_POST or $_GET array you are submitting.

Look it up on php.net and you will see it is very much like print_r(). You don't need it for what you are doing, only debugging purposes. smile.gif
User is online!Profile CardPM
+Quote Post

aj32
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 04:27 PM
Post #7

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
Ok, that helps a lot!

I still can't figure out what is wrong here, I am now using the following code, but, the text file does not have any data in it after I submit it?
CODE

function save()
{
<?php
$fh = fopen("settings.ini", "w");
fclose("settings.ini");

$filename = "settings.ini";
$somecontent = $_GET["email"];
$handle = fopen($filename, 'a');

fwrite($handle, $somecontent);
fclose($handle);
?>
}


("email" is the name of the textbox that I want to save to the file.)

-Thanks for helping me through this!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 04:39 PM
Post #8

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,212



Thanked: 216 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Everything PHP is to be defined INSIDE the php tags. Plus I am not sure why you are opening the file and then closing it before opening it again. Try something like this...

CODE

<?php
function save($somecontent)
{
     $filename = "settings.ini";
     $handle = fopen($filename, 'w');

     fwrite($handle, $somecontent);
     fclose($handle);

}


// Here we call the function and pass it the content from our $_GET.
save($_GET["email"]);
?>


Notice how all code is in the PHP tags. Also notice that I call the function and give it the content from the $_GET variable. Make sure you also have a field with the name "email" in your form.

I also set the file open to be "w" here because it will clear the file and then add the content all in one swoop. "a" would just add on to the end of the file. If that is what you wanted, be sure to change that back to "a".

smile.gif

User is online!Profile CardPM
+Quote Post

aj32
RE: Saving The Contents Of Text Boxes To A File
1 Feb, 2008 - 05:00 PM
Post #9

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
QUOTE(Martyr2 @ 1 Feb, 2008 - 07:39 PM) *

Everything PHP is to be defined INSIDE the php tags. Plus I am not sure why you are opening the file and then closing it before opening it again. Try something like this...

CODE

<?php
function save($somecontent)
{
     $filename = "settings.ini";
     $handle = fopen($filename, 'w');

     fwrite($handle, $somecontent);
     fclose($handle);

}


// Here we call the function and pass it the content from our $_GET.
save($_GET["email"]);
?>


Notice how all code is in the PHP tags. Also notice that I call the function and give it the content from the $_GET variable. Make sure you also have a field with the name "email" in your form.

I also set the file open to be "w" here because it will clear the file and then add the content all in one swoop. "a" would just add on to the end of the file. If that is what you wanted, be sure to change that back to "a".

smile.gif



I had it opening & closing & and opening the file because the first part was to clear the file.

Ok, Sorry for my stupidity on this thing, but...

I put the code you gave me into the "settings.php" file like below:

CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

//THE SAVE TO FILE CODE//

<?php
function save($somecontent)
{
     $filename = "settings.ini";
     $handle = fopen($filename, 'w');

     fwrite($handle, $somecontent);
     fclose($handle);
}

save($_GET["email"]);

?>
//***END***//


<script type="text/javascript">
function send()
{
}
</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit User Settings</title>   ...THE REST OF THE FILE...



I must be doing something, probably simple, wrong. Now when I click on the "save" button, I does nothing and showes the little "error on page" text on the stat. bar


I really apprieciate your help. right now stupid.gif when it comes to php!

User is offlineProfile CardPM
+Quote Post

maryaan
RE: Saving The Contents Of Text Boxes To A File
2 Feb, 2008 - 06:12 AM
Post #10

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 5


My Contributions
show whole content of your file, why are you using js to send this form to server?

//
simply add
CODE
<input type="submit" value="Send">
to your form and use it instead the "js-powered" wink2.gif button

This post has been edited by maryaan: 2 Feb, 2008 - 07:46 AM
User is offlineProfile CardPM
+Quote Post

aj32
RE: Saving The Contents Of Text Boxes To A File
2 Feb, 2008 - 01:10 PM
Post #11

D.I.C Addict
Group Icon

Joined: 30 Aug, 2007
Posts: 577



Thanked: 2 times
Dream Kudos: 675
My Contributions
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


<?php
function save($somecontent)
{
     $filename = "settings.ini";
     $handle = fopen($filename, 'w');

     fwrite($handle, $somecontent);
     fclose($handle);
}

// Here we call the function and pass it the content from our $_GET.
save($_GET["email"]);

?>

<head>
<script type="text/javascript">
function send() {
<?php
$email_to = "aj32.foxsoft@hotmail.com";
$email_subject = "Test E-Mail (This is the subject of the E-Mail)";
$email_body = "This is the body of the Email \nThis is a second line in the body!";

mail($email_to, $email_subject, $email_body);
?>
}

//]]>
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit User Settings</title>
<style type="text/css">
<!--
body  {
    font: 100% Verdana, Arial, Helvetica, sans-serif;
    background: #666666;
    margin: 0; /* it's good practice to zero the margin and padding of the body element to account for differing browser defaults */
    padding: 0;
    text-align: center; /* this centers the container in IE 5* browsers. The text is then set to the left aligned default in the #container selector */
    color: #000000;
}
.twoColFixLtHdr #container {
    width: 780px;  /* using 20px less than a full 800px width allows for browser chrome and avoids a horizontal scroll bar */
    background: #FFFFFF;
    margin: 0 auto; /* the auto margins (in conjunction with a width) center the page */
    border: 1px solid #000000;
    text-align: left; /* this overrides the text-align: center on the body element. */
}
.twoColFixLtHdr #header {
    background: #DDDDDD;
    padding: 0 10px 0 20px;  /* this padding matches the left alignment of the elements in the divs that appear beneath it. If an image is used in the #header instead of text, you may want to remove the padding. */
}
.twoColFixLtHdr #header h1 {
    margin: 0; /* zeroing the margin of the last element in the #header div will avoid margin collapse - an unexplainable space between divs. If the div has a border around it, this is not necessary as that also avoids the margin collapse */
    padding: 10px 0; /* using padding instead of margin will allow you to keep the element away from the edges of the div */
}
.twoColFixLtHdr #sidebar1 {
    float: left; /* since this element is floated, a width must be given */
    width: 200px; /* the actual width of this div, in standards-compliant browsers, or standards mode in Internet Explorer will include the padding and border in addition to the width */
    background: #EBEBEB; /* the background color will be displayed for the length of the content in the column, but no further */
    padding: 15px 10px 15px 20px;
}
.twoColFixLtHdr #mainContent {
    margin: 0 0 0 250px; /* the left margin on this div element creates the column down the left side of the page - no matter how much content the sidebar1 div contains, the column space will remain. You can remove this margin if you want the #mainContent div's text to fill the #sidebar1 space when the content in #sidebar1 ends. */
    padding: 0 20px; /* remember that padding is the space inside the div box and margin is the space outside the div box */
}
.twoColFixLtHdr #footer {
    padding: 0 10px 0 20px; /* this padding matches the left alignment of the elements in the divs that appear above it. */
    background:#DDDDDD;
}
.twoColFixLtHdr #footer p {
    margin: 0; /* zeroing the margins of the first element in the footer will avoid the possibility of margin collapse - a space between divs */
    padding: 10px 0; /* padding on this element will create space, just as the the margin would have, without the margin collapse issue */
}
.fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
    float: right;
    margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page */
    float: left;
    margin-right: 8px;
}
.clearfloat { /* this class should be placed on a div or break element and should be the final element before the close of a container that should fully contain a float */
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}
-->
</style>
<!--[if IE 5]>
<style type="text/css">
/* place css box model fixes for IE 5* in this conditional comment */
.twoColFixLtHdr #sidebar1 { width: 230px; }
</style>
<![endif]-->
<!--[if IE]>
<style type="text/css">
/* place css fixes for all versions of IE in this conditional comment */
.twoColFixLtHdr #sidebar1 { padding-top: 30px; }
.twoColFixLtHdr #mainContent { zoom: 1; }
/* the above proprietary zoom property gives IE the hasLayout it needs to avoid several bugs */
</style>
<![endif]-->

</head>

<body class="twoColFixLtHdr">

<div id="container">
  <div id="header">
    <h1>My profile settings:</h1>
  <!-- end #header --></div>
  <div id="sidebar1">
    <p>Available Settings:</p>
    <p>Name</p>
    <p>Age</p>
    <p>Email Address</p>
    <p>AIM ID</p>
    <p>MSN Messenger ID</p>
    <p>===============</p>
    <p>Domain Name</p>
    <p>Password</p>
    <p>Subscription Type</p>
    <p>&nbsp;</p>
  </div>
  <div id="mainContent">
    <p>Setting Window:</p>
    <hr />
    <h1 align="center">General Settings</h1>
    
    <p>-Will take place as soon as you save them</p>
    <div align="left">
      <table width="482" height="153" border="1">
        
        <tr>
          <td width="322" height="26">Name:</td>
          <td width="144"><label>
          <input type="text" name="name" id="name" />          </label></td>
        </tr>
        <tr>
          <td height="26">Age:
            <label></label></td>
          <td>          <input type="text" name="age" id="age" />        </td>
        </tr>
        <tr>
          <td height="26">Email Address:</td>
          <td>          <input name="email" type="text" id="emails" value="" />        </td>
        </tr>
        <tr>
          <td height="26">AIM ID:</td>
          <td>          <input type="text" name="aim" id="aim" />        </td>
        </tr>
        <tr>
          <td height="26">MSN Messenger ID:</td>
          <td>          <input type="text" name="msn" id="msn" />        </td>
        </tr    >
        </table>
    </div>
    <p align="center">
      <label></label>
      <input type="submit" value="Send" />
    </p>
    <hr />
    <h1 align="center">Advanced Settings</h1>
    <p>-Will take place after the admin views them</p>
    <table width="483" border="1">
      <tr>
        <td width="323">Domain Name:</td>
        <td width="144"><label>
          <input type="text" name="domain" id="domain" />
          </label></td>
      </tr>
      <tr>
        <td>Password:</td>
        <td>Change Password</td>
      </tr>
      <tr>
        <td>- Old Password: </td>
        <td>          <input type="password" name="pw" id="pw" />        </td>
      </tr>
      <tr>
        <td>- New Password:</td>
        <td>          <input type="password" name="pw2" id="pw2" />        </td>
      </tr>
      <tr>
        <td>- Confirm:</td>
        <td>          <input type="password" name="pw3" id="pw3" />        </td>
      </tr>
      <tr>
        <td>Subscription type:</td>
        <td>
          <select name="subtype" id="subtype">
            <option value="12-20">12-20</option>
            <option value="21-30">21-30</option>
            <option value="31-40">31-40</option>
            <option value="41 up">41 -&gt;</option>
          </select>        </td>
      </tr>
    </table>
    <p>
      <label></label>
      <label>Click to submit changes: </label>
<input type="button" name="sub2" id="sub2" value="Send" onclick="send()" />
    </p>
    <p>
      <label></label>
    </p>
    <p>
      <label></label>
      <label></label>
    </p>
    <p>
      <label></label>
    </p>
  </div>
    <!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
  <div id="footer">
    <p>Created By Andrew J. Warner, Fox Software Inc.</p>
  <!-- end #footer --></div>
<!-- end #container --></div>

</body>
</html>

User is offlineProfile CardPM
+Quote Post

maryaan
RE: Saving The Contents Of Text Boxes To A File
2 Feb, 2008 - 01:23 PM
Post #12

New D.I.C Head
*

Joined: 1 Feb, 2008
Posts: 5


My Contributions
i see 2 problems:
1) where are the <form> tags? it won't working without it
2) you have terrible mess in your code

//edit

you should make 2 forms
CODE
<form name="general" method="post">
//yourfields...

<input type="submit"....>
</form>
//some code here...

<form name="advanced" method="post">
//your fields...
<input type="submit"....>
</form>


then your script receiving data over the POST
CODE
$some_variable = $_POST['some_name']

don't use GET because variables are added to link, POST variables are "invisible"

//edit2

read this Get & Post Methods: How to & why.



This post has been edited by maryaan: 2 Feb, 2008 - 01:42 PM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:23PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads