PHP function doesn't run the second time [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
I have a problem with php.
I was trying to make a button that changes its text into random numbers every time I click, but it only worked for once.
My Code:
<!DOCTYPE html>
<html>
<body>
<?php
function yeet(){
return rand(1, 100);
}
?>
<script type="text/javascript">
function yeeb(){
let randNum = <?php echo yeet();?>;
document.getElementById("co").innerHTML = randNum.toString();
}
</script>
<button onclick = "yeeb()" id="co">click</button>
</body>
</html>
Any help would be grateful.

Why are you doing it using php?
You can make that function by javascript only
function yeeb(){
let randNum = Math.floor(Math.random() * 100) + 1;
document.getElementById("co").innerHTML = randNum.toString();
}

Related

problem with console command, the content can not print anything?

I am newbie with javascript and I wrote a very simple program to display something using console command like this
function dogYears(dogName, age) {
var years = age * 7;
console.log(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Exercise2</title>
</head>
<body>
</body>
</html>
As you can see, I hope that I made the function and then, use var to assigned the value myDog to Fido, and then I want to use the function dogYears to print by the command console.log.
But when I ran the file .html (I saved it by .html), it did not display anything.
What error did I get in this case ? Could you please help me in this one ? Thank you very much for your help.
To display your result in browser's console just add your JavaScript logic inside script tag in your html file before closing body tag or create separate JavaScript file and import it using script tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Exercise2</title>
</head>
<body>
<script>
function dogYears(dogName, age) {
var years = age * 7;
console.log(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
</script>
</body>
</html>
The problem is that you are not viewing the console. The console is not on the webpage, but instead on a separate tab. Press F12 on the keyboard, or press Ctrl+Shift+i and then click on the console. There is nothing wrong with the code. But if you want a more protected function, you can use this JavaScript code.
<script>
function dogYears(dogName, age) {
if(typeof(dogName)==="string"&&typeof(age)==="number"){
var years = age * 7;
console.log(dogName + " is " + years.toString() + " years old");
}else{
console.log("Error you want to display");
}
}
</script>
If you want to display information on the browser, typed document.write instead of console.log
<script>
function dogYears(dogName, age) {
var years = age * 7;
document.write(dogName + " is " + years + " years old");
}
var myDog = "Fido";
dogYears(myDog, 4);
</script>

innerHTML not printing. (Javascript) [duplicate]

This question already has answers here:
how do I set input from user to the value of an input?
(2 answers)
Closed 6 years ago.
I am not exactly new at coding,
But I am definitely an amateur (especially in Javascript), and I have just started a new project for personal purpose.
I can not seem to get my Javascript to print out my answer, and I do not want to move on with the project until I figure it out, because it is going to have more complicated calculations as I work on it.
My code is as below:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p>Enter Your Macros</p>
<p>Fats</p>
<input type="number" name="fats" id="fatInput"/><br>
<button onclick="myFunction()">Calculate</button><br>
<p id="todaysFat"></p>
<script>
function myFunction() {
var f = document.getElementById("fatInput").innerHTML;
document.getElementById("todaysFat").innerHTML = f;
}
</script>
</body>
</html>
Thank you for all of the help
try;
function myFunction() {
var f = document.getElementById("fatInput").value;
document.getElementById("todaysFat").innerHTML = f;
}
I don't think it's the innerHTML your want but the value of the INPUT.
So that's .value not innerHTML.
eg..
var f = document.getElementById("fatInput").value;
Change the innerhtml to value in the first line.
See the below code:
function myFunction() {
var f = document.getElementById("fatInput").value;
document.getElementById("todaysFat").innerHTML = f;
}
<p>Enter Your Macros</p>
<p>Fats</p>
<input type="number" name="fats" id="fatInput" />
<br>
<button onclick="myFunction()">Calculate</button>
<br>
<p id="todaysFat"></p>

How to decode javascript elements from an array [duplicate]

This question already has answers here:
De-obfuscate Javascript code to make it readable again [duplicate]
(5 answers)
Closed 7 years ago.
I try to work with javascript from Chrome extension, and have some problems.
Big parts of code including in encode array
var _0x6790 = ["\x44\x4F\x4D\x53\x75\x62\x74\x72\x65\x65\x4D\x6F\x64\x69\x66\x69\x65\x64", "\x67\x65\x74", "\x3A\x76\x69\x73\x69\x62\x6C\x65", "\x69\x73", "\x24\x61\x70\x70\x6C\x79\x46\x6F\x72\x4C\x6F\x6F\x74\x42\x75\x74\x74\x6F\x6E", "\x65\x6E\x61\x62\x6C\x65\x64", "\x61\x75\x74\x6F\x63\x6C\x69\x63\x6B\x69\x6E\x67", "\x73\x65\x74\x74\x69\x6E\x67\x73", "\x6D\x61\x67\x69\x63", "\x63\x6C\x69\x63\x6B", "\x5F\x62\x69\x64\x49\x6E\x74\x65\x72\x6E\x61\x6C\x44\x65\x6C\x61\x79", "\x61\x75\x74\x6F\x62\x69\x64\x64\x69\x6E\x67", "\x23\x67\x6F\x6C\x64\x73\x6C\x69\x64\x65\x72", "\x73\x65\x74\x42\x69\x64", "\x6F\x6E", "\x5F\x69\x6E\x73\x74\x61\x6E\x63\x65"];
and is used like this:
$(document)[_0x6790[14]](_0x6790[0], function() {
How can I decode the values in the array?
this is a worked demo that can help you:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<script>
function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2) str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
document.getElementById('result').innerHTML=str;
}
</script>
</head>
<body>
<h1><u>Hex Decoder</u></h1><br>
<h3><u>Hex code</u></h3>
<textarea id="hexcode" rows="4" cols="50"></textarea><br>
<button type="button" onclick="hex2a(document.getElementById('hexcode').value)">decode</button><br>
<h3><u>Result</u></h3>
<textarea id="result" rows="4" cols="50"></textarea>
</body>
</html>

Need to pass a variable from JS to php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I want to create a title description for a chess eco code:
This is the lookup array in php named eco_codes:
<?php
$eco_tmp = array(
"A00" => "Uncommon Opening 1.g4, a3, h3, etc.",
"A01" => "Nimzovich-Larsen Attack 1.b3",
"A02" => "Bird's Opening 1.f4",
"A03" => "Bird's Opening 1.f4 d5")
?>
I have the lookup code (ex. GameECO = "A02") like this:
<span id="GameECO"></span>
What I want is this:
<?php $key = array_search($val['GameECO'], $eco_codes);?>
<span id="GameECO" title = "<?php echo $key.$val['ECO']></span>
How can I proceed? Is php even required for this? Maybe js alone would do it?
my page is http://communitychessclub.com/basic.php?game=5312
Assuming you have the code in some variable, have a js file (ecocodes.js) containing
var ecoCodes = {
"A00":"Uncommon Opening 1.g4, a3, h3, etc.",
"A01":"Nimzovich-Larsen Attack 1.b3",
"A02":"Bird's Opening 1.f4",
"A03":"Bird's Opening 1.f4 d5"
} // no comma after the last one
then you can use
<!DOCTYPE html>
<html>
<head>
<title>Chess</title>
<script src="ecocodes.js"></script>
<script>
window.onload=function() {
var eco = "A02";
document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
}
</script>
</head>
<body>
ECO: <span id="GameECO"></span>
</body>
</html>
It works like this:
var ecoCodes = {
"A00":"Uncommon Opening 1.g4, a3, h3, etc.",
"A01":"Nimzovich-Larsen Attack 1.b3",
"A02":"Bird's Opening 1.f4",
"A03":"Bird's Opening 1.f4 d5"
} // no comma after the last one
window.onload=function() {
var eco = "A02";
document.getElementById("GameECO").innerHTML=eco + ":"+ecoCodes[eco];
}
ECO: <span id="GameECO"></span>

How to create a function that generates a random 3 letter sequence? [duplicate]

This question already has answers here:
Generate random string/characters in JavaScript
(93 answers)
Closed 9 years ago.
I need to connect the webpage with a 'random.js' file which I hope I have already correctly done, as well as creating a function named 'GenerateSequence' that generates 3 random letters. I have inserted 3 'RandomChar' into the body but am not entirely sure where exactly they go. In the end the page needs to look like this
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<title>Random Letter Sequence</title>
<script type="text/javascript" src="random.js"></script>
<script type="text/javascript">
function GenerateSequence()
{
}
</script>
</head>
<body>
<h2>Generate 3-Letter Sequence</h2>
sequence = RandomChar('abcdefghijklmnopqrstuvwxyz') +
RandomChar('abcdefghijklmnopqrstuvwxyz') +
RandomChar('abcdefghijklmnopqrstuvwxyz');
<hr>
<h3>You Random 3-Letter Sequence<h3>
<div id="outputDiv"></div>
</body>
</html>
function GenerateSequence(){
var result = "";
var alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
for(var i=0;i<3;i++){
result += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
}
return result;
}
window.onload = function(){
document.getElementById('goButton').onclick=function() {
document.getElementById('outputDiv').innerHTML = GenerateSequence();
}
}

Categories