点击提交按钮时,HTML表单做一些“动作”

前端之家收集整理的这篇文章主要介绍了点击提交按钮时,HTML表单做一些“动作”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想了解HTML表单.例如,我有2个输入文本字段的名字和姓氏和一个提交按钮.当点击提交按钮时,我希望网页显示如下:您的姓名是“名字”“姓氏”.
<!DOCTYPE html>
<html>
   <body>
      <form>
         First name: <input type="text" name="firstname" /><br />
         Last name: <input type="text" name="lastname" /><br />
         <input type="submit" value="Submit" />
      </form> 
   </body>
</html>

当我点击该按钮时,为了有一些“动作”,我需要有什么?

编辑:好的,现在我弄清楚,我需要PHP或JavaScript这里.有人可以建议还是提供PHP或Js的示例代码作为参考?

解决方法

好的,我会刺伤这个.如果要使用PHP,则需要在计算机上安装和配置PHP和Web服务器.本文可能让您开始: PHP Manual: Installation on Windows systems

完成环境设置后,您可以开始使用Webforms.直接从文章Processing form data with PHP

For this example you will need to create two pages. On the first page
we will create a simple HTML form to collect some data. Here is an
example:

06000

This page will
send the Name and Age data to the page process.PHP. Now lets create process.PHP to use
the data from the HTML form we made:

<?PHP   
    print "Your name is ". $Name;   
    print "<br />";   
    print "You are ". $Age . " years old";   
    print "<br />";   $old = 25 + $Age;
    print "In 25 years you will be " . $old . " years old";   
?>

As you
may be aware,if you leave out the method=”post” part of the form,the
URL with show the data. For example if your name is Bill Jones and you
are 35 years old,our process.PHP page will display as
07002 If you want,
you can manually change the URL in this way and the output will change
accordingly.

其他JavaScript示例

这个单一文件示例从您的问题中获取html,并将表单的onSubmit事件与一个JavaScript函数相关联,该函数会拉取2个文本框的值,并将其显示在警报框中.

注意:document.getElementById(“fname”).value获取ID标签等于fname的对象,然后提取它的值 – 在这种情况下,它是“名字”文本框中的文本.

<html>
    <head>
     <script type="text/javascript">
     function ExampleJS(){
        var jFirst = document.getElementById("fname").value;
        var jLast = document.getElementById("lname").value;
        alert("Your name is: " + jFirst + " " + jLast);
     }
     </script>

    </head>
    <body>
        <FORM NAME="myform" onSubmit="JavaScript:ExampleJS()">

             First name: <input type="text" id="fname" name="firstname" /><br />
             Last name:  <input type="text" id="lname" name="lastname" /><br />
            <input name="Submit"  type="submit" value="Update" />
        </FORM>
    </body>
</html>
原文链接:https://www.f2er.com/html/230926.html

猜你在找的HTML相关文章