<a class="clickme">Optional</a>
<a class="submit">Submit me</a>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var the_action = function(type) {
    switch(type) {
        case 'a':
            console.log('Case A');
            break;
         case 'b':
            console.log('Case B');
            break;
    }
};

$('.clickme').click(function() { 
    console.log('Clicked');
    $(this).data('clicked', true);
});

$('.submit').click(function() {
    if($('.clickme').data('clicked') == true) {
        $('.clickme').data('clicked', false);
        the_action('a');
    } else {
        the_action('b');
    }
});
</script>

<?php
    $arrayName = array('ctaIva', 'ctaComb', 'ctaRet', 'ctaReti', 'ctaCaj');

foreach ($arrayName as $key => $value) {
    echo '<br>'.$value;
}

function prueba(){
    function blah(&$var) {
        $tipoPol;
        $var="INSERT INTO tabla (id) VALUES('$tipoPol')";
    }

    $tipoPol = "006";
    blah($query);
    echo '<br><br>'.$query;

    $tipoPol = "007";
    blah($query);

    echo '<br><br>'.$query;
}



function prueba2(){
    global $tipoPol;
    function blah() {
        global $tipoPol;
        return "INSERT INTO tabla (id) VALUES('$tipoPol')";
    }

    $tipoPol = "006";
    echo '<br><br>'.blah();

    $tipoPol = "007";
    echo '<br><br>'.blah();
}
/*
$tipoPol = 1;
function blah()
{
    //$var =& $GLOBALS["tipoPol"];
    $tipoPol=& $GLOBALS["tipoPol"];
    $var="INSERT INTO tabla (id) VALUES('$tipoPol')";
}

blah();*/

prueba2();
?>