-2

So I have written a basic page which has buttons:

<button type="button" onclick="minus()"> </button>

with the script:

function minus() {
  if (<?php echo $number ?> > 1){
  var num = <?php echo $number; ?> - 1;
  var number=num;
  window.location.href = "page2.php?w1=" + number;}
};

So this just subtracts 1 off of a PHP variable and it works perfectly well in Chrome, IE and Safari, but in Firefox it doesn't work at all. Is this a security thing is this code particularly insecure?.

Is there any way in which I can write a button to change this PHP variable without using JavaScript as to make it work on Chrome, IE, Safari + Firefox?

Many thanks

user3412782
  • 561
  • 1
  • 4
  • 9
  • 1
    It is not Java, it is JavaScript method. You need to write valid JavaScript code and then it will work. Check if FF indicates that there are JavaScript errors in your page – Germann Arlington Aug 05 '14 at 09:45
  • 1
    also dont use term `java` for `javascript` – Santhosh Aug 05 '14 at 09:46
  • 2
    you can't use javascript if it is not enabled. You cannot force it to work, it would be a security issue. You would need to have a fallback function for when javascript isn't available, which should be ye old postbacks – musefan Aug 05 '14 at 09:46
  • Sorry, honest mistake I'm just trying to learn. Could you advise on why the JavaSript doesn't seem to run in firefox? – user3412782 Aug 05 '14 at 09:49
  • @user3412782: What is your console error? Also, what does your source code look like (after the PHP has been rendered... View Source) – musefan Aug 05 '14 at 09:51
  • Haha: *`"is this code particularly insecure?"`*... "Does my bum look fat in this function? – musefan Aug 05 '14 at 09:52
  • Ok thank you all for pointing out Java and Javascript aren't the same, it is noted :). The Javascript works flawlessly in Chrome, IE and Safari thus I don't believe it to be an issue directly with the script. I believe it is because Javascript is disabled, how can I write a back up function for case where Javascript is disabled (or what should I search to find out please?)? – user3412782 Aug 05 '14 at 09:53
  • No console error, it just doesn't do anything – user3412782 Aug 05 '14 at 09:53
  • @user3412782: Does any javascript work for you in firefox? Just test a simple alert. Also, [maybe this is the issue](http://stackoverflow.com/questions/275092/windows-location-href-not-working-on-firefox3)? – musefan Aug 05 '14 at 09:58
  • Yes some does, I just tested the confirm command and that worked fine. But I have just noticed some of my CSS 'hover' styles aren't working in FF when they're surrounded by the – user3412782 Aug 05 '14 at 10:34
  • I've found FF requires the div to be positioned relatively and not absolutely for the button to work. I've shown it below :) thanks for you help. – user3412782 Aug 05 '14 at 12:59

2 Answers2

1

PHP is a server-side language - JavaScript a client-side language; PHP is executed before Javascript, so by the time your Javascript function is initiated the PHP code is already gone.

Take a look at this: Using PHP in javascript's IF Statement.

It might help!

Community
  • 1
  • 1
Beejay
  • 183
  • 1
  • 3
  • 13
  • Thank you for your input :) it turns out the reason it wasn't working was FF requires that button div's are position:relative; otherwise it doesn't seem to work, but with that positioning it works now. – user3412782 Aug 05 '14 at 12:51
0

The solution to this issue (or at least a solution I have found to work) is for the positioning of the div which is the button to be set to relative (note I have only found FF to require this IE, Chrome and Safari don't seem to be affected by position:absolute). i.e.

<button type="button" onclick="someFunction()">
<div id="button"></div>
</button>

with the CSS being:

#button {
position:relative
}

and the hover styling applied to the button element instead of the div.

user3412782
  • 561
  • 1
  • 4
  • 9