脚本不会执行ajax调用

时间:2015-03-27 15:48:44

标签: php jquery ajax

我正在向文件发送ajax请求,但由于某种原因文件没有完成操作,我知道请求到达文件,因为它回应了收到的请求,并在那里停止。我无法弄清楚为什么。

<?php
 /**
* Created by PhpStorm.
* Date: 23.2.2015.
* Time: 5:20
* Version 1.1
*/
    $nacin_rada = 'echo';//promjenjivo u session
    $mysqli = mysqli_connect('localhost', 'root', 'password', 'database');
    $year = date("Y");

    echo 'request received';
    if (isset($_POST['submit'])) {   //get_magic_quotes_gpc removed in php 5.4.0
        $username = stripslashes($_POST['username']);
        $password = stripslashes(md5($_POST['password']));
        $passverif = stripslashes(md5($_POST['passverif']));
        $email = stripslashes($_POST['email']);
        $ime = stripslashes($_POST['ime']);
        $prezime = stripslashes($_POST['prezime']);
        $dan = stripslashes($_POST['dan']);
        $mjesec = stripslashes(intval($_POST['mjesec']));
        $godina = stripslashes(intval($_POST['godina']));
        $avatar_source = stripslashes($_POST['avatar']);
        $sex = stripslashes($_POST['sex']);


        if ($sex == 'žensko' and $avatar_source == '') {

                $avatar_source = 'default_profile_female.jpeg'; //potrebno dodati podršku za default sliku

        } elseif ($sex == 'muško' and $avatar_source == '') {

            $avatar_source = 'default_profile_male.jpeg'; //potrebno dodati podršku za default sliku
        }


        if ($username == '' or $password == '' or $passverif == '' or $email == '' or $ime == '' or $prezime == '' or $dan == '' or $mjesec == '' or $godina == '' or $avatar_source == '') {
            echo ' You didn\'t fill out all the forms';
            exit;
        }

        if ($password <= 2) {
            echo 'Password too short';
            exit;
        }


        if ($passverif <= 2) {
            echo 'Password verification too short';
            exit;
        }

        if ($password != $passverif) {
            echo 'Va&#353;e lozinke se ne podudaraju';
            exit;
        }

        if ($mjesec > '12' or $mjesec < '1') {
            echo $mjesec . 'nije valjani izbor';
            exit;

        }

        if ($godina > $year) {  //year defined at line 12
            echo 'Vi ste iz budu&#269;nosti?';
            exit;
        }

        if ($godina == '0000') {
            echo '0000 nije valjani izbor';
            exit;
        }


        $result = mysqli_query($mysqli, "SELECT * FROM users WHERE email='$email'") or die("Query Failed");// redefining sql query for user data selection
        $num_row = mysqli_num_rows($result);
        $row = mysqli_fetch_array($result);
        if ($num_row >= 1) {
            echo '<span style="color:white;">U bazi podataka ve&#269; postoji ra&#269;un s tim Emailom</span>';
        } elseif ($num_row == 0) {

            $insert_users = mysqli_query($mysqli, "INSERT INTO users(username, password, email, ime, prezime,sex, avatar, dan, mjesec, godina) VALUES ('$username','$password','$email','$ime','$prezime','$sex','$avatar_source','$dan','$mjesec','$godina')");
            $insert_user_data = mysqli_query($mysqli, "INSERT INTO user_data(hometown, love_status, about) VALUES ('NULL','NULL','NULL' )");
            echo 'Registracija uspje&scaron;na,mo&#382;ete se <a href="login.php">prijaviti</a>';
        } else echo 'Registracija neuspjela';


    } 
?>

3 个答案:

答案 0 :(得分:1)

JQuery ajax方法,数据参数除其他外需要一个对象,例如:

data: { username: username, password: password, passverif: passverif, email: email ... }

你正在通过:

data: "{username,password,passwordverif,email...}"

在那里不起作用的一些事情:1)不是key: value对,2)这是一个字符串,而不是一个对象。

另一个问题,这些:

var username = $('.username').val;

...需要更改为:

var username = $('.username').val();

注意最后的括号。


所以你要解决3件事。两个在JavaScript中,一个在PHP文件中。

JavaScript的:

  1. data设置中使用对象表示法。
  2. 不要引用该对象。
  3. PHP:

    1. 使用$_POST['submit']以外的内容检查服务器脚本上的表单数据。

答案 1 :(得分:1)

你有几个错误:

在你的AJAX调用中使用这种方式发送参数,比如bloodyKnuckles指出:

data: { username: username, password: password, passverif: passverif, email: email ... }

此外,未定义$_POST['submit']。相反,请尝试使用$_POST['username']或您参数中的其他内容。

答案 2 :(得分:0)

<html>
<style>
    body{
        outline: none;
        background-color:#222222;
    }

    select{
        background-color: #FFFFFF;
        border: 0 solid transparent;
        width: 100px;
    }

    table{
        margin-left: 850px;
        border: 1px solid blue;
        border-radius: 5px;
        background:#EAEAEC;
        margin-top:0;

    }
    input{width:372px;height:34px;}
    .submit{background-color:white;border:0 transparent}
    .mjesec{position:absolute;margin-left:200px;}
    .dan{margin-left:40px;position:absolute;margin-top:-25px;}

</style>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>

    function Registracija_ajax() {
        var username = $('.username').val;
        var password = $('.password').val;
        var passverif = $('passverif').val;
        var email = $('.email').val;
        var ime = $('.ime').val;
        var prezime = $('.prezime').val;
        var dan = $('.dan').val;
        var mjesec = $('.mjesec').val;
        var godina = $('.godina').val;
        var avatar_source = $('.avatar_source').val;
        var sex = $('.sex').val;
        $.ajax({
            type: "POST",
            url: "registration.php",
            data: "{username,password,passverif,email,ime,prezime,dan,mjesec,godina,avatar_source,sex}",
            beforeSend: function () {
                $('.loading_gif').css('display:inline');
            },
            success: function (response) {

                $('.response').html(response);



            }
        });

    }

</script>

<body>

        <table cellspacing="21">
                <tr>
                    <td>
                        <input name="username" required="ddd" placeholder="username" class="username">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="password" type="password" required placeholder="password" class="password">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="passverif" type="password" required placeholder="password" class="passverif">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="email" placeholder="email" required class="email">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="ime" placeholder="ime" required class="ime">
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="prezime" placeholder="prezime" required class="prezime">
                    </td>
                </tr>

                <tr>
                    <td>
                        <select name="sex" class="sex" style="margin-left:40px;position:absolute;">
                            <option value="male">Muško</option>
                            <option value="female">Žensko</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select name="godina" class="godina" style="margin-left:200px;position:absolute;margin-top:-23px;">
                            <option value="0000">Godina</option>
                            <?php
                            $beggining_year = date('Y') - 13;
                            $end_year = '1975';

                            while ($end_year <= $beggining_year) {
                                echo '<option value="' . $beggining_year . '">' . $beggining_year . '</option>';
                                $beggining_year--;
                            }
                            ?>

                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select class="mjesec" name="mjesec">
                            <option value="0000">Mjesec</option>
                            <?php
                            $beggining_month = '1';
                            $end_month = '12';

                            while ($beggining_month <= $end_month) {
                                echo '<option value="' . $end_month . '">' . $end_month . '</option>';
                                $end_month--;
                            }
                            ?>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <select class="dan" name="dan">
                            <option value="0000">Dan:</option>
                            <?php
                            $beggining_day = '1';
                            $end_day = '31';

                            while ($beggining_day <= $end_day) {
                                echo '<option value="' . $end_day . '">' . $end_day . '</option>';
                                $end_day--;
                            }
                            ?>

                        </select>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input class="avatar" name="avatar" placeholder="Profile photo(link only)">
                    </td>
                </tr>
                <tr>
                    <td>
                        <button value="submit" class="submit" onclick="Registracija_ajax()"  name="submit">Registracija</button>
                    </td>
                </tr>
        </table>
</body>
<div class="response"></div>

@Philip Sorokin,你上面有另一个文件。