简体   繁体   English

(HTML)JavaScript + PHP - $ _GET错误

[英](HTML) JavaScript + PHP - $_GET Error

What I would like to achieve: 实现:

  • A index page ( index.html ), which allows the user to register, which runs on JavaScript ( index.js ) to check the fields ( not mentioned in snippet index.js ), and then to redirect to a register page ( scripts/register.php ), which then adds the values to the database. 一个索引页面( index.html ),它允许用户注册,它运行在JavaScript( index.js上来检查字段(在片段index.js中 没有提到),然后重定向到一个注册页面( scripts / register.php ),然后将值添加到数据库中。

What is actually happening: 实际发生了什么:

  • It redirects to the PHP page correctly, however none of the values seem to be transferred when using the $_GET method: I get an empty page. 它正确地重定向到PHP页面,但是在使用$_GET方法时似乎没有传输任何值:我得到一个空页面。

What am I doing wrong? 我究竟做错了什么?

Code: 码:


index.html (only a snippet) index.html(只是一个片段)

<input name="user" type="text" id="user" size="25" />
<input name="email" type="text" id="email" size="25" />
<input name="pass" type="password" id="pass" size="25" />
<input type="submit" name="signup" id="signup" value="Sign Up" />
<script type = "text/javascript", src = "index.js">
</script>

index.js (only a snippet) index.js(只是一个片段)

document.getElementById("signup").onclick = signup;
var aref = "refcode";
function signup()
{
    window.location.href = 'scripts/register.php?emailaddress=' + document.getElementById("email").value + '&username=' + document.getElementById("user").value + '&password=' + document.getElementById("pass").value + '&aref=' + aref;
}

scripts/register.php (only a snippet) scripts / register.php(只是一个片段)

<?php 
echo $_GET['emailaddress'];
echo $_GET['username']; 
echo $_GET['password']; 
echo $_GET['aref'];
?>

EDIT: I accidentally copied the wrong code for 'scripts/register.php', sorry to all the answers who corrected it for me 编辑:我不小心为'scripts / register.php'复制了错误的代码,对不起给我纠正的所有答案

You're never submitting the form (because you don't seem to have one), thus never getting anything but the data that you embed into the URL (which is very unsecure, not a good idea to send sensitive data like passwords like that). 你永远不会提交表单(因为你似乎没有提交表单),因此从来没有得到任何东西,只是你嵌入到URL中的数据(这是非常不安全,不是一个好主意发送像这样的密码的敏感数据)。

I'm not sure, however, why are you complicating things like that. 但是,我不确定你为什么要这样复杂化。

If you want to use GET, no need to build the URL yourself, just set up the form with GET method and use regular submit to send it, no javascript needed . 如果你想使用GET,不需要自己构建URL,只需使用GET方法设置表单并使用常规提交发送它, 不需要javascript Use the hidden field for the aref value (you can populate it when the form is generated, before submitting, etc, whatever works for you): 使用隐藏字段作为aref值(您可以在生成表单时填充它,在提交之前填写等等,对您有用):

<form method="GET" action="scripts/register.php">
    <input name="aref" type="hidden" value="refcode" />
    <input name="user" type="text" id="user" size="25" />
    <input name="email" type="text" id="email" size="25" />
    <input name="pass" type="password" id="pass" size="25" />
    <input type="submit" name="signup" id="signup" value="Sign Up" />
</form>

Again, changing the method to POST would be a much better idea . 再次,将方法更改为POST将是一个更好的主意 Of course, then you need to access the variables like $_POST['aref'], etc. Just like this: 当然,那么你需要访问像$ _POST ['aref']等变量。就像这样:

<form method="POST" action="scripts/register.php">
    <input name="aref" type="hidden" value="refcode" />
    <input name="user" type="text" id="user" size="25" />
    <input name="email" type="text" id="email" size="25" />
    <input name="pass" type="password" id="pass" size="25" />
    <input type="submit" name="signup" id="signup" value="Sign Up" />
</form>

And the PHP (for POST): 和PHP(用于POST):

<?php 
echo $_POST['email'];
echo $_POST['user']; 
echo $_POST['pass']; 
echo $_POST['aref'];
?>

Your fields are not named the same way in the URL and in register.php. 您的字段在URL和register.php中的命名方式不同。 Try this. 尝试这个。

<?php 
echo $_GET['emailaddress'];
echo $_GET['username']; 
echo $_GET['password']; 
echo $_GET['aref'];
?>
window.location.href = 'scripts/register.php?emailaddress=' + document.getElementById("email").value + '&username=' + document.getElementById("user").value + '&password=' + document.getElementById("pass").value + '&aref=' + aref;

To access them: 要访问它们:

$_GET['username']
$_GET['password']
etc...

In your code you never use the good variable names: 在您的代码中,您永远不会使用好的变量名称:

<?php 
echo $_GET['email'];
echo $_GET['user']; 
echo $_GET['pass']; 
echo $_GET['accountref'];
?>

For an Solution without JS, and PHP instead: 对于没有JS和PHP的解决方案:

<form action="scripts/register.php?<? echo $refcode /* HERES YOUR REFCODE */?>" method="GET">
    <input name="user" type="text" id="user" size="25" />
    <input name="email" type="text" id="email" size="25" />
    <input name="pass" type="password" id="pass" size="25" />
    <input type="submit" name="signup" id="signup" value="Sign Up" />
</form>

The normal way to do what you want is using method Attribute in your form or the .submit() event in jquery. 执行所需操作的.submit() method是在表单中使用method属性或在jquery中使用.submit()事件。 I'll show how I would do that: 我将展示我将如何做到这一点:

HTML without javascript using POST 没有使用POST javascript的HTML

<form method="post" id="login_form" action='register.php'> 
  <input name="user" type="text" id="user" size="25" />
  <input name="email" type="text" id="email" size="25" />
  <input name="pass" type="password" id="pass" size="25" />
  <input type="submit" name="signup" id="signup" value="Sign Up" />
</form>

php using $_POST php使用$ _POST

$user = isset($_Post['user']) ? $_Post['user'] : NULL;
$email = isset($_Post['email']) ? $_Post['email'] : NULL;
$pass = isset($_Post['pass']) ? $_Post['pass'] : NULL;

HTML using Jquery 使用Jquery的 HTML

<form id="login_form" method="post" action=""> 
  <input name="user" type="text" id="user" size="25" />
  <input name="email" type="text" id="email" size="25" />
  <input name="pass" type="password" id="pass" size="25" />
  <input type="submit" name="signup" id="signup" value="Sign Up" />
</form>
<script type = "text/javascript" src = "index.js"></script>
//You have a type with a comma

JS JS

$('#login_form').submit(function(e){
  var data = $(this).serializeArray();
  $.post('register.php',data,
  function(result){
   //Your callback function
 })
})

NOTE My advise to you is that you should use POST method in this case 注意我建议您在这种情况下应该使用POST方法

GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

and

POST submits data to be processed (eg, from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

So essentially GET is used to retrieve remote data, and POST is used to insert/update remote data. 因此,本质上GET用于检索远程数据,POST用于插入/更新远程数据。

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

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