简体   繁体   English

您如何获取保存在表单(html)中的数据?

[英]How do you get the data you saved in form (html)?

I have no idea of html and need your help to do something.我不知道 html 需要你的帮助才能做点什么。

I wrote this code:我写了这段代码:

<form action="Programmierung/1.html">
    <table>
        <tr>
            <td><label for = "name">Name</label></td>
        </tr>
        <tr>
            <td><input id="name" type="text" size="15"/></td>
        </tr>
        <tr>
            <td><label for = "vorname">Vorname</label></td>
        </tr>
        <tr>
            <td><input id="vorname" type="text" size="15"/></td>
        </tr>
        <tr>
            <td><label for = "geburtsdatum">Geburtsdatum</label></td>
        </tr>
        <tr>
            <td><input id="geburtsdatum" type="date"/></td>
        </tr>
        <tr>
            <td><label for = "strasse">Straße</label></td>
            <td><label for = "hausnummer">Hausnummer</label></td>
        </tr>
        <tr>
            <td><input id="strasse" type="text" size="15"/></td>
            <td><input id="hausnummer" type="text" size="15"/></td>
        </tr>   
        <tr>
            <td><label for = "plz">Postleittzahl</label></td>   
            <td><label for = "ort">Ort</label></td>
        </tr>
        <tr>
            <td><input id="plz" type="text" size="15"/></td>
            <td><input id="ort" type="text" size="15"/></td>
        </tr>
        <tr>
    </table>
    <br><br>
    <table>
            <td><input type="submit" value="01: Personenbezogene Daten"/></td>
    </table> 
</form>

I want to send all the variables to another html page named "1.html", but I don't know how.我想将所有变量发送到另一个名为“1.html”的 html 页面,但我不知道如何。 What did I do wrong?我做错了什么? Do I need php for that?我需要 php 吗? And if yes how do I implement it?如果是,我该如何实施?

These days there is no need to use forms to transmit data to other pages.这些天没有必要使用 forms 将数据传输到其他页面。 That was how it was done in the 1990s... but today, we use AJAX.这就是 1990 年代的做法……但今天,我们使用 AJAX。

With a form, the user enters data, clicks submit, and the page sends the data - and navigates over to - another page.使用表单,用户输入数据,点击提交,页面发送数据 - 并导航到 - 另一个页面。 The view actually changes over to that other page.该视图实际上切换到该其他页面。

AJAX is even simpler than working with forms, especially if you use the jQuery $.ajax().done() code block . AJAX 甚至比使用 forms 更简单,特别是如果您使用 jQuery $.ajax().done()代码块 With AJAX, you:使用 AJAX,您:

a.一个。 Collect the data from the user inputs从用户输入中收集数据

b.湾。 Specify the server-side processing file (a.PHP file is quite easy to do)指定服务器端处理文件(a.PHP文件很容易做到)

c. c。 When the user clicks "submit", the data is sent over to the.php file, you receive it over there and do something with it, then the.php file sends back (using the php command echo ) a string of data to the .done() part of the AJAX code block, and inside the .done() block, you receive the data from the php side, and当用户点击“提交”时,数据被发送到.php文件,你在那里接收它并对其进行处理,然后.php文件发回(使用ZE1BFD762321E409CEE4AC0B6E8的echo显命令)aC4 string AJAX 代码块的.done()部分,在.done()块内,您从 php 端接收数据,并且

d. d。 Still inside the .done() block, you have the opportunity to modify the webpage with the newly received data.仍然在.done()块内,您有机会使用新收到的数据修改网页。

So, for example, you can:因此,例如,您可以:

  • Send login information to the.php file, and it can respond with a pass or fail, zero or one, response.将登录信息发送到.php文件,它可以响应通过或失败,零或一,响应。 Or, the php side can send back some hidden data that non-authenticated users are not permitted to see.或者,php侧可以发回一些未经认证的用户不允许看到的隐藏数据。

  • Send an address to the back-end file, and the php file can insert that into a database (MySQL or MariaDB, for example)将地址发送到后端文件,php 文件可以将其插入数据库(例如 MySQL 或 MariaDB)

  • Send quiz answers to the back-end file and it can respond with the correct answer, or with just a 1/0 (pass/fail), whatever.将测验答案发送到后端文件,它可以以正确答案或仅以 1/0(通过/失败)响应,等等。

When the ajax code block is finished, the user is still on the same page as before -- but the page may have been updated with new information.当 ajax 代码块完成后,用户仍然在与以前相同的页面上——但该页面可能已经更新了新信息。 So, if you have several ajax events happening on your page, you can do all the interaction with the user that you wish - you do not need other pages.因此,如果您的页面上发生了多个 ajax 事件,您可以与用户进行您希望的所有交互 - 您不需要其他页面。

Important notes:重要笔记:

  1. jQuery is now outdated. jQuery 现在已经过时了。 Vanilla javascript is much improved since 2015, and the Fetch API is almost as easy to use as jQuery's $.ajax() . Vanilla javascript 自 2015 年以来有了很大改进,Fetch API 几乎与 jQuery 的$.ajax()一样易于使用。 However, it is more difficult to explain and still a bit more difficult for a beginner.但是,它更难以解释,对于初学者来说仍然有点困难。 I suggest that you study the $.ajax() way of doing it, and then try to switch over to using Fetch.我建议你研究一下$.ajax()的做法,然后尝试切换到使用 Fetch。

  2. You can still use a <form> container and a submit button, but you do not put an action= attribute on the form -- the back end processing file is specified in the $.ajax() code block.您仍然可以使用<form>容器和提交按钮,但不要在表单上放置action=属性——后端处理文件在$.ajax()代码块中指定。

  3. And most importantly, if you use a form, you must trap the click event on the submit button (with javascript), and use event.preventDefault() to stop the form from trying to navigate away from the current page.最重要的是,如果您使用表单,则必须捕获提交按钮上的单击事件(使用 javascript),并使用event.preventDefault()来阻止表单尝试离开当前页面。 See the following examples:请参阅以下示例:

AJAX vs Form Submission AJAX 与表单提交

How can I make, when clicking a button, send an email with the data included from the form? 如何在单击按钮时发送包含表单中数据的 email?

values not updated after an ajax response 在 ajax 响应后未更新值

How to make an Ajax Contact Form 如何制作 Ajax 联系表

Implementing PHP实现 PHP

Is easy.简单。 Just rename the file from 1.html to 1.php .只需将文件从1.html重命名为1.php Done.完毕。

Inside any.php file, the actual php code must be surrounded with "start" and "finish" tags that look like this:在 any.php 文件中,实际的 php 代码必须用“开始”和“完成”标签包围,如下所示:

<?php to start, and <?php开始,并且

?> to finish. ?>完成。

Often, the file begins with the <?php ... and if the entire file is php code then the final ?> may be omitted.通常,文件以<?php ... 开头,如果整个文件是 php 代码,那么最后的?>可以省略。 If you are using a php file as a back-end processing file for AJAX, it is common for the entire file to be php - in which case you would begin the file with <?php and the processing would end when you echo the data back to the client-side page (usually called index.html or index.php or some such. If you are using a php file as a back-end processing file for AJAX, it is common for the entire file to be php - in which case you would begin the file with <?php and the processing would end when you echo the data返回客户端页面(通常称为index.htmlindex.php或类似名称。

Note that a file ending in .php doesn't need to have any php code in it at all... You can rename all your .html files to .php and everything will still work the same. Note that a file ending in .php doesn't need to have any php code in it at all... You can rename all your .html files to .php and everything will still work the same. The only difference is that now you can use.php code inside that file, however the php code will be processed all at once, at the beginning, before the user ever has the opportunity to interact with the page.唯一的区别是现在您可以在该文件中使用.php 代码,但是 php 代码将在用户有机会与页面交互之前在开始时一次性处理。

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

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