简体   繁体   中英

error while sending POST request via ajax

I want to send a post request and get an echo from a php file. But i cannot find my mistake, always get a notice:undefined index...

please not jquery...

<form id="register" method="POST" action="register-action.php">

    <input type="text" name="first_name" placeholder="Onoma"><br>
    <input type="text" name="last_name" placeholder="Epitheto"><br>
    <input type="text" name="mail" placeholder="E-mail"><br>
    <input type="password" name="pass" placeholder="password"><br>

    <select name="eidikotita_id">
        <?php while($eid = $eidikotites->fetch( PDO::FETCH_ASSOC )):
            echo "<option value={$eid['id_eidikotitas']}>{$eid['titlos']}</option>";
        endwhile; ?>
    </select>

    <input type="submit" value="Register"><br>
</form>

///////////////////////////////////////////////////////////////

var mail_input = document.querySelector("#registerinput[name='mail']").value;
alert(mail_input);
document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {

var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
request.open("POST", "register-action.php", true);//Specifying the Request
request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
request.send(mail_input);//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
};

/////////////////////////////////////////////////////////////////////////////

<?php 
echo $_POST['mail'];
?>

Just print_r() POST data to see what is the problem.

xhr.send() should look like this: xhr.send('mail=' + encodeURIComponent(email)) because you want $_POST['mail'] . Also there is a problem with your selector which should be "#register input[name='mail']" .

document.querySelector("#register input[name='first_name']").focus();
document.querySelector("#register input[name='mail']").onblur = function() {

    var mail_input = document.querySelector("#register input[name='mail']").value;
    var request = new XMLHttpRequest();//instantiate an XMLHttpRequest object
    request.open("GET", "register-action.php?mail="+mail_input, true);//Specifying the Request
    request.setRequestHeader("Content-Type", "text/plain");//POST requests,for example, need a “Content-Type” header
    request.send(encodeURIComponent(mail_input));//specify the optional request body. GET requests never have a body, so you should pass null or omit the argument.
//////send succcess

    if (request.readyState === 4 && request.status === 200) {
        request.onreadystatechange = function(){
        console.log(request.responseText);      
        }
    }   
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM