2013-10-28 33 views
0

實現:

  • 一個索引頁(的index.html),它允許用戶註冊,它運行於JavaScript (index.js)檢查字段(不在片段index.js提到),然後重定向到一個寄存器頁(腳本/ register.php),其然後將這些值對數據庫。

什麼是實際發生的事情:

  • 它重定向到正確的PHP頁面,但是沒有值似乎使用$_GET方法時轉移:我得到一個空白頁。

我在做什麼錯?

代碼:


的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(僅一個片段)

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

腳本/ register.php(僅摘錄)

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

編輯:我不小心複製了 '腳本/ register.php' 的錯誤代碼,對不起所有的答案誰糾正了它我

+0

'

0
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']; 
?> 
1

您的字段在URL和register.php中的命名方式不同。嘗試這個。

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

他們是一樣的'index.js' – Izkata

+0

啊,我在問題的編輯歷史中看到它最初是錯誤輸入的...... – Izkata

0

對於沒有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> 
0

正常的方式做你想要的是使用你的形式method屬性或jQuery中的.submit()事件是什麼。我會告訴我會怎麼做這:

HTML 不使用JavaScript的使用$ _ POST

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

HTMLPOST

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

<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用來插入/更新遠程數據。