Forum Moderators: coopster
On to my questions..:
Firstly, I need to write a simple script which simply takes every field (empty or not) in the form when submitted, and adds it to a new line on a .csv file.
I want there to be one particular field which contains a 4-digit number, and for the form to be added to the .csv (which I'll call db1.csv) the number has to be one that is in another .csv (db2.csv).
So basically what I need to write is a script that does this:
Form filled out & submitted
--> 4-digit number in 'ID' field matches a number in db2.csv
------> Data in fields added to db1.csv and page redirected to 'success' page
OR
--> 4-digit number in 'ID' field does not match any in db2.csv
------> Data NOT added to db1.csv, page redirected to 'error' page
I haven't got much yet, but here is bits and pieces..:
My form (just a couple of unformmated fields so you get the idea)
<form method="post" action="formprocess.php" enctype="text/plain">
<input type="text" name="Name" id="Name" size="40">
<input type="text" name="Email" id="Email" size="40">
<input type="text" name="Customer_ID" id="Customer_ID" size="4" maxlength="4">
</form>
formprocess.php (bulk taken from this site, can't remember users)
<?php
$csv = "db/db1.csv";
$writetocsv = $_POST['Name'] . "," .
$_POST['Email'] . "," . $_POST['Customer_ID'];
$fp = fopen("db/db1.csv","a");
fclose($fp);
header("location: form2.html");
?>
I didn't get as far as working out a validation process for the customer ID, partly because I didn't know how and partly because the script above doesn't work. After pressing submit, it goes to the success page (form2.html) but does not write data to db1.csv..
So, any help? :S
Look at the functions fgetcsv [php.net] and fputcsv [php.net]; these will do most of the work for you.
If you're using php4, you can set this in at the top of your script:
// This function will be called if not using php5, which defines fputcsv(). Unlike the php5 version,
// $fields isn't optional - the documentation doesn't state what happens if it's not included, so
// I implemented this with the argument being mandatory.
if(!function_exists(fputcsv)) {
function fputcsv($handle,$fields,$delimiter=',',$enclosure='"') {
$delimiter = substr($delimiter,0,1);
$enclosure = substr($enclosure,0,1);
$line = '';
foreach($fields as $field) {
if(strpos($field,$delimiter))
$field = $enclosure . $field . $enclosure;
$line .= $field . $delimiter;
}// EndForEach array element
$line = substr($line,0,-1);// Lose the last delimiter
$line .= "\n";// Terminate line with newline
return(fputs($handle,$line));
}// End fputcsv()
}// EndIf php4
In the script you posted, you open db1.csv for append and then you close it, but you don't do any writing in between.
To find out if the customer id is valid, open db2.csv for reading, then use fgetcsv inside a loop to get each line of the file. Look in the appropriate spot in the returned array to compare the id you've got with the one in the line. If it matches, set yourself some sort of flag and break out of the while (for example, before the while set $found = false; then if you find it set $found = true;). Close db2.csv.
If $found is true, then set about placing your posted values into an array, open db1, fputcsv, and close db1. You want to use your own array instead of the $_POST array directly because you don't want to include your Submit button and you want to validate the data where you can.
Use header [us2.php.net] (third example down, Location) to direct your users to the appropriate page. It's a good idea to put an exit; line after a redirect header.
I don't think I explained it well enough- the reason I can't do it is because I don't fully understand how to script what I want.. (I'm new to PHP, I really only want it for this simple database for now).
So basically I think I have the layout but not the bones of the script...
<?
$fp = fopen('db/db2.csv', 'r');
? check if it matches a line in db2.csv somehow
? if false
header("Location: form3.html");
exit;
? if it matches close db2.csv
fclose($fp);
? then open db1.csv
$fp = fopen('db/db1.csv', 'w');
? and write to it somehow
? then close db1.csv and redirect to success page
fclose($fp);
header("Location: form2.html");
exit;
?>
Any further help?
PS. My host has PHP 4.3.8.. :S
We have a great thread from our library that demonstrates and describes how to extract data from a csv file: [webmasterworld.com...]
After reading, use this newly acquired knowledge to try to put something together a little further and we can help work it out from there.
good luck
I've cobbled something together, I just need help with how to check db2 for a code. (I think.. there might be problems in the script I wrote).
<?
$sfp = fopen('db/db2.csv', 'r');
?-somehow check to see if it matches a line in db2.csv-?
if (?-$Customer_ID == a row in db2.csv-? ) {
fclose($sfp);
$fp = fopen('db/db1.csv', 'w');
$writetocsv = $_POST['Name'] . "," . $_POST['Telephone_Number'] . "," . $_POST['Customer_ID'];
fwrite($fp,$writetocsv);
fclose($fp);
header("Location: form2.html");
exit;
} else {
fclose($sfp);
header("Location: form3.html");
exit;
}
?>
Once again, can anyone help me?
It's not vital, as I can go through it manually, but it'd save time if I could somehow replace any commas with a space.
I found this little script, but I don't know what I would do to make it look through every form field.. I put the part in question in capitals.
$_ = 'DATA FROM FORMS?';
$result ='';
while ($_) {
s/^((?:\"[^\"]*\")¦(?:[^,]*))(,?)//;
$result.= $1 . ($2? ' ' :'');
}
The first line filters the posted customer id by only allowing letters and numbers, in the event someone is up to no good.
You need to supply the correct field position for customer id in db2 in the second line.
The 500 in the call to fgetcsv is an arbitrary number and should be set to the maximum length of a line of data in db2.
If you look at the link I provided for fgetcsv, it describes how data containing the delimiting character (comma by default) is surrounded by an enclosure character (quote by default) to prevent the problem you mentioned. If you use the fputcsv function I posted, it will act likewise; surrounding data containing commas with quotes.
$custid = preg_match('/^[a-zA-Z0-9]+$/iD', $_POST['Customer_ID'])? $_POST['Customer_ID'] : '';
$id_postn = 2;// Whatever field position the customer id is in in db2.csv
if($custid!= '') {
$found = false; // Preset flag to 'we haven't found the id'
$hdl = fopen('db/db2.csv','r'); // open file for reading
while($dat = fgetcsv($hdl,500)) { // Get a line from the file
if($dat[$id_postn] == $custid) { // If we found the id,
$found = true; // set the flag
break; // and exit the while loop
}// EndIf found the customer id
}// EndWhile look for customer id
fclose($hdl); // Close the file
if($found) {
// write the data to db1.csv and redirect to the success page
} // EndIf found customer id
// redirect to the failure page
} // EndIf posted customer id doesn't contain illegal characters