jQuery redirect to specific URL based on checkbox

2019-09-21 11:46发布

I have an multiple category on my page to filter search.

http://localhost/shop/category/produkunggulan/CT0002

Example I have this category:

Brand

$qBrandList = mysqli_query($con, "SELECT P.productid, P.brandid_fk, B.Brandid, B.brand_name FROM tb_product P, tb_brand B WHERE P.brandid_fk = B.brandid AND P.categoryid_fk LIKE '%" . $getCategoryID . "%' GROUP BY B.brandid ORDER BY B.brand_name ASC");
while($dBrandList = mysqli_fetch_array($qBrandList))
{
?>
    <div><label><input name="chkBrand[]" class="chkBrand" type="checkbox" value="<?php echo $dBrandList['brandid_fk']; ?>"/> <?php echo $dBrandList['brand_name']; ?></label></div>
<?php
}

Discount

<label><input type="checkbox" name="discount" value="Y"/> Diskon</label>

Now I want when user click the checkbox, it will be directly redirect to the url including the key of category (every click checkbox) and remove the value if uncheck.

Example I click EMORI then FOSSIL, so the URL should be:

http://localhost/shop/category/produkunggulan/CT0002?brand=BR0003,BR0010

enter image description here

1条回答
看我几分像从前
2楼-- · 2019-09-21 12:17
<label><input type="checkbox" name="discount" value="Y"/> Diskon</label>

I don't think you need to use a checkbox to redirect. Simply use input type 'submit'.

<label><input type="submit" name="discount" value="Diskon"/> 

or add onchange event to submit

<label><input type="checkbox" onChange="this.form.submit()" name="discount" value="Y"/> Diskon</label>

also use 'get' method in the tag to show your data at url.

One thing for sure your url wont look as you mentioned rather that it will look as

http://localhost/shop/category/produkunggulan/CT0002?chkBrand%5B%5D=BR0003& chkBrand%5B%5D=BR0010

Here %5B is for'[' and %5D is for ']'.

查看更多
登录 后发表回答