Pregunta

I am trying to figure out why this code does not pass the value of the hidden input field on submit.

The code is taken out of a WordPress template page, i have stripped it from all characteristic WP tags in order to paste it here. The field id is "cutoff".

<?php

$cutoffdate = $_POST['cutoff'];
$school = empty($_COOKIE['school']) ? null : $_COOKIE['school'];
$department = empty($_COOKIE['department']) ? null :  $_COOKIE['department'];

if ( ! empty($_POST['school']) ) {
setcookie('school', $_POST['school'], time() + 3600 * 24 * 30, '/');
setcookie('department', @$_POST['department'], time() + 3600 * 24 * 30, '/');

header('Location: '. $_SERVER['REQUEST_URI']);
exit;
}


if ( ! $school) {
    /*some code here */ }

elseif ($cutoffdate == "1") {
?> 
<div>
<form>
<select id="school" name="school">
<option value="97" >Art Center College of Design</option>
<option value="96" >Biola University</option>
<option value="4"  selected='selected'>California State University, Bakersfield</option>
<option value="22" >California State University, Pomona</option>
<option value="27" >California State University, San Marcos</option>
<option value="31" >Occidental College</option>
<option value="32" >Seminole State College of Florida</option>
<option value="89" >University of California, Davis</option>
<option value="90" >University of California, San Diego</option>
<option value="43" >University of Central Florida</option>
<option value="48" >University of Phoenix</option>
<option value="91" >Woodbury University</option>
</select>

        <input type="hidden" id="cutoff" name="cutoff" value="" />
        <br>
   </form>
    <?php endif; ?>



    </div>
    <script type="text/javascript">

jQuery( 

function ($) {

    $('#school').change( function (e) {

    var unihashtable = new Array();
        var unicutofftable = new Array();

        unihashtable[4] = 'California State University, Bakersfield';
        unihashtable[22] = 'California State University, Pomona';
        unihashtable[27] = 'California State University, San Marcos';
        unihashtable[31] = 'Occidental College';
        unihashtable[32] = 'Seminole State College of Florida';
        unihashtable[43] = 'University of Central Florida';
        unihashtable[48] = 'University of Phoenix';
        unihashtable[89] = 'University of California, Davis';
        unihashtable[90] = 'University of California, San Diego';
        unihashtable[91] = 'Woodbury University';
        unihashtable[96] = 'Biola University';

        unicutofftable[4] = new Date(2014, 0, 11, 21, 0, 0);
        unicutofftable[22] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[27] = new Date(2014, 4, 14, 21, 0, 0);
        unicutofftable[31] = new Date(2014, 4, 16, 21, 0, 0);
        unicutofftable[32] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[43] = new Date(2014, 3, 28, 21, 0, 0);
        unicutofftable[48] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[89] = new Date(2014, 5, 10, 21, 0, 0);
        unicutofftable[90] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[91] = new Date(2014, 4, 8, 21, 0, 0);
        unicutofftable[96] = new Date(2014, 4, 21, 21, 0, 0);


        for (var i in unihashtable) {


            if (i == $('#school').val()) {

                datecheck = new Date();
                if (datecheck > unicutofftable[i]) {
                    $("[name='cutoff']").val(1);
                    alert("Some Alert"); 
                    this.form.submit();


                    } else { this.form.submit(); }

            }

        }






    }   );


});
</script>
<?php }

else {
?>


<div>
<form>
<select id="school" name="school">
<option value="97" >Art Center College of Design</option>
<option value="96" >Biola University</option>
<option value="4"  selected='selected'>California State University, Bakersfield</option>
<option value="22" >California State University, Pomona</option>
<option value="27" >California State University, San Marcos</option>
<option value="31" >Occidental College</option>
<option value="32" >Seminole State College of Florida</option>
<option value="89" >University of California, Davis</option>
<option value="90" >University of California, San Diego</option>
<option value="43" >University of Central Florida</option>
<option value="48" >University of Phoenix</option>
<option value="91" >Woodbury University</option>
</select>
        <input type="hidden" id="cutoff" name="cutoff" value="" />
        <br>

    </form>
    <?php endif; ?>




</div>

<script type="text/javascript">

jQuery( 

function ($) {

    $('#school').change( function (e) {

    var unihashtable = new Array();
        var unicutofftable = new Array();

        unihashtable[4] = 'California State University, Bakersfield';
        unihashtable[22] = 'California State University, Pomona';
        unihashtable[27] = 'California State University, San Marcos';
        unihashtable[31] = 'Occidental College';
        unihashtable[32] = 'Seminole State College of Florida';
        unihashtable[43] = 'University of Central Florida';
        unihashtable[48] = 'University of Phoenix';
        unihashtable[89] = 'University of California, Davis';
        unihashtable[90] = 'University of California, San Diego';
        unihashtable[91] = 'Woodbury University';
        unihashtable[96] = 'Biola University';

        unicutofftable[4] = new Date(2014, 0, 11, 21, 0, 0);
        unicutofftable[22] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[27] = new Date(2014, 4, 14, 21, 0, 0);
        unicutofftable[31] = new Date(2014, 4, 16, 21, 0, 0);
        unicutofftable[32] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[43] = new Date(2014, 3, 28, 21, 0, 0);
        unicutofftable[48] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[89] = new Date(2014, 5, 10, 21, 0, 0);
        unicutofftable[90] = new Date(2014, 5, 11, 21, 0, 0);
        unicutofftable[91] = new Date(2014, 4, 8, 21, 0, 0);
        unicutofftable[96] = new Date(2014, 4, 21, 21, 0, 0);


        for (var i in unihashtable) {


            if (i == $('#school').val()) {

                datecheck = new Date();
                if (datecheck > unicutofftable[i]) {
                    $("[name='cutoff']").val(1);
                    alert("Some Alert"); 

                    this.form.submit();


                    } else { this.form.submit(); }

            }

        }






    }   );


});
</script>

<?php
};

I am probably missing something elementary or obvious, but i just cannot see why this doesn't work...

¿Fue útil?

Solución

Right, i figured out what was wrong:

if ( ! empty($_POST['school']) ) {
setcookie('school', $_POST['school'], time() + 3600 * 24 * 30, '/');
setcookie('department', @$_POST['department'], time() + 3600 * 24 * 30, '/');

header('Location: '. $_SERVER['REQUEST_URI']);
exit;
}

The header function here was redirecting and killing the $_POST values. Although i cannot say 100% that it is the header itself and not te exit that was messing it all up. I would love if someone who knows more could explain maybe...

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top