简体   繁体   English

需要访问使用php输入到表单中的数据,然后将其发送到javascript,并且都在同一页面上而无需重新加载吗?

[英]Need to access data entered into a form using php and then send it to javascript all on the same page and without reloading?

I have a user registration form (register.php), the user enters username, password and email. 我有一个用户注册表(register.php),用户输入用户名,密码和电子邮件。 These then get sent to another php page (formValidate.php) for validation using ajax. 然后将这些发送到另一个php页面(formValidate.php),以使用ajax进行验证。

I would like to convert the password entered into the form to md5 hash using php, then pass this variable to javascript, so that javascript can pass this data on to formValidate.php 我想使用php将输入表单的密码转换为md5哈希,然后将此变量传递给javascript,以便javascript可以将此数据传递给formValidate.php

As far as I understand it, this should all happen without having to reload the register.php page since I also have javascript on this page which is sending the data entered to my formValidate.php page. 据我了解,这一切都应该发生而无需重新加载register.php页面,因为该页面上也有JavaScript,它将输入的数据发送到formValidate.php页面。

I am unsure how to go about doing this. 我不确定该怎么做。

In order to accomplish this, you will have to use AJAX. 为了做到这一点,您将不得不使用AJAX。 In essence, there are two parts: 本质上,有两个部分:

1) You need a PHP script that will accept a post variable for the password, will md5 hash it, and then echo it as output. 1)您需要一个PHP脚本,该脚本将接受密码的post变量,将md5对其进行哈希处理,然后将其作为输出回显。 Lets call this to_md5.php 让我们称之为to_md5.php

2) Within your registration page, you need to intercept the form submission, have an ajax call that will submit the password to your to_md5.php script. 2)在您的注册页面中,您需要截取表单提交,进行ajax调用,将密码提交给to_md5.php脚本。 You also need to have a callback function setup to finish form submission when you get the MD5 hashed password. 当您获得MD5哈希密码时,还需要设置一个回调函数来完成表单提交。 Within the callback function, you can change the password value and submit the form. 在回调函数中,您可以更改密码值并提交表单。

There are probably better ways to do this, but hopefully it gives you a rough idea. 可能有更好的方法来执行此操作,但希望它可以给您一个大概的想法。

[EDIT] [编辑]

That's the problem, I was advised not to use javascript to send the plain text password to a php page. 这就是问题所在,建议我不要使用javascript将纯文本密码发送到php页面。

Ah, I see. 知道了 Luckily there are a lot of crypto algorithms implemented in Javascript as well. 幸运的是,还有许多用Javascript实现的加密算法。 One that comes to mind is CryptoJS hosted on Google Code. 我想到的一个是托管在Google Code上的CryptoJS

The MD5 implementation looks something like this: MD5实现看起来像这样:

<script type="text/javascript" src="http://crypto-js.googlecode.com/files/2.5.3-crypto-md5.js"></script>

<script type="text/javascript">

   var digest = Crypto.MD5("MyP@ssw0rd");

   // Two different ways of creating the MD5 hash...
   var digestBytes = Crypto.MD5("MyP@ssw0rd", { asBytes: true });
   var digestString = Crypto.MD5("MyP@ssw0rd", { asString: true });

</script>

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

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