Javascript:当表单使用类选择器代替时,来自表单输入的变量从PHP返回为undefined

时间:2016-05-19 00:15:14

标签: javascript php html forms

我不知道这里发生了什么。我的表格如下:

echo '<form class="changePassForm" action="" method="post" enctype="multipart/form-data">';
echo '<input class = "passwordText" type="password" placeholder="Password" name="passwordText">';
echo '</form>';

表单必须有类选择器,以便我的css在下面工作(我有点不清楚为什么,但我还没有找到解决方法):

.changePassForm {
  padding: 20px 0;
  position: relative;
  z-index: 2;
}
.passwordText {
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  outline: 0;
  border: 1px solid rgba(255, 255, 255, 0.4);
  background-color: rgba(255, 255, 255, 0.2);
  width: 250px;
  border-radius: 3px;
  padding: 10px 15px;
  margin: 0 auto 10px auto;
  display: block;
  text-align: center;
  font-size: 18px;
  color: white;
  -webkit-transition-duration: 0.25s;
          transition-duration: 0.25s;
  font-weight: 300;
}
.passwordText :hover {
  background-color: rgba(255, 255, 255, 0.4);
}
.passwordText :focus {
  background-color: white;
  width: 300px;
  color: #53e3a6;
}
.changePassForm button {
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none;
  outline: 0;
  background-color: white;
  border: 0;
  padding: 10px 15px;
  color: #53e3a6;
  border-radius: 3px;
  width: 250px;
  cursor: pointer;
  font-size: 18px;
  -webkit-transition-duration: 0.25s;
          transition-duration: 0.25s;
}
.changePassForm button:hover {
  background-color: #f5f7f9;
}

下面是应该将传递中存储的表单中的输入传递给要回显的php文件的javascript。我知道这是有效的,因为console.log“WORKS !!”输出正确。

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
        var pass = $('.changePassForm input').val();
        //var pass = document.getElementById("p2");
        $.ajax({
            url: "../php/passwordchange.php", // Url to which the request is send
            type: "POST",             // Type of request to be send, called as method
            data: 'passwordText=' + pass, // data sent to php file
            success: function(data)   // A function to be called if request succeeds
            {
                console.log(data);

            }});    
        console.log("WORKS!!");   
    }
});

下面是我的php应该回显在表单中输入的内容:

session_start();
include("../php/Session.class.php");
$sess = new Session();
$sess->Init();
$cookie = isset($_COOKIE["session"]); 
if($cookie) 
{
    $cookie = $_COOKIE["session"];
    $account = $sess->Verify($cookie);
}
$pass=$_POST['passwordText']; //name of input
echo $pass;

实际上,传递被回复为undefined。这很麻烦,因为如果我将表单的选择器更改为id而不是类,并将传递更改为

$('#changePassForm input').val();

正确回显输入文本。这里出了什么问题?我错过了什么吗?

0 个答案:

没有答案