尝试在PHP中回显输入文本时出现未定义的索引错误


Undefined index error when trying to echo input text in PHP?

我正在尝试在回车时回显输入字段的内容。我在这里创建了一个文本输入字段:

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

该字段正确调用此javascript函数:

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
    $.ajax({
        url: "../php/passwordchange.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        datatype: 'text',
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }});    
     console.log("WORKS!!");   
    }
});

此处引用密码更改.php:

<?php
session_start();
    echo "Hello world";
    $pass=$_POST['passwordText']; //name of input
    echo $pass;
/*$dbh = new PDO("mysql:host=localhost;dbname=sqlserver", 'username', 'password');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $checkforpass = "SELECT password FROM sqlserver.accounts WHERE username='".$username."'";*/
?>

我不精通PHP,但Hello世界是控制台的输出。如何在$pass中输出/存储文本字段的值?

未测试,即兴:

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
      var pass = $('#changePassForm input').val();
    $.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 server, a set of key/value pairs (i.e. form fields and values)
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }});    
     console.log("WORKS!!");   
    }
});

还要注意,它是dataType:而不是datatype:,并且dataType只影响返回数据,而不影响发送到PHP的数据

因此,首先简单地让它工作(如上所述),然后使用$('#changePassForm').serialize()等。

按以下方式尝试

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
      var postData = $('#changePassForm').serializeArray();
    $.ajax({
        url: "../php/passwordchange.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: postData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        datatype: 'text',
        contentType: false,       // The content type used when sending data to the server.
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }});    
     console.log("WORKS!!");   
    }
});

如果您想使用FormData,您可以从手册中看到,FormData对象构造函数采用了一个可选的<form>元素,而您使用的是this,它指的是一个jQuery对象$(".passwordText")。您可以通过以下操作传递表单元素:

var form = document.getElementById("changePassForm");
var fd = new FormData(form);

把这些放在一起,我们就会有:

$(document).ready(function() {
  $(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
      event.preventDefault();
      var form = document.getElementById("changePassForm");
      var fd = new FormData(form);
      $.ajax({
        url: "../php/passwordchange.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: fd, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
        dataType: 'text',
        cache: false,             // To unable request pages to be cached
        processData:false,        // To send DOMDocument or non processed data file it is set to false
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }});    
       console.log("WORKS!!");   
    }
  });
});

作为一种选择,您可以手动附加您希望在ajax请求中发送的任何值,如下所示:

var fd = new FormData();
fd.append($(this).attr("name"), $(this).attr("value"));

免责声明:

也就是说,FormData接口只在IE>=10中可用,所以如果你担心跨浏览器兼容性,你可以考虑简单地使用jQuery的serialize()方法来发送data。作为额外的奖励,它只有一行代码。:

data: $("#changePassForm").serialize()