1

Hi guys I am trying to use jquery and ajax to dynamically create selection and than pass the selected item from that selection to $_POST. The selection listed is being created without the need of refreshing the page but the variable $_POST['subCat'] is not being posted it seems. I tried various changes bu to no avail. Below is the code, maybe someone can give me a hand.

FORMS:

<!-- 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

Buffer page (Part 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);

Buffer page (part 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 -->
  • Thanks, I saw that post but I thought it was not my case as $subCat=sanitize($_POST['subCat']); was saved in a wrong if statement – marius mifsud Aug 25 '17 at 09:11

0 Answers0