Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Markup (HTML, XHTML, XML) _ save form data locally

Posted by: kjpr Sep 3 2013, 12:35 PM

Hello all,

I am trying to save the input form data locally in a text file. I dont have access to the webserver. so, i am planning to save the data locally at first and then change the path , once i get the access.
the issue i have here is that i am not able to open a text file and write the inputs into it..i am pasting my code.Please take a look at it and let me know where i am going wrong..

CODE

<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>

</style>
<script type="text/javascript">
  $(function(){
  
     $('form').submit(function(event){
   event.preventDefault();
   window.location = $('input[type=radio]:checked').val();
   });
   });
</script>
<script>
//  put the data into a file in the current directory called "data.txt"

if (isset($_POST['submit'])) {

// Put the contents of the forms into the file
   file_put_contents('./data.txt', $_POST['url'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['Dataset'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['checkbox1'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['checkbox2'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['checkbox3'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['checkbox4'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['checkbox5'], FILE_APPEND);
  

}
</script>


</head>


<body>
<div id="stylized" class="myform">
<form name="form" method="post" action="">

<label> New Data set: </label>

    <input type="radio" name="url" value="Inputs1.html"/> Yes
    <input type="radio" name="url" value="ResultsPage.html"/> No
    
<br><br>
<label>Dataset description:
</label>
<input type ="text" name= "Dataset" size="30"><br><br><br>
<table id="Previous Datasets">
  <tr>
    <th>Check</th>
    <th>Token Number</th>
    <th>Dataset description</th>
  </tr>
  <tr>
    <td><input type="checkbox" name="checkbox1"></td>
    <td>234567</td>
    <td>MHEX North America Dataset</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="checkbox2"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="checkbox3"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="checkbox4"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="checkbox5"></td>
    <td></td>
    <td></td>
</tr>
</table>
<div style="text-align: center"><br>
  <input type="Submit" name="submit" value="Submit" class="submit">
<div class="spacer"></div>
</form>
</div>
</body>


Thanks in advance

Posted by: pandy Sep 3 2013, 01:14 PM

The only thing you can create on a user machine from the Web is cookies.

Posted by: kjpr Sep 3 2013, 01:17 PM

I dont know how to do that.. Could you please modify my code ..

Thanks




QUOTE(pandy @ Sep 3 2013, 01:14 PM) *

The only thing you can create on a user machine from the Web is cookies.


Posted by: kjpr Sep 3 2013, 02:38 PM

Hello all,
I have updated my script to read the checkbox options in the table.
But, i still have no clue about writing the data to txt file locally..please find the code below and let me know the modifications to make it work.. Thanks in advance


CODE

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>

<script type="text/javascript">
if (isset($_POST['submit'])) {

// Put the contents of the forms into the file
//file_put_contents('./data.txt', $_POST['url'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['Dataset'], FILE_APPEND);
   file_put_contents('./data.txt', $_POST['condition'][index], FILE_APPEND);
   $("#form > table > tr").each(function(){
     var checkbox = $(this).find("input [type=checkbox]");
     var Token Number = $(this).find("td:eq(1)").text();
     $(this).find("checkbox").val(Token Number);
});
  $(function(){
  
     $('form').submit(function(event){
   event.preventDefault();
   window.location = $('input[type=radio]:checked').val();
   });
   });
</script>


</head>


<body>
<form name="form" method="post" action="">

<label> New Data set: </label>

    <input type="radio" name="url" value="Inputs1.html" checked/> Yes
    <input type="radio" name="url" value="ResultsPage.html"/> No
    
<br><br>
<label>Dataset description:
</label>
<input type ="text" name= "Dataset" size="30"><br><br><br>
<table id="Previous Datasets">
  <tr>
    <th>Check</th>
    <th>Token Number</th>
    <th>Dataset description</th>
  </tr>
  <tr>
    <td><input type="checkbox" name="condition[]"></td>
    <td>234567</td>
    <td>MHEX North America Dataset</td>
  </tr>
  <tr>
    <td><input type="checkbox" name="condition[]" value="Token"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="condition[]"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="condition[]"></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td><input type="checkbox" name="condition[]"></td>
    <td></td>
    <td></td>
</tr>
</table>
<div style="text-align: center"><br>
  <input type="Submit" name="submit" value="Submit" class="submit">
<div class="spacer"></div>
</form>
</div>
</body>







QUOTE(kjpr @ Sep 3 2013, 01:17 PM) *

I dont know how to do that.. Could you please modify my code ..

Thanks




QUOTE(pandy @ Sep 3 2013, 01:14 PM) *

The only thing you can create on a user machine from the Web is cookies.


Posted by: Christian J Sep 3 2013, 02:58 PM

QUOTE(kjpr @ Sep 3 2013, 07:35 PM) *

I am trying to save the input form data locally in a text file. I dont have access to the webserver. so, i am planning to save the data locally at first and then change the path , once i get the access.

In that case javascript and cookies seems like a bad idea, unless you plan to rely on cookies for the published site as well. Instead you might download a test server to use locally, in which case e.g. PHP run on it can write to local text files. The Apache server, PHP module and (optionally) MySQL database are free to download, but I've heard that complete packages like XAMPP are easier to install/configure.

BTW your code examples seem to mix PHP and javascript inside SCRIPT elements. PHP is only parsed on the server, while javascript is parsed in the user's browser. See also http://www.php.net/manual/en/language.basic-syntax.phptags.php

Posted by: pandy Sep 3 2013, 03:06 PM

I didn't mean you should use cookies (even if it may be a possibility). I meant that you can't do what you want. You can't create and/or change files on a user machine, even if it happens to be your own. Well, apart from cookies, that is. wink.gif

Posted by: Christian J Sep 3 2013, 06:29 PM

QUOTE(pandy @ Sep 3 2013, 10:06 PM) *

You can't create and/or change files on a user machine, even if it happens to be your own.

Not with javascript, but with server-side scripting (run by a server program on a user machine) you can.

Posted by: pandy Sep 4 2013, 01:09 AM

The problem is he isn't using something server side. Still, while you can download a file, it would be hard to edit a file without going through the whole upload and save processes.

Posted by: Christian J Sep 4 2013, 07:04 AM

QUOTE(pandy @ Sep 4 2013, 08:09 AM) *

The problem is he isn't using something server side.

My reading is that he will do so later:
"I dont have access to the webserver. so, i am planning to save the data locally at first and then change the path , once i get the access."

but until he gets a proper web host he wants to work locally, in which case a local test server is the way to go IMO.

Not sure what we are discussing here... huh.gif








Posted by: pandy Sep 4 2013, 08:12 AM

Sure, but then the file will be on the server, not the local machine. Even if it happens to be the same computer.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)