为什么$ _POST ['submit']为空?

时间:2012-03-02 01:59:48

标签: php html ajax null form-submit

$_POST['submit']让我发疯。我没有看到任何理由为什么它的值为NULL。

这是我的PHP代码:

if(isset($_POST['username'])  && isset($_POST['email']) && isset($_POST['email2']) && isset($_POST['password']) 
&& isset($_POST['firstname']) && isset($_POST['surname']) && isset($_POST['gender']) && isset($_POST['day'])
&& isset($_POST['month'])  && isset($_POST['year']) ) {

    $username = $_POST['username'];
    $email = $_POST['email'];
    $email2 = $_POST['email2'];
    $password = $_POST['password'];
    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $gender = $_POST['gender'];
    $day = $_POST['day'];
    $month = $_POST['month'];
    $year = $_POST['year'];

    $date = $year."-".$month."-".$day;

    $hashed_password = hashMe($password,$salt);


    if (!preg_match("/^[a-z](?=[\w.]{3,19}$)\w*\.?\w*$/i", $username)) {

        echo "invalid username";
    }
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

        echo "Invalid email.";
    }
    if ($email !== $email2) {

        echo "Emails are not matching.";
    }
    if (!preg_match("/^([^\s])[a-zA-Z]*$/i", $firstname)) {

        echo "Invalid first name.";
    }
    if (!preg_match("/^([^\s])[a-zA-Z]*$/i", $surname)) {

        echo "Invalid last name.";
    }
    if($day === "Day" || $month === "Month" || $year === "Year") {

        echo "Choose a date.";

    }
    if($gender === "Your gender:") {

        echo "choose a gender.";
    }
    else if(array_key_exists('submit', $_POST))
    {
        $mysqli = new mysqli('blaa', 'blablaa', 'blabla', 'cucumber');

        if(mysqli_connect_errno()) {
            echo "Connection Failed: " . mysqli_connect_errno();
            exit();
        }

        $stmt = $mysqli->prepare("INSERT INTO USERS (USER_USERNAME, USER_EMAIL, USER_GENDER, USER_FIRSTNAME, USER_LASTNAME,         USER_BIRTHDAY, USER_PASSWORD, USER_SALT) VALUES (?,?,?,?,?,?,?,?)");
        $stmt->bind_param('ssssssss',$username,$email,$gender,$firstname,$surname,$date,$hashed_password,$salt);
        $stmt->execute();

        echo "everything successfully inserted into the database \n";



    }
    else{
        var_dump($_POST['submit']);
        echo "something went wrong!"; 
    }
}

这是我的按钮代码:

<input class="btn btn-large btn-success" id="register-button" type="submit" name="submit" value="Registreer" onclick="handlePost()"/>

有用的知道可能:我已经尝试过阅读所有日志文件,没有任何内容。所有其他值都很好地发送到服务器,因此表单的其余部分没有问题。 handlePost()对服务器进行Ajax调用。使用var_dump($_POST)除了提交,我得到了所有内容。 var_dump($_POST['submit'])给了我NULL。

更新:handlePost()

function handlePost() {  

 var username = $('#username').val();  
 var email = $('#email').val();  
 var email2 = $('#email2').val(); 
 var password = $('#password').val();   
 var firstname = $('#firstname').val();
 var surname = $('#surname').val();
 var gender = $('#gender').val();
 var day = $('#day').val();
 var month = convertMonth($('#month').val())
 console.log(month);
 var year = $('#year').val();

 $.ajax({  
   type: "POST",  
   url: "handleRegister.php",  
   data: "username="+username+"&email="+email+"&email2="+email2+"&password="+password+"&firstname="
   +firstname+"&surname="+surname+"&gender="+gender+"&day="+day+"&month="+month+"&year="+year,  
   success: function(resp){  
     // we have the response  
     //alert("Server said:\n '" + resp + "'");  

     console.log("Server said:\n '" + resp + "'");

   },  
   error: function(e){  
     //alert('Error: ' + e);  
     console.log("Server said:\n '" + e + "'");
   }  
 });  
}  

我没有使用<form>,所以我想我也必须发送'submit'

2 个答案:

答案 0 :(得分:1)

handlePost()如何进行AJAX调用?

我的印象是,如果提交表单的事件来自按钮本身,那么提交按钮值将仅包含在POST数据中(这就是为什么当你有许多提交按钮时,只有那个是点击包含在数据中。)

修改

data调用中的$.ajax属性必须包含要发送到服务器的所有内容;它不会自动包含您未指定的表单值。

执行AJAX调用与正常提交表单不同,无论其如何触发。

答案 1 :(得分:1)

您的data字符串不包含提交字段。如果你添加它,它应该可以解决你的问题。

var sbmt = $('#register-button').val();

data: "username="+username+"&email="+email+"&email2="+email2+"&password="+password+"&firstname=" +firstname+"&surname="+surname+"&gender="+gender+"&day="+day+"&month="+month+"&year="+year+"&submit="+sbmt