简体   繁体   English

使用PHP / Javascript将html表转换为文本文件

[英]Convert html table to text file using PHP/Javascript

I want to ask all of you for some help since I'm still new to web programming. 我想向大家寻求帮助,因为我还是Web编程的新手。 So basically I have html table like this: 所以基本上我有这样的html表:

<table width='200' border='1'>
<TH>Year 1 Spring</TH>

<TR>
<TD>
<div id="y1f_0">one</div>
<div id="y1f_1">one</div>
</TD>
<TD>
<div id="y1s_0">two</div>
<div id="y1s_1">two</div>
</TD>
</TR>
</table>
<input  type="button" onclick="something">

and for example if I click the button, and it will save the table above and output/export it as a text file so later one I want get back that text file to export it back to the html table. 例如,如果我单击该按钮,它将保存上面的表并将其输出/导出为文本文件,因此稍后我想找回该文本文件以将其导出回html表。 I don't really know how to do this, should I use PHP? 我真的不知道该怎么做,我应该使用PHP吗? Really appreciate all your help. 非常感谢您的帮助。

you would use javascript ( jquery ) to collect the text and send it to a php file to save it on the server. 您将使用javascript(jquery)收集文本并将其发送到php文件以将其保存在服务器上。

example of javascript( jquery ): javascript(jquery)的示例:

$( "#some_button" ).click(function( ){

    var file_text;

    file_text =  $( "#y1f_0" ).text( );
    file_text += $( "#y1f_1" ).text( );

    file_text += $( "#y1s_0" ).text( );
    file_text += $( "#y1s_1" ).text( );

    $.post( "save_file.php", file_data : file_text , function( data ){
        alert( data + " bytes have been written" );
    });
});

example of php script: php脚本示例:

<?php
echo file_put_contents( "data.txt", $_POST[ "file_data" ] );
?>

Read up on jquery and php on http://w3schools.com http://w3schools.com上阅读jquery和php

You'll find some more information there on $.post and other methods from javascript and php. 您可以在$ .post以及javascript和php的其他方法中找到更多信息。

If you're new to web programming, it might take a little learning to get a hang of what you're trying to do. 如果您不熟悉Web编程,则可能需要一点学习才能掌握您要尝试做的事情。 This is because you have to learn how to get the client script ( on the user's browser ) to connect to the server ( where the website files are ). 这是因为您必须学习如何获取客户端脚本(在用户浏览器上)以连接到服务器(网站文件所在的位置)。 If you don't have a server, you need to download one ( like apache ) or sign up to use one ( like godaddy.com ). 如果您没有服务器,则需要下载一台服务器(如apache)或注册以使用一台服务器(如godaddy.com)。 With apache, you will have to do more learning but it's free. 使用apache,您将需要做更多的学习,但这是免费的。 Otherwise, it's convenient to use a external server so that everything is already done for you. 否则,使用外部服务器会很方便,以便一切都已为您完成。

Here's some quick notes on the rough handed example: 以下是一些粗略示例的简要说明:

  • The '#' refers to the id of the element. “#”是指元素的ID。

  • $( "#some_button" ).click is executed when the user clicks the div it's associated with. 当用户单击与其关联的div时,将执行$(“ #some_button”).click。

Just wanted to add that in order to make the above code work i had to add the following curly brackets around the file_data : file_text 只是想添加一下,以便使上面的代码起作用,我不得不在file_data周围添加以下大括号:file_text

 $.post( "save_file.php", {file_data : file_text} , function( data ){
    alert( data + " bytes have been written" );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM