简体   繁体   中英

data is not sent to php from ajax

hi
in the code below i want to send the email (with input data by user, in case email address) to a php file but

$email = $_POST['email']; <<<<<<<<<<<< this retrieve nothing in the php file

js file

<script type="text/javascript">
    $(document).ready(function() {
        $("#Form").submit(function() {
             $.ajax({
                 url: "data.php",
                 type: "post",
                 dataType: "json",
                 data: { email: $('#email').val()},
                 success: function(data) {
                     if () {
                         //something
                     } else {
                         //something
                     }
                 }
             });
             return false;
         });
    });
</script>

any reason for that?

i tested email="onePrivateEmail" in php and the validation is correct, so the problem is the email is not sent by js file. Correct?

thanks

form:

<form method="post" id="Form" action="">
                        <div>
                            <label for="name">Name</label> 
                            <input id="name" name="name" type="text" /> 
                            <span id="nameInfo">Insira o seu nome</span>
                        </div>
                        <div>
                            <label for="email">E-mail</label> 
                            <input id="email" name="email" type="text" /> 
                            <span id="emailInfo">Insira um email válido por favor!</span>
                        </div>
                        <div>
                            <label for="myPassword">Password</label> 
                            <input id="myPassword" name="myPassword" type="password" /> 
                            <span id="myPasswordInfo">Insira pelo menos 4 letras e sem espaços</span>
                            <div id="bar" style="width: 234px; height: 20px;"></div>
                        </div>
                        <div>
                            <label for="pass2">Confirm Password</label> 
                            <input id="pass2" name="pass2" type="password" /> 
                            <span id="pass2Info">Confirme a password</span>
                        </div>
                        <div></div>
                        <div>
                            <input id="send" name="send" type="submit" value="Send" />
                        </div>

as i said (before deleted)

<script type="text/javascript">
$(document).ready(function() {

   alert( $('#email').val() ); 

});
</script>

this doesn't show anything in the alert

your not targeting the right form you want

 $("#customForm").submit(function() { ....

given the new information I suspect that either you don't have jquery loaded or you did put anything in the inputs

here is a demo showing data in eamil

I would assign email to a variable and console.log(). Then assign that variable to the email key/value pair you have above. Might help track down your problem. Be sure to check your console and check you are getting the value you expect.

<script type="text/javascript">
$(document).ready(function() {
    var email = $('#email').val();
    console.log(email);
    $("#Form").submit(function() {
         $.ajax({
             url: "data.php",
             type: "post",
             dataType: "json",
             data: { email: email},
             success: function(data) {
                 if () {
                     //something
                 } else {
                     //something
                 }
             }
         });
         return false;
     });
});

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