AJAX发布的变量意外表现

时间:2016-05-18 14:05:33

标签: php jquery html ajax

所以我有一个AJAX进程,它从HTML表单中的select选项提交变量,然后从中构建另一个选择菜单。基本上应该发生的是,例如,如果用户选择2月份,发送到PHP脚本的变量将为“02”,然后新的下拉框将有29个选项。问题在于,尽管我的剧本告诉我02已被发送,但日子一切都没有了。这些日子似乎与月份无关,然而尽管日子不对,但它们总是错误的。例如,虽然2月份不会打印29天,但无论是30天还是8000天,它都会始终打印“30天”选项。

这是我的javascript文件:

$(document).ready(function() {      //Makes sure page is loaded.

    /*  === LINKS ===
        Controls the links (site behaviour) via JQuery (as 
        defined by the Assignment CRA).
    */
    $('#home').html('<a href="./index.php">Home</a>');
    $('#signup').html('<a href="./signup.php">Sign Up</a>');
    $('#activities').html('<a href="./activities.php">Upcoming Activities</a>');
    $('#feedback').html('<a href="./feedback.php">Student Feedback</a>');
    $('#contact').html('<a href="./contact.php">Contact</a>');


    /*  === STAFF INFORMATION TOGGLE ===
        Displays or hides information about 
        staff members on click.
    */
    /*
    $('#spongeimage').click(function() {  
        $("#spongeinfo").slideToggle("slow");

    });*/

    $(".staff").click(function() {  
    //alert("ALERT");
        $(".staffinfo", this).slideToggle("slow");

    });


    $('#year').change(function() {  
        $("#month").slideToggle("slow");

    });

    $('#month').change(function() {
        var month_var = $("#month").val();
        var dataString = 'name1='+ month_var;
        //Start

    $.ajax({
        type: "POST",
        url: "./javascript/month_post.php",
        data: dataString,
        dataType: "text",
        success: function(data) {
            //alert(dataString);
            $("#day").html(data);
            $("#okay").html(dataString);
        }
    });



        //End

        $("#day").slideToggle("slow");

    });




    /*  === 'FIRST NAME' FORM FOCUS
        Focuses the cursor on the 'first name' 
        field when the Student Feedback page 
        is loaded.
    */
    $('input[name="firstname"]').focus();



    /*  === HOVER EFFECTS ===
        The effects that occur after hovering over 
        an item on the link bar (1.0) and one of 
        the two buttons on the Student Feedback 
        page (1.1).
    */

    //(1.0)
    $("li a").hover(function(){
            $(this).css({"color": "#000000", "background-color": "#5590ff"});
            }, function(){
            $(this).css({"color": "#333", "background-color": "#FFFFFF"});
    });

    //(1.1)
    $("formbutton").hover(function(){
            $(this).css({"color": "#000000", "background-color": "#5590ff"});
            }, function(){
            $(this).css({"color": "#333", "background-color": "#FFFFFF"});
    });

});

这是我的PHP文件:

<?php
echo $name;

if(isset($_POST["name1"]))
{


$name = $_POST["name1"];
//echo $name;
// Here, you can also perform some database query operations with above values.
//echo "Welcome ". $name ."!"; // Success Message
$counter = 1;

//echo "<select name='gday'>";

if($name == 02)
{
    $monthdays = 29;
}
if($name == 06 || $name == 09 || $name == 04 || $name == 11)
{
    $monthdays = 30;
}
else
{
    $monthdays = 31;
}


while ($counter <= $monthdays)
{

    echo "<option value='hi'>".$counter."</option>";

    $counter++;
}
//echo "</select>";


}


?>

这是我的HTML文件:

<!DOCTYPE html>
<html>
<head>
    <title>KIT411 - Homepage</title>
    <link rel="stylesheet" href="./CSS/style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="./javascript/script.js"></script>
</head>
<body>

    <!--    TITLES      -->
    <div class="titles">
    &zwnj;  <!-- Blank character, to ensure the titles div extends to the top of the page. -->
        <h1 class="title">KIT411</h1>   <!-- Unit name. -->
        <h2 class="subtitle">Sign Up</h2>       <!-- Page name. -->
    </div>
    <div class="right">

    <?php
    include('login.php');
    ?>

    </div>

    <!--    PAGE LINKS
            Links are controlled by JQuery, which is why 
            there are no <a></a> tags.  -->
    <?php
    include('PHP/linkbar.php');
    ?>

    <div class="center">
        <div class="inline">
        <?php
        include('./db-connection/register.php');
        ?>
            <div id="okay">TEST </div>
            <form method="post" action="">
                Username:<br>
                <input type="text" name="usr" value="<?php echo $user; ?>"><br>
                Password:<br>
                <input type="password" name="pass" value="<?php echo $password; ?>"><br>
                Retype Password:<br>
                <input type="password" name="pass2" value="<?php echo $passwordTwo; ?>"><br>
                Name:<br>
                <input type="text" name="name" value="<?php echo $name; ?>"><br>
                DOB:<br>

                <select id="day" name="dobDay">
                <?php
                $counter = 1;


                while($counter <= 31)
                {
                ?>
                <option value="<?php echo $counter; ?>"><?php echo $counter; ?></option>
                <?php
                $counter++;
                }
                ?>
                </select>


                <select id="month" name="dobMonth">
                <option value="01">January</option>
                <option value="02">February</option>
                <option value="03">March</option>
                <option value="04">April</option>
                <option value="05">May</option>
                <option value="06">June</option>
                <option value="07">July</option>
                <option value="08">August</option>
                <option value="09">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
                </select>

                <select id="year" name="dobYear">
                <?php
                $counter = 1100;
                while($counter <= 2016)
                {
                ?>
                <option class="yearClass" value="<?php echo $counter; ?>"><?php echo $counter; ?></option>
                <?php
                $counter++;
                }
                ?>
                </select>

                <br>
                Email:<br>
                <input type="text" name="email" value="<?php echo $email; ?>"><br>

                <input type="submit" name="submit" value="Sign Up">
            </form>

        </div>
    </div>


<?php
    include('PHP/footer.php');
    ?>  
</body>
</html>

如果我的代码中有任何脏话,那么非常感谢任何帮助,抱歉。有时我会在测试时做一些有趣的事情而忘记删除它们。无论如何,任何帮助都是进步。

1 个答案:

答案 0 :(得分:0)

问题在于PHP代码,当您尝试设置天数时。您有一个if和一个if ... else,但行为不符合预期。

如果

,你首先要这样做
if($name == 02)
{
    $monthdays = 29;
}

在这之后:

if($name == 06 || $name == 09 || $name == 04 || $name == 11)
{
    $monthdays = 30;
}
else
{
    $monthdays = 31;
}

最后一个将天数设置为30或31,因为它只有两个选择。在这里,您可以使用elseif控制结构。 http://php.net/manual/en/control-structures.elseif.php

if ($name == 02)
{
    $monthdays = 29;
} 
else if ($name == 06 || $name == 09 || $name == 04 || $name == 11)
{
    $monthdays = 30;
}
else
{
    $monthdays = 31;
}