I have a weird question and i don't know what to search to find the answer for it
Can I give an html button's onclick function args? onclick="func('Wednesday')"
function func(a) {
if (a == "Wednesday") {
var day = a;
}
} else {
var day = "not Wednesday";
}
Given that this is possible, is there a way to take this var and send it to another file, preferably a php file?
sorry if this is a repeat, im newIf You are asking about what i have written you can do that by creating function:-
<html>
<body>
<button onclick="myFunction()">Click me</button>
<p id="demo"></p>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";
}
</script>
</body>
</html>
Your two questions are not related as you are may be thinking.
The first question where you want to do something like this onclick="func('Wednesday')"
is doable but perhaps not in the way you are imagining, I will do it in someway like this
HTML
<a id="my-button">Click Me</a>
Javascript
function func(a, e) {
if (a == "Wednesday") {
var day = a;
} else {
var day = "not Wednesday";
}
}
var btn = document.getElementById("my-button")
btn.addEventListener("click", func.bind(null, "Wednesday"))
Read more about this way of binding callbacks here https://www.w3.org/wiki/The_principles_of_unobtrusive_JavaScript
Also I used bind
function of Function
to attach arguments with my callback which will be passed to my function whenever callback will be called
Now the questions about passing props from javascript to PHP. Well, think about the flow of server to client. Your PHP sends the javascript to browser as plain text to browser which gets interpreted at user machine, which means when your javascript begins to run PHP was no more in context hence you can't simply pass vars from JS to PHP since one of them runs on server and other on client.
To pass the props from client to server you will need to create AJAX request
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.