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

时间:2016-05-18 04:39:41

标签: javascript php ajax

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

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!!");   
    }
});

这里引用密码change.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 world输出到控制台。如何在$ pass中输出/存储文本字段的值?

3 个答案:

答案 0 :(得分:0)

尝试以下

$(".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!!");   
    }
});

答案 1 :(得分:0)

未经测试,脱下袖口:

$(".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()等等。

答案 2 :(得分:0)

如果您想使用FormData,可以从the manual看到FormData对象构造函数采用可选的<form>元素并且您正在使用this },它指的是$(".passwordText"),它是一个jQuery对象。您可以通过执行以下操作传递表单元素:

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!!");   
    }
  });
});

作为替代方案,您可以手动append您希望在ajax请求中发送的任何值,如下所示:

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

声明:

话虽如此,FormData界面仅在IE&gt; = 10中可用,所以如果您担心跨浏览器兼容性,您可能需要考虑使用jQuery的serialize() method来发送data 。作为额外的奖励,它只有一行代码。:

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