Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checkbox echos but can't store to upload 1

Status
Not open for further replies.

driter

Technical User
Oct 10, 2012
6
CA
Hello, New to php
I have a set of checkboxes that I can get to echo, but cannot get to post to upload to an text file.
Any help would be much appreciated.

HTML code
<form name="contactform" method="post" action="email3.php?savedata=1">
<table width="530">
<td width="174"></tr>
<tr>
<td valign="top">
First Name *
</td>
<td width="180" valign="top"><input type="text" name="first_name" maxlength="50" size="30" /></td>
<td width="101" valign="top">Last Name *</td>
<td width="180" valign="top"><input type="text" name="last_name" maxlength="50" size="30" /></td>
</tr>
<tr>
<td><input name="FrameNo" type="text" value="" size="16" maxlength="16" id="FrameNo" />
*</td>
<td height="30"><input type="checkbox" name="firstorder[]" value="8.10" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="A8.10" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="5.7" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="A5.7" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="11.14" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="4.6" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="16.20" /></td>
<td height="30"><input type="checkbox" name="firstorder[]" value="L.Res" /></td>
<td><input type="checkbox" name="firstorder[]" value="H.Res" /></td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="4" style="text-align:center">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</table>
</form>


PHP code
<?php
$savedata = $_REQUEST['savedata'];

$fstorder = $_POST['firstorder'];
if(empty($fstorder))
{
echo("Please select a size.");
}
else
{
$N = count($fstorder);
echo("You selected $N size(s): ");
for($i=0; $i < $N; $i++)
{
echo($fstorder[$i] . ", ");
}
}

if ($savedata == 1){
$data .="First Name: ".$_POST['first_name']. PHP_EOL;
$data .="Last Name: ".$_POST['last_name']. PHP_EOL;
$data .="Frame 1: ".$_POST['FrameNo']. PHP_EOL;
//$data .="First Order: ".$_POST['firstorder']. PHP_EOL;
$data .="First Order: ";
$N = count($fstorder);
for($i=0; $i < $N; $i++)
{
$data .=.$fstorder[$i].;
}
$data .=PHP_EOL;

$filename = $_POST['last_name'];

$filename2 = $filename.".";

$target = "upload/";
$target = $target . $filename2.txt;

$fp = fopen($target, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");

fclose($fp);
echo "Your Form has been Submitted!";

}
?>



Thanks
 
Hi

To clarify first : you actually mean save [tt]form[/tt] data to a text file, not to upload a text file, right ?

If so,
[ul]
[li]Are you getting any of your [tt]die()[/tt]'s messages ?[/li]
[li]Are there any messages in the web server's error log ?[/li]
[li]Has the web server's user permission to write in the upload/ directory ?[/li]
[/ul]

Note that the way you are generating the file name is quite bad
[ul]
[li]If there will be more data with the same last name, they will overwrite each other.[/li]
[li]If the last name will contain a maliciously crafted value including relative path, your script will overwrite important files.[/li]
[/ul]

Next time please post your code between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] TGML tags.

Feherke.
[link feherke.github.com/][/url]
 
Clarifying - Yes save form data to text file

Recieving no die messages (only message is
"You selected 3 size(s): 8.10, A5.7, 16.20," on screen print
but not the "Your Form has been Submitted!" Which tells me failure
The web server's error log - Will have to check server tonight but I do not think so.
And without checkboxes yes I can get code to write but checkboxes will not.

As to the saving function. This code is meant to write and write additional information if same last name is used. It does not over write the information already there.

And thanks for the [ code ] and [ /code ] information.
 
Code:
$target = $target . $filename2[red] . '.txt'[/red];
 
Hi

Justin, that is indeed ugly, but is syntactically and functionally correct :
Code:
[blue]php >[/blue] $filename = 'last_name'; 
[blue]php >[/blue] $filename2 = $filename.".";
[blue]php >[/blue] $target = "upload/"; 
[blue]php >[/blue] $target = $target . $filename2.txt;
[blue]php >[/blue] echo $target;
upload/last_name.txt

driter, not sure yet what is the problem, but one thing is sure : $_POST['firstorder'] is an array, so better handle it accordingly :
Code:
[navy]$data[/navy] [teal].=[/teal][green][i]"First Order: "[/i][/green][teal].[/teal][highlight][COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]', '[/i][/green][teal],[/teal][/highlight][navy]$_POST[/navy][teal][[/teal][green][i]'firstorder'[/i][/green][teal]][highlight])[/highlight].[/teal] PHP_EOL[teal];[/teal]

Feherke.
[link feherke.github.com/][/url]
 
@feher
I live and learn.

but if you'll allow me I don't think it is syntactically correct because the 'txt' is an unquoted string. I suspect what is happening is the OP has error reporting suppressed so that the E_NOTICE is not being generated and php is interpreting the unquoted string as a string in the absence of any identically named constant.

try this
Code:
error_reporting (E_ALL);
$filename2 = 'adie.';
echo $filename.txt;
I get this output
Code:
Notice: Use of undefined constant txt - assumed 'txt' in php shell code on line 1
adie.txt

i would have expected an E_ to be raised about an unquoted string.

the OP I think handles the array in this code block

Code:
$N = count($fstorder);
for($i=0; $i < $N; $i++)
{
$data .=.$fstorder[$i].;
}
$data .=PHP_EOL;

personally I would delete the concatenators as they should generate a parse error and kill the script. perhaps this is the reason the script does not work?

Code:
$N = count($fstorder);
for($i=0; $i < $N; $i++) {
  $data .= $fstorder[$i];
}
$data .=PHP_EOL;

but your implode method is much tidier of course and avoids the concatenators.
 
Will test this evening.

Why must we work, when life is so much more stressful...
 
Hi

[tt][red][small][ignore][flame][/ignore][/small][/red][/tt]
Well, the "syntactically correct" may be my personal interpretation. I enjoy using scripting languages because their syntax is generally less strict. So receiving a notice because I force the syntax's limits is just fair to me.
[tt][red][small][/flame][/small][/red][/tt]

By the way, well spotted the extra operator. [medal] I find really difficult to analyze unindented code. driter, please choose an Indent style and use it consistently.

Feherke.
[link feherke.github.com/][/url]
 
Thanks all. Everything worked out in the end.

feherke. code worked with the implode also surrounded by an (!empty($_POST.

Code:
$data .="Frame 1: ".$_POST['FrameNo']. PHP_EOL;
         $data .="First Order: ".implode(', ',$_POST['firstorder']). PHP_EOL; 
$data .="Frame 2: ".$_POST['FrameNo2']. PHP_EOL; 
	if(!empty($_POST['secorder'])) { 
         $data .="Second Order: ".implode(', ',$_POST['secorder']). PHP_EOL; 
         }

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top