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!
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
hi 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'].
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.
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.
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!
This post has been edited by Martyr2: 1 Feb, 2008 - 01:58 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 !
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.
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...
// 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".
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...
// 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".
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>
<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 when it comes to php!
<!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">
// 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!";
//]]> </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]-->