1

I would like to include the following php variable ($crs_sub2) inside my following javascript statement:

<script>
    document.getElementById("checkbox1").addEventListener('change', function() {
        var crs_category1 =  document.getElementById("checkbox1").checked;
        if (crs_category1 === true){
          document.getElementById('checkboxLoading').innerHTML = ''; 

        } else {
window.open ('searchPage.php?user_query=$crs_sub2&search=search','_self',false);
</script>

my issue is as follow: the variable crs_sub2 refers to the following php statement:

$search_query2 = $_GET['crs_category'];

$get_cats = "select DISTINCT cat_subc1,cat_subc2,cat_subc3,cat_subc4,cat_subc5,cat_subc6,cat_subc7,cat_subc8,cat_subc9,cat_subc10,cat_subc11,cat_subc12,cat_subc13,cat_subc14,cat_subc15,cat_subc16,cat_subc17,cat_subc18 from categories where (cat_title like '%$search_query2%')";

$run_cats = mysqli_query($con, $get_cats);

while ($row_cats=mysqli_fetch_array($run_cats)) {
    $crs_sub2 = $row_cats['cat_subc2'];
}

2 Answers 2

3

use like this

window.open ('searchPage.php?user_query=<?php echo $crs_sub2; ?>&search=search','_self',false);
Sign up to request clarification or add additional context in comments.

Comments

0

You may try as follows (if both javascript and php are on the same page)

     $search_query2 = $_GET['crs_category'];

            $get_cats = "select DISTINCT cat_subc1,cat_subc2,cat_subc3,cat_subc4,cat_subc5,cat_subc6,cat_subc7,cat_subc8,cat_subc9,cat_subc10,cat_subc11,cat_subc12,cat_subc13,cat_subc14,cat_subc15,cat_subc16,cat_subc17,cat_subc18 from categories where (cat_title like '%$search_query2%')";


     $run_cats = mysqli_query($con, $get_cats);

            while ($row_cats=mysqli_fetch_array($run_cats)){


                $crs_sub2 = $row_cats['cat_subc2'];
                ?>
<script>
    document.getElementById("checkbox1").addEventListener('change', function() {
            var crs_category1 =  document.getElementById("checkbox1").checked;
            if (crs_category1 === true){


            document.getElementById('checkboxLoading').innerHTML = ' '; 

                } else {
        window.open ('searchPage.php?user_query=<?php echo $crs_sub2; ?>&search=search','_self',false);}
</script>
        <?php
        }

Hope this helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.