(网页)JavaScript + PHP - $_GET 错误


(HTML) JavaScript + PHP - $_GET Error

我想实现的

  • 索引页(index.html),允许用户注册,在JavaScript(index.js)上运行以检查字段(片段索引.js提及),然后重定向到注册页(scripts/register.php),然后将值添加到数据库中。

实际发生的情况:

  • 它正确地重定向到 PHP 页面,但是在使用 $_GET 方法时似乎没有传输任何值:我得到一个空页面。

我做错了什么?

法典:


索引.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>

索引.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;
}

脚本/寄存器.php(仅一个片段)

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

编辑:我不小心复制了"脚本/注册.php的错误代码,对所有为我更正它的答案表示歉意

您永远不会提交表单(因为您似乎没有表单),因此除了嵌入到 URL 中的数据之外,永远不会得到任何东西(这是非常不安全的,发送像这样的密码等敏感数据不是一个好主意)。

但是,我不确定您为什么要使这样的事情复杂化。

如果你想使用GET,不需要自己构建URL,只需使用GET方法设置表单并使用常规提交来发送它,不需要javascript。使用隐藏字段作为 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>

同样,将方法更改为 POST 将是一个更好的主意。当然,然后您需要访问 $_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>

和PHP(用于POST):

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

您的字段在 URL 和 register.php 中的命名方式不同。试试这个。

<?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;

要访问它们:

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

在代码中,您永远不会使用好的变量名称:

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

对于没有 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>
执行

所需操作的正常方法是在窗体中使用method Attribute 或在 jquery 中使用 .submit() 事件。我将展示我将如何做到这一点:

.HTML没有JavaScript使用POST

<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 使用 $_POST

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

使用 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

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

注意我给你的建议是,在这种情况下你应该使用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.

POST submits data to be processed (e.g., 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.

所以本质上GET用于检索远程数据,POST用于插入/更新远程数据。