将javascript变量传递给PHP无效

时间:2015-06-08 01:37:53

标签: javascript php jquery ajax

我提交表单时没有收到任何错误,但代码不执行$ .post()函数。当有人放置信息时,它不会更新数据库。好像根本没有进入控制器文档。有人可以帮我弄清楚发生了什么吗?此外,如果有解决方法。谢谢!

index.html:

<form method='POST'>
   <input type="text"       id="fname"          name = "fname"      class="email" placeholder="First Name"              required=""/>
   <input type="text"       id="lname"          name = "lname"      class="email" placeholder="Last Name"               required=""/>
   <input type="text"       id="company_name"   name="company_name" class="email" placeholder="Company"                 required=""/>
   <input type="text"       id="citizenship"    name="citizenship"  class="email" placeholder="Country of Citizenship"  required=""/>
   <input type="text"       id="email"          name="email"        class="email" placeholder="Email Address"           required=""/>

    <ul class="login-buttons">
        <li><input  id="signIn" type="submit"  name="Sign In" class="submitButton"/></li>
</ul></form>


        <script type="text/javascript">
             $(function () {
                $('#signIn').click(function () {
                    var fname = $('#fname').val();
                    var lname = $('#lname').val();
                    var citizenship = $('#citizenship').val();
                    var company_name = $('#company_name').val();
                    var email = $('#email').val();

                    console.log(fname, lname, citizenship, company_name, email); 


        if(fname == '' || lname == '' || company_name == '' || citizenship == '' || email == ''){
            if(fname == ""){
                console.log("Please fill in your first name");
            }
            if(lname == ''){
                console.log("Please fill in your last name");
            }
            if(citizenship == ''){
                console.log("Please fill in your citizenship");
            }                                                                                               
            if(company_name == ''){
                console.log("Please fill in your company name");
            }                                               
            if(email == ''){
                console.log("Please fill in your email");
            }                                               
            }else{
                console.log("executing signIn function");
            var postData = {
                action: "signIn",
                fname: fname,
                lname: lname,
                company_name: company_name,
                citizenship: citizenship,
                email: email
            }
    $.post('controller/user_c.php', postData, function (e){
          if(parseInt(e.msgType) == 1){
               console.log("Success!");
            }else{
                console.log("no Success!");
            }
        }, "json");
    }
  });
});

usr_c.php:

///////////////////usr_c.php////////////////////
<?php
include '../models/user_m.php';

$ctrl = new user_m();
if (array_key_exists("action", $_POST)) {
    if ($_POST['action'] == "signIn") {
        $ctrl->setUsr_name($_POST['fname']);
        $ctrl->setUsr_pass($_POST['lname']);
        $ctrl->setUsr_fname($_POST['citizenship']);
        $ctrl->setUsr_lname($_POST['company_name']);
        $ctrl->setUsr_email($_POST['email']);
        $ctrl->signIn();
    } 
}
?>

user_m.php:

//////////////////////user_m.php//////////////////////////

<?php

session_start();
include 'dbConfig/connectDB.php';

/**
 * Description of user_m
 *
 * @author vertexsolution
 */
class User_M extends ConnectDB {

    const DB_TABLE = 'attendees';


    private $fname;
    private $lname;
    private $company_name;
    private $citizenship;
    private $email;

    function get_fname() {
        return $this->fname;
    }

    function get_lname() {
        return $this->lname;
    }

    function get_comapny_name() {
        return $this->company_name;
    }

    function get_citizenship() {
        return $this->citizenship;
    }

    function get_email() {
        return $this->email;
    }

    function set_fname($fname) {
        $this->fname = $fname;
    }

    function set_lname($lname) {
        $this->lname = $lname;
    }

    function set_company_name($usr_fname) {
        $this->company_name = $company_name;
    }

    function set_citizenship($citizenship) {
        $this->citizenship = $citizenship;
    }

    function set_email($email) {
        $this->email = $email;
    }

    public function signIn(){
        this->get_fname();
        this->get_lname();
        this->get_comapny_name();
        this->get_citizenship();
        this->get_email();

        $sql = "INSERT INTO `attendees` (`fname`, `lname`, `company_name`, `citizenship`, `email`) VALUES (:fname, :lname, :company_name, :citizenship, :email);";

        this->con->beginTransaction();

        try{
            $createstmt = $this->con->prepare($sql);
            $createstmt -> bindParam(':fname', $this->get_fname(), PDO::PARAM_STR);
            $createstmt -> bindParam(':lname', $this->get_lname(), PDO::PARAM_STR);
            $createstmt -> bindParam(':company_name', $this->get_comapny_name(), PDO::PARAM_STR);
            $createstmt -> bindParam(':citizenship', $this->get_citizenship(), PDO::PARAM_STR);
            $createstmt -> bindParam(':email', $this->get_email(), PDO::PARAM_STR);

                if($createstmt->execute()){
                    $this->con->commit();
                    echo json_encode(array("msgType" => 1, "msg" => "Welcome! You may proceed and network."));       
            }else{
                $this->con->rollback();
                echo json_encode(array("msgType" => 2, "msg" => "There was a problem signing you in. Please notify a board member about this issue."));
            }
        }catch(Exception $exc){
            $this->con->rollback();
            echo json_encode(array("msgType" => 2, "msg" => "There was a problem signing you in. Please notify a board member about this issue."));
        }
    }

}

0 个答案:

没有答案