Click here to Skip to main content
14,732,921 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am currently having issues with save the table data in data.csv file. Using this javascript I am getting the value from table and show it in alert box. But I want to store this variable data in adata.csv file. I have passed the hidden field value into this page. Please edit my code. Because this is my first PHP code.

Here is my code:

<html>
<head>
<script>
function GetCellValues()
{
var str = '';
var rows = document.getElementsByTagName('tr');
var table=document.getElementById("project");
for (var i=0;i<table.rows[0].cells.length;i++)>
{
if (i > 2 )
{
str = str + table.rows[0].cells[3].innerHTML.replace(", ");
}
else
{
str = str + (table.rows[0].cells[i].innerHTML) + ', ' ;
}
}
for (var c = 1 ; c < rows.length ; c++)
{
str += '\n' + "0" + c + ', ';
var row = rows[c];
var inputs = row.getElementsByTagName('input');
for (var k = 0 ; k < inputs.length ; k++)
if (k > 1)
{
str += inputs[k].value.replace(", ");
}
else
{
str += inputs[k].value + ', ';
}
}
document.getElementById('hiden').value = str;
alert(document.getElementById('hiden').value);
}
Here I have the problem, I am not able to save the table data into the file.

function saved()
{
$handle = fopen("data.csv", "w");
$hiden = $_REQUEST["hiden"];
fwrite($handle,$hiden);
}
?>
</script>
</head>
I have made the buttons in the following way:

<body>
<form>


* * * PROJECTS * * *




<input type = "hidden" id = "hiden" name = "hiden" value = "">
<input type = "button" name = "submit" önclick = "GetCellValues()" Value = "SAVE">
<input type = "button" name = "submit" önclick = "document.write('');" Value = "submit">



</form>
Here I stored the data from .csv file and display it in table:

$handle = fopen("data.csv", "w");
$hide = $_REQUEST['hide'];
fwrite($handle,$hide);

$file = file('data.csv');
$lines = count($file);

echo'
style = "width: 60%; margin-removed auto; margin-removed auto; border-color: brown; background-color:gray;">';
echo'';
for ($i=1; $i<$lines; $i++)
{
$part = explode(',', $file[$i]);
echo'
';
}
echo'
Sl.No. Project Name ChangeDate Changed By
'.$part[0].' <input type="text" value='.$part[1].'> <input type="text" value='.$part[2].'> <input type="text" value='.$part[3].'>
';
?>
</body>
</html>


Posted

1 solution

Hello Member,
Its good that when you open the file "$handle = fopen("data.csv", "w");" then when you are done, you can close the file "fclose($handle);"...
So now to your question, just create a string with all the data in CSV format, like this:

$csv = "1,Hello World, 02-01-2014, Killzone\n2,Hello World 2, 02-01-2014, Killzone\n";
file_put_contents("adata.csv", $csv, FILE_APPEND);


In your case, you can build the csv string with the javascript and send it with a POST to a PHP file, and then in the PHP file make just this:

file_put_contents("adata.csv", $_POST['csv'], FILE_APPEND);


Any question, please feel free!
Regards,
KZ
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900