skyguy skyguy - 6 months ago 10
PHP Question

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

I am trying to echo the contents of an input field on enter. I have a text input field that I create here:

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


The field calls this javascript function correctly:

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


Which references password change.php here:

<?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."'";*/

?>


I'm not versed in PHP but Hello world is output to console. How can I output/store value of text field in $pass?

Answer

Untested, off the cuff:

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

Note also that it is dataType: not datatype:, and that dataType only affects returning data not data being sent to PHP

So, get it working simply first (as above), then get fancy with $('#changePassForm').serialize() etc.

Comments