The beginning stages of a web-based form are very simple: you read the HTML (HyperText Markup Language) guide, learn about select menus, options, form labels, radio inputs and all the myriad ways you can inveigle information from your visitors. You also learn about the action
and method
attributes. Great. So all of this HTML you’ve learned is wonderful – but won’t actually do anything. You need to provide some kind of back-end rules to actually make any use of this information.
Deciding how to use the Data
First things first, you have to decide what you want to do with the data. I’m only going to deal with the most common probable tasks, here – no image editing or complex database updates, just simple data management. Are you going to write your data to a database? Do you want to use it to create a file which will hold a record of the transaction? Do you want it to send an email?
For the sake of each example, I’m going to imagine we’re creating a survey. The form needs to submit information which will be inserted into a MySQL database which, if you choose, can be analyzed in various ways using other scripts later. This is just about getting the data into the database. We’ll start, therefore, by describing the form you’ll be using to collect information.
Let’s imagine a survey collecting four pieces of information:
- A reason for contacting you
The reason for contact will have four options, but only one will be allowable as a choice. This will use radio button inputs.
- A list of services the contacting person might be interested in.
In this case, the person will be able to select several options, so we’ll be using checkboxes.
- Survey taker’s state of residence.
This will be a drop down. Only one can be chosen, but there are far too many choices for a radio input to make any sense.
- Comments from the survey taker.
Could be anything – so this will have to be a text field.
The code for your form will look something like this:
<form method="post" action="">
<p>
<fieldset>
<legend>Why are you contacting us?</legend>
<div>
<input type="radio" name="q1_why" id="q1a">
<label for="q1a">Technical Support</label><br />
<input type="radio" name="q1_why" id="q1b">
<label for="q1b">Applying for Work</label><br />
<input type="radio" name="q1_why" id="q1c">
<label for="q1c">Wanted to Hire You</label><br />
<input type="radio" name="q1_why" id="q1d">
<label for="q1d">Other</label><br />
</div>
</fieldset>
<fieldset>
<legend>What service are you interested in? (Check all that apply.)</legend>
<div>
<input type="checkbox" name="q2_service[]" id="q2a" value="Heating" />
<label for="q2a">Heating</label><br />
<input type="checkbox" name="q2_service[]" id="q2b" value="Cooling" />
<label for="q2b">Cooling</label><br />
<input type="checkbox" name="q2_service[]" id="q2c" value="Plumbing" />
<label for="q2c">Plumbing</label><br />
<input type="checkbox" name="q2_service[]" id="q2d" value="Wiring" />
<label for="q2d">Wiring</label><br />
<input type="checkbox" name="q2_service[]" id="q2e" value="Carpentry" />
<label for="q2e">Carpentry</label>
</div>
</fieldset>
<fieldset>
<legend>Where are you from?</legend>
<div>
<label for="q3">Pick your state:</label>
<select id="q3" name="q3_state">
<option value="none">Choose One</option>
<option value="MN">Minnesota</option>
<option value="MT">Montana</option>
<option value="NY">New York</option>
</select>
</div>
</fieldset>
<fieldset>
<legend>Comments</legend>
<div>
<label for="q4">Please provide additional comments about our services.</label><br />
<textarea id="q4" name="q4_comments" rows="4" cols="40"></textarea>
</div>
</fieldset>
<fieldset>
<div>
<label for="submit">Submit the form</label>
<input type="submit" id="submit" name="submit" value="Send your Input" />
</div>
</fieldset>
</form>
This form will be the centerpiece of our example form processing script.
Creating your Form and your Script
Regardless of your chosen method of using data, you’ll need to have a script which collects the information from the form. This can be the same for each processing choice — first gather all data, then build the final element which either sends your email, inputs data to your database, or adds information to your data file.
What happens if you leave the action attribute blank in a form?
The form will submit to itself – that is, the page which holds the form will be used to process the form information. In PHP (Hypertext PreProcessing), this simply requires you to add your form processing code to that page. This is a workable way of handling form data for a simple isolated form, but leaving the action
attribute empty is not valid under the HTML 4.01 or XHTML (eXtensible HyperText Markup Language - HTML reformulated as XML (eXtensible Markup Language)) 1.0 doctypes. To have a page process itself, it’s preferable to use the PHP global variable $_SERVER['PHP_SELF']
, which will supply the current file name.
You don’t, however, want to use this global variable “in the raw.” To make it a bit safer, you’ll want to do something more like htmlspecialchars( $_SERVER['PHP_SELF'] , ENT_QUOTES )
, which will strip out some potentially harmful attacks. It’s not infallible; but it’s better than using PHP_SELF
by itself!
There are two general types of form submission methods – POST
and GET
. Both are useful, but in this case I’ll only be dealing with POST data. If you want to learn more about the differences between the GET and POST methods, you might want to read an article describing the difference at length. You’ll notice that the code above has had the method
attribute set to POST
. The other important attribute for forms, action
has been left blank for now.
In the course of this article, we’re going to build a script to process this example form – our script will be called process.php
. For simplicity, we’ll contain the entire script in a single page. The form itself and all the processing commands will be contained in one page.
The First Pass: Retrieving the Variables
Opening the script:
We’ve established the outermost boundaries of our data processing. This snippet initiates PHP, establishes a simple if
logical query which checks whether the form has been submitted. The data following the double forward slash? Just a comment. Anything on the same line as a double slash is a comment – PHP ignores it.
One of the most important things to consider when retrieving data from a publicly available web form is whether you can trust the data. And the most important thing to realize when you’re considering this is that you can never trust public input. Never. Part of having a form online is the acknowledgement that whether through malice, incompetence, or accident, there will always be the possibility that your form will be used to send data other than what you intended. You should take precautions, therefore, to check every piece of information that comes your way.
Text fields are the easiest fields to retrieve, but can be tricky to examine. For more information on PHP security, I highly recommend reading Dave Child’s “Writing Secure PHP” or ordering a book specifically on the subject, such as Pro PHP Security by Chris Snyder and Michael Southwell. I’ll touch lightly on the subject; but don’t count on this script for any serious security.
So what needs to go inside that commented space? We’ll need to collect the information which was placed in the form, check and make sure it’s the type of information we were looking for, and then we’ll assign each piece of data to a variable which we can use to format our final result.
$q1_why = $_POST['q1_why'];
$q2_service = $_POST['q2_service'];
$q3_state = $_POST['q3_state'];
$q4_comments = $_POST['q4_comments'];
Pretty straightforward – we’re taking the raw information from the form submission and assigning them to variables. Every piece of data sent from a form using the POST method will be stuck into PHP’s $_POST array variable, retrievable using the name
attribute of the form input. You’ll notice one thing here which doesn’t seem to quite match – the name attribute for question 2 was actually “q2_service[].” What do the square brackets mean? They mean that this input, when submitted, will be placed in a variable of type array
as well. This is what allows you to retrieve multiple answers from this question – everything checked off is now associated with the $q3_service array variable.
$error = FALSE;
if ($q3_state == "none") {
$error = TRUE;
}
if (isset($q4_comments)) {
$q4_comments = trim($q4_comments);
$q4_comments = strip_tags($q4_comments);
}
if ( isset($q1_why) && isset($q2_service) && isset($q3_state) && isset($q4_comments) && $error == FALSE ) {
$process = TRUE;
} else {
$process = FALSE;
}
Slightly more complicated. I’ve elected to skip examining the data for questions 1 and 2, because they are defined within the form. There’s relatively little chance that they’re being faked. Nonetheless, in a production script you may well want to inspect them. I am going to check questions 3 and 4, however. Question 3 could be a problem because the default option is “None.” In this script, we’re going to insist that our visitors be from one of these three states. In a real situation, you may want to allow for some other possibility – but not today. We’re checking to see whether our visitor has failed to pick their state. If so, we’ll set the variable $error
equal to the boolean value “TRUE,” which we’ll use later to trigger an error condition.
With question 4, we don’t really care what the content is – but we don’t want any extra spaces and we REALLY don’t want anybody submitting HTML formatted text. So we’re using the trim()
function to strip white space and the strip_tags()
function to remove HTML code.
Question 2 may have multiple parts. If the user can submit multiple choices, there’s always a possibility that these multiple parts will need to be dealt with – we can’t just send the array around directly. We’ll need to turn this array of answers into specific parts.
while ( (list($key,$val) = each($q2_service)) ) {
$q2_services .= "[" . $val . "]";
}
This code loops through the array of services selected and creates a string variable with each element contained in square brackets. This doesn’t serve any programmatic purpose except to make it easy to distinguish one element from another when viewing your final results.
Finally, we string everything together – make sure every variable has been set and that $error
is FALSE, indicating that no error has occurred. If everything’s OK, we’ll set the value of $process to TRUE and we’ll move on to actually do something with this information.
Writing Form Data to a MySQL Database
The first task you’ll need to accomplish is to create your database. This won’t necessarily be available on every hosting solution. If you’re using one of the major hosting services, such as GoDaddy, you can always refer to their extensive documentation on databases. One of your first steps should always be going to your host’s help pages – any luck, and they’ll provide documentation like this which may answer a number of your questions on setting up a database. Regardless of your hosting service, however, if you use MySQL you will have to deal with the language which defines and interacts with the database – SQL (Structured Query Language (a database standard)), or Structured Query Language.
It’s well beyond the scope of this article to try and articulate the complete nature of MySQL or Structured Query Language. To get started learning SQL, you may want to give a look to the articles available from the W3Schools’ SQL Tutorial.
However, the minimum you’ll need will be a single database table with five fields. I’m actually going to go to six, because I like to attach a timestamp to every form submission – this information can really make your data a lot more useful.
CREATE TABLE survey (
sid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
q1_why VARCHAR(127) NOT NULL,
q2_service TEXT NOT NULL,
q3_state VARCHAR(2) NOT NULL,
q4_comments TEXT NOT NULL,
time_stamp TIMESTAMP NOT NULL)
TYPE = MYISAM;
This SQL code will create your basic MySQL table with six fields. It’ll contain an automatically incremented unique ID, create space for your fields, and keep track of when the submission occurred. For more explanations of databases, try the MySQL home page or consider reading a complete guide to MySQL such as MySQL™: The Complete Reference, by Vikram Vaswani.
Creating the database is somewhat outside the range of what I’m trying to cover, however. Hopefully the references provided will help you out – but for now, we’re going to have to deal with getting information into the database. This, of course, will require a completely different, though related, SQL query.
define ('DB_USER', 'yourusername');
define ('DB_PASSWORD', 'yourpassword');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'your_database');
$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die('Failure: ' . mysql_error() );
mysql_select_db(DB_NAME) or die ('Could not select database: ' . mysql_error() );
Before you can start adding information to a database, you need to attach your script to the database. In order for it to do that, you need to supply that script with your username, your password, and the name of your database. Now, obviously, this is a security issue. You should never make this information easily accessible – if possible, place this information in a separate file with a difficult to guess filename, somewhere below your website’s root directory. You can then include
your database connection:
include ("../mydb_connect1597.php");
In this case, the connection file has been included one directory below the active script. Having connected to your database, you can move on and insert your data.
$query = "INSERT INTO survey VALUES ('','$q1_why','$q2_services','$q3_state','$q4_comments','now()')";
$q = mysql_query($query);
That’s all the PHP code required to insert the information from your form into a database. Now, you probably want to check that the data was correctly inserted, as well.
if (!$q) {
exit("<p>MySQL Insertion failure.</p>");
} else {
mysql_close();
//for testing only
//echo "<p>MySQL Insertion Successful</p>";
}
This code will simply check whether the mysql_query
was performed successfully, and notify you of the results. It also closes the database connection, since you no longer need it in this instance of the script.
That’s something like the bare minimum needed to process a form and insert the data into a MySQL database – there are scads of issues not dealt with, including many database security issues and error handling. But that’s a question for a later article.
Placing your data in a file
As long as you know the correct data format, you can create just about any type of file using PHP. The easiest, of course, are .txt
and .csv
files. Comma separated values files tend to be a bit more useful for this kind of thing, since you can then open the file in a program such as Microsoft Excel and be able to sort the information. Although it’s possible to create the file from the beginning with PHP, it’s much easier to simply create the file in advance. You can create a .csv format file in any text editor – simply open a blank document and save it as “survey.csv” – the quotes will be required if you’re using Microsoft’s Notepad.
Servers are protective of files. Naturally, they don’t want to leave every file just sitting there waiting for anybody to edit it. In order to prevent this, files have permissions settings. Each file has permissions set for three users: the owner, the group, and the WORLD – which is to say, everybody. Each user is granted specific permissions for three actions: read, write, and execute. Most of the time, you want everybody to be able to read your documents, but only want yourself to be able to write them. For scripts and files which will be recording data, however, you need to be a little more trusting. For more on file permissions, read “ Understanding file permissions on Unix: a brief tutorial.”
Permissions are represented most commonly in 9 character strings. Each set of three characters represents one user, and each character within that user represents one behavior. For example, the permissions set rwxrw-r-x
gives the owner of the file permission to read it, write it, and execute it. The group can read and write, and the world can read and execute the file.
You need to make certain that your .csv file has permissions set which will allow your script to read it and write it. The script probably runs with group permissions, but in some servers it may require world-wide permissions – if PHP is running under a different group than the owner of the file.
The code:
$csv_file = 'survey.csv';
if (is_writable($csv_file)) {
if (!$csv_handle = fopen($csv_file,'a')) {
// this line is for troubleshooting
// echo "<p>Cannot open file $csv_file</p>";
exit;
}
}
You’ve defined a file pointer which can find the comma separated values file you created. You’re checking it to see whether it’s readable and, while you’re testing, you’re providing an error message to tell you what’s going wrong.
About Error Messages
You don’t want to provide technical error messages in a production setting – when you’re live, you need to focus on telling your users what they need to know and nothing more. Giving away technical information is an invitation to hackers and means nothing to the average user.
At this point, you’ve also opened the file for editing using the function fopen
. This function accepts two arguments: the first has to be a file pointer. The second is a code which represents the manner in which you’re opening the file – you need to tell the script whether you want to overwrite the existing content of the file, whether you want to add information at the beginning of the file or the end, or whether you don’t intend to write to the file at all.
The PHP online manual has a complete list of this codes at the fopen
function page. For now, however, just know that a
means “Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.” We’re not interested in reading the information and we don’t want to overwrite anything that’s already there, so it’s to the end of the file we go.
$csv_item = "\"$q1_why\",\"$q2_services\",\"$q3_state\",\"$q4_comments\"\n";
if (is_writable($csv_file)) {
if (fwrite($csv_handle, $csv_item) === FALSE) {
//for testing
//echo "Cannot write to file";
exit;
}
}
fclose($csv_handle);
This script creates a text string containing the four variables, checks to see whether the file is writable, and writes the string into the file. It then closes the file by releasing the file handler you’ve created and exits the script. The information has been saved to your file for later reading.
Sending your Information by Email
In some ways, this is the easiest method. It will simply stick the information into an email message and send it to you. What it doesn’t do is keep a running list of the uses of the form – if you wanted to use this for a survey, for example, you’d be manually entering the information from each survey submission into your chosen method of data tracking.
There are four basic elements required to send mail with PHP. The entire process is handled by a function called, predictably enough, mail()
. This function needs four arguments:
- The destination address, or recipient.
- The message subject.
- The message content, or body.
- Mail header information.
Technically speaking, the mail headers are optional information. However, I think they’re important and valuable so we’re going to go ahead and pretend that they’re required.
//first, define your four mail function fields
$recipient = "you@yourdomain.com";
$subject = "Online Survey from Your Domain";
$content = "Online Survey:\n
Why are you contacting us? $q1_why\n
What services are you interested in? $q2_services\n
Where are you from? $q3_state\n
Your Comments:\n
$q4_comments\n";
$header = "From: YourSurvey \n"."Reply-To: survey@domain.com\n";
This is pretty straightforward – each field is a basic text string. To make it easier to read and understand, I’m writing the questions asked into the message before the variable with the answers. I’m also making use of \n
– this code creates a new line in the message, and will make the results much easier to process. (It has the same effect above in the CSV files section.)
You can name the sending address whatever you’d like, but I’d recommend using a name which will make it easy to associate the messages sent with this particular script.
Having assigned these variables, all that’s left is to send the message:
mail($recipient, $subject, $content, $header);
exit;
And you’re done. You’ve processed your data.
Conclusion
In conclusion, this is a very sparse way of addressing the complicated issue of letting people use your site interactively. It hardly touches on any of the major issues of usability or security, such as protecting your scripts from use by spammers; preventing hackers from accessing your databases; returning user-friendly error messages; or any higher level data processing. But it’s intended to be basic – this is just an introduction. Once you’ve gotten your feet wet, you should really look into a solid textbook which will cover some of the more difficult issues.
Recommended Reading
- Learning PHP and MySQL, by Michele E. Davis and Jon A. Phillips
- PHP and MySQL Web Development, by Luke Welling and Laura Thomson
- Beginning PHP and MySQL 5: From Novice to Professional, by W. Jason Gilmore
- Pro PHP Security, by Chris Snyder and Michael Southwell
I know that I’ve mentioned that Pro PHP Security book already — but I can’t emphasize the importance of security enough. By putting a form on your site and inviting any kind of input from your visitors, you’re creating a doorway into your website. Most of the time you’ll be fine, even if you haven’t taken the precautions you should — but it’s not worth the risk if you have the opportunity to protect yourself.
Ogbonda
I just finished designing my site http://www.powerhomebizz.com and I want to create a contact us page with a thank you page but i am having problems with the post action, how do direct the action on my web post to a folder on my server that is on my form <form action="contact.htm" method="post", how do i create a folder on my server so that any form submited will be seein in the folder. instead of showing me errors thanks
Lisa
Hi!
Great tutorial, really enjoyed it. Do you have a follow up on how to read and use the input? Or any tips where I can fins good tutorials?
I have been able to create a form (containing 3 input-fields) and save all input in a csv-file.
I have also been able to open the file with “fopen” and “fgetcsv()”and get the content as an array like this: Array ( [0] => testing [1] => month [2] => this is a string of text ).
How ever now I’m completely stuck. My idea is to use the input stored in [0] as a title on a “blogpost” and [1] as a choice of month and [2] as the actual text (a very short blogpost).
Any advice would be extremely appreciated!
Kind Regards
Lisa
Alan Siva
Hi, I was able to read your website would you be so kind to write about the pros and cons of having a window blind in the kitchen?
KJ
Thanks for the reply. It turns out the host uses a “global” php.ini file. All I needed to do was move a copy it into my domain folder and make modifications there to override some settings.
Joe Dolson
@KJ The simple answer is to be sure to include an email “From:” header in the headers area of the
mail()
function. That’s described above in the example — in the place where it says “From: YourSurvey <survey@domain.com>” you’d substitute your desired From: address.There is, however, a more complicated answer — on some server configurations, PHP (Hypertext PreProcessing) doesn’t automatically deliver from the From: header just because you’ve told it to. Here’s a brief FAQ (Frequently Asked Questions) discussing some of your options: Problem with PHP mail From header.
KJ
Well written guide. This has helped clear up some questions I have. One thing I have been unable to clear up, (and I’m probably not searching the correct phrase) – when an autoreply is sent to the person who filled out the form, or the information from the form is mailed to a recipient, how do I make sure that “From” is an email address for the domain, and not a generic one created by the host?
TIA
Ravi Kumar Jangra
Nice Form Submitions Example Realy
Sir, how can we call the submitted values that rare stored in (survey.csv) file on a html, php file to display what the submitins the visitor has provide.
plz let me know as soon as you go through this Comment.
or u can just mail me : rsuitor@yahoo.com, when the code is coocked.
Joe Dolson
I’d recommend using PHP (Hypertext PreProcessing) to report your errors rather than attempting to write JavaScript alerts as a result. What you’ve done here won’t do anything, because there’s no trigger on the events. If you simply what you’re echoing with plain text:
echo "I am sorry, we need your First name, Lastname, and E-mail to process this form";
it would be more effective.Marc
The basic pf your tutorial:
<?php
if (isset($_POST['submit'])) {
// This variable is to choose which type of processing you're using.
$mode = "email"; // or "csv" or "email"
// These set your initial variables.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
// it's good planning to set your variables explicitly before setting them programatically.
// EMPTY FIELD CHECK
if( (empty($firstname)) || (empty($lastname)) || (empty($email) ))
{
echo 'alert(“I am sorry we need: First Name, Last Name and E-mail.”);’;
echo ‘history.back(1);’;
exit;
} /* EMPTY FIELD CHECK */
$error = FALSE;
if ($email == “none”) {
$error = TRUE;
}
if (isset($firstname) && isset($lastname) && isset($email) && $error == FALSE) {
$process = TRUE;
} else {
$process = FALSE;
}
once everything is filled in the form re-directs correctly, I just do not get the warning message to come up.
Joe Dolson
Well, what are you doing? What’s the actual code you’re using to create a required field?
Marc
Hi, works well, but I keep getting a syntax error when I try to create required field. What am I missing. Thanks
Nathan
Hiya, found this article as it’s quite helpful. In the past I have created forms using html and then using php to process them. As I’m a beginner I am still learning ways of validating user input but so far quite fun! Thx
Jason
Looking at your PHP (Hypertext PreProcessing) skills Joe, cannot hold back to ask you one question:
Is it possible to make a PHP call to index all the pages of any website and gather the page titles of those pages (or any other properties)?
Miro
I do not have other words, just one: FANTASTIC !
Joe Dolson
Either way will work; it’s a matter of choice. Ultimately, the PHP (Hypertext PreProcessing) script needs to be the target of your form’s “action” attribute – from that point, the script can handle the form input whether the form file is present or not.
For usability, you’ll want to find some way of posting the form data back to the form in case of errors, but there’s usually a way — hope you’re able to find yours!
Anna
Okay… I’m a complete beginner, and this is the first tutorial I’ve found that remotely addresses what I need to know. So I apologize in advance for my elementary understanding of any of this.
I’m confused – do I make the form in an HTML (HyperText Markup Language) file and point to a separate PHP (Hypertext PreProcessing) file to post it? Or should this work with all the code (form + PHP post script) in a single PHP file?
Joe Dolson
Well, from what you’ve said, I think that all you really need to do is change the way the CSS (Cascading Style Sheets) information is written. It looks to me like the regular expression which Smarty templating uses to identify Smarty tags is seeing
{float:none;}
as a Smarty template tag. If you were to change it to:Then, I bet that Smarty wouldn’t throw that error. Give it a try, at any rate.
Cristian
Hello. Can I make PHP (Hypertext PreProcessing) skip a number of lines in the html page? There is an IE (Internet Explorer) fix for a drop-down menu that’s causing an error.
The script:
#menu{float:none;} /* This is required for IE to avoid positioning bug when placing content first in source. */
/* IE Menu CSS (Cascading Style Sheets) */
/* csshover.htc file version: V1.21.041022 – Available for download from: http://www.xs4all.nl/~peterned/csshover.html */
body{behavior:url(/css/csshover.htc);
font-size:100%; /* to enable text resizing in IE */
}
#menu ul li{float:left; width: 100%;}
#menu h2, #menu a{height:1%;font:bold 0.7em/1.4em arial,helvetica,sans-serif;}
The error:
Fatal error: Smarty: [in ./templates/tmpl1/index.tpl.html line 8]: syntax error: unrecognized tag: float:none; (smarty_compiler.class.php, line 417) in /home/rzeg3143/public_html/smarty/smarty_compiler.class.php on line 2062
Joe Dolson
Thanks, Matt! Glad to be able to help.
Matt Dabbert
Thanks Joe!! I found this form processing article much easier to follow than similar articles on the web. Keep up the good work.
Joe Dolson
Well, it does sounds like you’re missing the “From:” address information in your script. Since it seems like that information is ALSO missing from your php configuration files, the mail() function won’t know how to send the message.
You’ll need to make certain that your headers statement used in the mail() function includes a From: header or modify your PHP (Hypertext PreProcessing).ini file to offer a default.
I suggest reading the PHP Mail function reference at PHP.net for help.
roshan
i am having problem with sending mails. I am getting error like
mail() [function.mail]: “sendmail_from” not set in php.ini or custom “From:” header missing in c:\wamp\www\mail\mail.php on line
also when i kept the smtp=192.168.1.xx it works for lan only but fails in global domain.
Please help me to solve this problem. Do I need any extra codings for sending mail?
Joe Dolson
Well, XML (eXtensible Markup Language) is kind of a different beast. What you can do with it depends on what version of PHP (Hypertext PreProcessing) you’re using – PHP 5 has built in XML parsing functions, PHP 4.3 and below have XML handling functions as an extension — available, but not necessarily installed in your version of XML.
The easiest way is generally to parse the XML information into an array, replace the elements you want to update, and then write the whole thing back to the file, replacing the previous information.
You may want to consider finding a script to parse and edit XML documents. I haven’t used it, but XML22 might do the trick for you.
Greg
Thanks for the understandable and helpful article. I just have one question… As I understood, files can only be edited by PHP (Hypertext PreProcessing) either at the top of the file or at the bottom… So how would I go about editing an XML (eXtensible Markup Language) file (feed), which needs to have opening/closing tags at the top and bottom? Can I make the PHP skip a few lines before throwing in the information?
Thanks.
Joe Dolson
Cool! Glad to be timely 🙂
Cath
Thanks Joe, this is a very timely tutorial for me.
Joe Dolson
Thanks, Dave! That’s really a fine compliment coming from you. When I first saw you’d left a comment my first thought was that I’d recommended something stupid and you were correcting me…the actual comment was much more pleasant!
Dave Child
Great guide, Joe. I know this topic comes up a lot in forums, so it’s about time someone came up with a good quality, easy to understand tutorial explaining how to manage basic form stuff!
Joe Dolson
Thanks, Mike…most articles I write here take less than a couple hours. This one took a couple days…
Maybe I just got carried away!
I have to admit, I got curious about the length of it so I copied it into a word processor to find out: it’s 10 pages printed.
Mike Cherim
Wow, Joe, great article. That must’ve taken some effort.
Joe Dolson
Thanks, Pat.
I’m glad that what I wrote up was able to help you! I was glad to do it.
Pat S
Thank you Joe,
this reminds me of one of my teachers( quite a while back) he noticed i was not getting his lectures in class, called me in and asked why a smart guy like me was getting a d grade… I explained about my visual learning problem, coupled with dyslexia. next day in class he handed out a couple of pages he had prepared for his talk that day. It worked, I was able to follow much better. This problem with learning from books, and lecture type lessons affects a lot more people than is realized, I can not begin to count all the books I have purchased that make no sense to me. The Authors seem to think since they know what they are saying you automatically understand them. You are an excellent teacher Joe, and Thanks again for actually sharing knowledge, this in itself is a rarity now days. every one thinks they should be paid for what they know instead of what they do. If this were the case I would be rich and I could pay a code writer…. ( humor). Ok I took up more than my share of space. thank you again, very good example.