PHP AJAX Jquery Form不发送$ _POST变量 - 注意:未定义变量:subcat

时间:2017-08-25 08:49:21

标签: php jquery ajax

大家好我正在尝试使用jquery和ajax动态创建选择,而不是将所选项目从该选择传递到$ _POST。列出的选择是在不需要刷新页面的情况下创建的,但似乎没有发布变量$ _POST [' subCat']。我尝试了各种改变无济于事。下面是代码,也许有人可以帮我。

形式:

<!-- Product Type -->
        <div class="form-group col-md-3">
            <label for="producttype">Product Type*:</label>
            <select class="form-control" id="producttype" name="producttype">
                <option value="<?= ((isset($_POST['producttype'])&& $_POST['producttype'] == '')?' selected':'') ; ?>"></option>
                <?php while($producttype= mysqli_fetch_assoc($productQuery)):   ?>
                    <option value="<?= $producttype['ProductTypeID']; ?>"<?= ((isset($_POST['producttype'])&&$_POST['producttype']== $producttype['ProductTypeID'])?' selected':'') ; ?>><?= $producttype['ProductType']; ?></option>
                <?php endwhile;  ?> 
            </select>
        </div>
        <!-- Product Sub-Type -->
        <div class="form-group col-md-3">
            <label for="subCat">Product Sub-Type*:</label>
            <select id="subCat" name="subCat" class="form-control">
            <option value="<?= ((isset($_POST['subCat'])&& $_POST['subCat'] == '')?' selected':'') ; ?>"></option>  
            </select>
        </div>

的jQuery / AJAX:

function get_sub_cat(){
    var producttypeID = jQuery('#producttype').val();
    jQuery.ajax({
        url:'/ares/aresStore/admin/parsers/productSubCategories.php',
        type: 'POST',
        data: {producttypeID:producttypeID},
        success: function(data){
            jQuery('#subCat').html(data);//#subCat is ID of form and data is the data which is sent if succesful
        },
            error: function(){alert("ERROR: With Sub-Categories (productSubCategories.php/Function:get_sub_cat)")},
    });
}
jQuery('select[name="producttype"]').change(get_sub_cat);//when the selection of producttype is selected it triggers function get_sub_cat

缓冲页面(第1部分):

<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/ares/aresStore/core/init.php'; //this path needs to be changed of ares folder is deleted
$producttypeID =(int)$_POST['producttypeID'];
$sql2="SELECT * FROM producttype WHERE ProductTypeID='$producttypeID'";
$ptresults= $db->query($sql2);
while($producttype = mysqli_fetch_assoc($ptresults)):
    $ProductType=$producttype['ProductType'];
endwhile;

$subcatColumnName=$ProductType.'Type'; //ex BlurayType
$subcatTableName=strtolower($subcatColumnName); //ex bluraytype
$joinTableName=$subcatTableName.'product'; //ex bluraytypeproduct
$joinColumnName=$subcatColumnName.'ID'; //ex BlurayTypeID
//Below SQL reference: http://www.sql-ex.com/help/select5.php
$sql3="SELECT * FROM $subcatTableName ORDER BY $subcatColumnName";
$subcatresults= $db->query($sql3);

缓冲页面(第2部分)

    ob_start(); 
?>
<option value="<?= ((isset($_POST['subCat'])&& $_POST['subCat'] == '')?' selected':'') ; ?>"></option>  
<?php while($subcattypes = mysqli_fetch_assoc($subcatresults)): ?>
    <option value="<?= $subcattypes[$subcatColumnName]; ?>"<?= ((isset($_POST['subCat'])&& $_POST['subCat']== $subcattypes[$subcatColumnName])?' selected':'') ; ?>><?= $subcattypes[$subcatColumnName]; ?></option> 

<?php endwhile ;?>
<?php echo ob_get_clean();?> <!--clears above buffer -->

0 个答案:

没有答案