Page 1 of 2
PHP forms.
Posted: Sun Jul 25, 2004 12:57 am
by Simon Angell
Hi all.
I am wanting a script that users can enter various detials and upon clicking "add" it writes various details to various different files.
So, the users selects what they want using checkbox's, more then one can be selected.
Select this if you want this product 1 [*]
Select this if you want this product 2 [*]
Select this if you want this product 3 [*]
etc etc
Then they enter their details.
Your Name [enter name here]
Your Number [enter number here]
Credit [credit amount]
etc etc
Now the script needs to do the following...
1. Takes the user name (i.e John Smith) and convert it to john_smith.txt - for use further along.
2. See the Products selected and write the converted user name to a txt file like, product_1.txt product_2.txt..- which will contain info like this: john_smith.txt
joe_average.txt
etc etc
3. Write a file called john_smith.txt which contains the Phone number.
4. Write another file called john_smith.txt (diff directory) which contains the credit amount.
5. Now the product_1.txt and BOTH:john_smith.txt need to be set at CHMOD 666...
6. Also, product_1.txt May or may not already exist, if it doesn't then it needs to write it, if it does, it needs to ammend it, and if the user is already written in that file then it is not to write it at all...
any help will be muchly appreciated...
Posted: Sun Jul 25, 2004 1:04 am
by lolpix
Re: PHP forms.
Posted: Sun Jul 25, 2004 3:51 am
by ol4pr0
Simon Angell wrote:I am wanting a script that users can enter various detials and upon clicking "add" it writes various details to various different files.
Dont we all WANT something
Posted: Sun Jul 25, 2004 4:57 am
by McGruff
We're not going to write it for you but if you have a specific problem getting a piece of code to work please post your query.
Posted: Sun Jul 25, 2004 11:51 pm
by Simon Angell
yeah, fair enuf, just needed some pointers to get me started

Posted: Mon Jul 26, 2004 2:20 am
by Simon Angell
Ok, i have some sort of script going right now, but have come to a sticky point.
Now the form page has radio buttons which the user can select (one or more) (see first post)
<input type="radio" name="warn1" value="Severe_Thunderstorm_Warning_(1).txt">
Severe Thunderstorm Warning (1)<br>
<input type="radio" name="warn2" value="Severe Thunderstorm Warning (2)">
Severe Thunderstorm Warning (2)<br>
<input type="radio" name="warn3" value="Severe Thunderstorm Warning (3)">
Severe Thunderstorm Warning (3)<br>
etc etc
and then the php script needs to see which ones are selected and write into the corrosponding file.
right now it will will write to file 1 if radiobutton 1 is selected, but will not write to file 2 if radiobutton 2 is selected, (even with radio1 un/selected)
Code: Select all
<?php
$warning1 = $_POST["warn1"];
$warning2 = $_POST["warn2"];
$warning3 = $_POST["warn3"];
//writing local files
$dir = "Users";
$rContents = "$filename";
if (!$handle = fopen("$dir/$warning1", 'awb+')) {
echo "Cannot open file ($warning1)";
exit;
}
// Write content to file.
if (fwrite($handle,"\r\n $rContents") === FALSE) {
echo "Cannot write to file ($warning1)";
exit;
}
echo "Success, wrote ($rContents) to file ($warning1)";
fclose($handle);
?>
thats the code that writes to file1, so i just copied and pasted it for file 2...but with no luck.
so how do i get the php to see if a radio button is selected, and then write to the file - or create the file if it doesn't exsist?
[/quote]
Posted: Mon Jul 26, 2004 3:17 am
by Simon Angell
oh to add... im using checkbox's instead of radio buttons due to obvious reasons....
Posted: Mon Jul 26, 2004 4:35 am
by Simon Angell
grrr, this is getting frustrating!!!, i got it to work the way i wanted, it had something to do with the names of the checkbox's and such, so i changed them to completely different names, instead of warn1 warn2 etc etc, modified the php script for it and prest it worked, figured the names made no sense so went back and changed them to something that did, and changed the php to match, the names were different enough, 1_STW, 2-STW, STW_3 etc etc. but it didn't work, so decideded ah well, they can be completely different, and changed them to a buch of gibberish, changed the php to match, and nothig, stupid thing didn't work! grrrr
Posted: Mon Jul 26, 2004 4:42 am
by Simon Angell
ok, solved that problem.... was laziness in copy & pasting and not changing things, so had 3 instances of $warning2 = $_POST[]; etc ect.. now on with the show...
Posted: Tue Jul 27, 2004 3:38 am
by Simon Angell
hey everyone.
Just wandering if there is an easier way to do this?
Code: Select all
<?php
//grab posted details....
$warning1 = $_POST['a'];
$warning2 = $_POST['b'];
$warning3 = $_POST['c'];
//small example of the ~50odd $_POST['values']
//writing local files
$dir = "Users";
$rContents = "$filename";
if (!$handle = fopen("$dir/$warning1", 'awb+')) {
echo "<br>Cannot open file ($warning1)";
exit;
}
// Write content to file.
if (fwrite($handle,"\r\n $rContents") === FALSE) {
echo "<br>Cannot write to file ($warning1)";
exit;
}
echo "<br>Success, wrote ($rContents) to file ($warning1)";
fclose($handle);
//repeated for every $_POST above...changing the $handle to $handle1 etc etc....
?>
any help on compacting the above functions would be great!
Posted: Tue Jul 27, 2004 3:45 am
by feyd
you could make the $warning's an array.. then iterate through it with a for/foreach/while loop..
Posted: Tue Jul 27, 2004 4:15 am
by Simon Angell
i did try an array
Code: Select all
<?php
//quick example
$warnings=array($_POST[a] ,$_POST[b]);
foreach ($warnings as $warning)
//then the file write code
$dir = "Users";
$rContents = "$filename";
if (!$handle = fopen("$dir/$warning", 'awb+')) {
echo "<br>Cannot open file ($warning)";
exit;
}
// Write content to file.
if (fwrite($handle,"\r\n $rContents") === FALSE) {
echo "<br>Cannot write to file ($warning)";
exit;
}
echo "<br>Success, wrote ($rContents) to file ($warning)";
fclose($handle);
?>
and got cannot write to file() errors....
Posted: Tue Jul 27, 2004 10:38 am
by feyd
the code you have will loop on the $dir line only.
Posted: Tue Jul 27, 2004 7:45 pm
by Simon Angell
so i tried this:
Code: Select all
<?php
//then the file write code
while ($dir = "Users") {
$rContents = "$filename";
if (!$handle = fopen("$dir/$warning", 'awb+')) {
echo "<br>Cannot open file ($warning)";
exit;
}
// Write content to file.
if (fwrite($handle,"\r\n $rContents") === FALSE) {
echo "<br>Cannot write to file ($warning)";
exit;
}
echo "<br>Success, wrote ($rContents) to file ($warning)";
fclose($handle);
}
?>
and go this: Warning: fopen(Users/): failed to open stream: Permission denied in c:\www.canberra-wx.com\wwxs\adduser.php on line 31
Cannot open file ()
Posted: Tue Jul 27, 2004 8:42 pm
by McGruff
This
http://wiki.devnetwork.net/index.php/Ti ... ermissions might help.
Content is in a bit of a mess since it appears to be being worked on and re-arranged.