Pergunta

First problem

I want to use variable don_settings[don_btn_act] (=checkbox) to define a button action.

IF don_settings[don_btn_act] IS on
THEN -> load checkout
ELSE -> page reload

don_settings[don_btn_act] is definded and it is modified well, so i can turn it on or off. The problem is that I should use it in javascript.

What I have so far:

function reloadpage(){
  <?php if(isset($don_config['don_btn_act']) && $don_config['don_btn_act'] =='on') { ?>
    window.location.href = 'index.php?route=dcheckout/checkout';
  <?php } else { ?>
    window.location.reload();
  <?php } ?>    
}

It is always executing the window.location.reload() function.

Second problem

in the same way i have the variable don_settings[min_amount]. I use it to define the minimum amount of input.
it is defined in php like the previous varible. But i should use it in javascript part of tpl file, too.

What I have so far:

function validation(){
    if(jQuery('#opton').val() == ''){
        alert("<?php echo $drop_empty_msg; ?>");
        return false;
    }
    else if(jQuery('#don_amount').val() == '' || jQuery('#don_amount').val() == '0'){
        alert("<?php echo $amount_empty; ?>");
        jQuery('#don_amount').focus();
        return false;
    }
    else{
        return true;
    }
}

i use

|| jQuery('#don_amount').val() <= "<?php $don_settings[min_amount]; ?>"

insteed of

|| jQuery('#don_amount').val() == '0'

but it returns false every time

Foi útil?

Solução

Sorry, I couldn't understand your problem or your intensions. Try correcting your misspelling, your grammar and please take care of beautiful formatted code!

Nonetheless i have some recommendations for writing maintainable code.

Your problem (#1)

If your browser is always executing the window.location.reload(), the problem does not refer to your javascript. It is your server side code which fails. Your following condition seems to be false:

<?php if(isset($don_config['don_btn_act']) && $don_config['don_btn_act'] =='on') { ?>

Don't mix your javascript and php up

First of all, compute your necessary values/conditions:
(Preferably into a seperated file)

<?php
  $condition = isset($don_config['don_btn_act']) && $don_config['don_btn_act'] =='on';
?>

Now, you can use it very comfortable and clearly:

function reloadpage() {
  var condition = '<?php echo $condition; ?>';

  if (condition)
    window.location.href = 'index.php?route=dcheckout/checkout';
  else
    window.location.reload();
}

There are lots of big advantages not to mess up your code.

Also be careful ...

... while injecting code through php to the markup/javascript. Don't offer potential attackers new security holes!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top