将表单数据发送到HTML页面,并在没有任何PHP / Python的情况下使用Javascript进行检索 [英] Sending form data to HTML Page and Retrieving it with Javascript without any PHP/Python

查看:109
本文介绍了将表单数据发送到HTML页面,并在没有任何PHP / Python的情况下使用Javascript进行检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些关于将表单数据发送到另一个html页面的文章和问题线索,但是这些解决方案并没有为我解决问题。



基本上,我有一个数据表格:

 < form id =registrationname =registrationaction =register.htmlmethod =POST> 

在register.html中,我尝试访问输入文本字段,其中id和name分别为username这:

var x = document.getElementById(registration)。elements.namedItem(username)。value;



控制台声明它不能读取null的属性。我怎样才能用Javascript访问这些值?框架很好,但不是PHP / Python。

解决方案

前几天我碰到类似这样的东西。原来,你可以用jQuery获取值。

  var userName = $(input [type = text] [name = username])。val(); 

只需将其放入窗体的onsubmit中调用的函数即可。

 < script> 
函数getFormData(){
var userName = $(input [type = text] [name = username])。val();
//用数据
做你想做的事情返回true;
}
< / script>
< form id =registrationname =registrationaction =register.htmlonsubmit =return getFormData();方法= POST >
< input type =textname =username/>
< / form>

但是,当您加载下一页时,这不会保留数据。要做到这一点,只需将该值提交到sessionStorage。

  function saveFormData(){
var userName = $(输入[类型=文本] [名=用户名])VAL();
sessionStorage.setItem(user,userName);
返回true;
}

然后在下一页(您要调用的那一页resgister.html )您可以使用此代码来检索它。

  var userName = sessionStorage.getItem(user); 

我希望这有助于您!


I've read a few articles and a question thread on sending form data to another html page, but the solutions didn't solve the issue for me.

Basically, I have a form with a bunch of data:

<form id="registration" name="registration" action="register.html" method="POST">

In register.html, I tried accessing an input text field with id and name as "username" with this:

var x = document.getElementById("registration").elements.namedItem("username").value;

The console stated that it cannot read property of null. How can I access these values with Javascript only? Frameworks are fine but not PHP /Python.

解决方案

I ran into something like this the other day. Turns out you can just get the values with jQuery.

var userName = $("input[type=text][name=username]").val();

Just put it into a function that's called in the form's onsubmit.

<script>
  function getFormData() {
    var userName = $("input[type=text][name=username]").val();
    // Do what you want with the data
    return true;
  }
</script>
<form id="registration" name="registration" action="register.html" onsubmit="return getFormData();" method="POST">
  <input type="text" name="username" />
</form>

However, this doesn't keep the data when you load the next page. To do that, just commit the value into sessionStorage.

function saveFormData() {
  var userName = $("input[type=text][name=username]").val();
  sessionStorage.setItem("user", userName);
  return true;
}

And then on the next page (the one you're calling resgister.html) you can use this code to retrieve it.

var userName = sessionStorage.getItem("user");

I hope this helps!

这篇关于将表单数据发送到HTML页面,并在没有任何PHP / Python的情况下使用Javascript进行检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆