I am trying to pass more then 1 php variable in parameter in a onclick.
<button type="button" class="btn btn-dark bmd-btn-fab bmd-btn-fab-sm"
onclick="<?php echo 'analyse(\'' . $name . '\', \''. $type .'\')'; ?>">
<img src="../assets/add.png" />
</button>
For only 1 parameter it worked with this :
onclick=<?php echo "analyse('$name')"; ?>"
and this:
onclick="<?php echo 'analyse(\'' . $name . '\')'; ?>"
I would put this as a comment but the placeholder text in comments says to not put answers in comments...
First, analyse
has to be a JS function, not a PHP function.. Also, you don't need to put analyse
inside of the <?php
code block..
<button onclick="analyse('<?php echo $name ?>', '<?php echo $type ?>')">
Full demo PHP file:
<?php
$name = "John Smith";
$type = "Best Type"
?>
<div>
<button onclick="analyze('<?php echo $name ?>', '<?php echo $type ?>')">Show Name</button>
<p id="results"></p>
</div>
<script>
function analyze(name, type) {
document.getElementById("results").innerHTML = "<b>NAME:</b> " + name + " <b>TYPE:</b> " + type;
}
</script>
To elaborate, just in case you wanted to use a PHP function to return a name or type, you would use it like so:
<?php
$name = "John Smith";
$type = "Best Type";
function get_Name($nameToGet) {
return $nameToGet;
}
function get_Type($typeToGet) {
return $typeToGet;
}
?>
<div>
<button onclick="analyze('<?php echo get_Name($name) ?>', '<?php echo get_Type($type) ?>')">Show Name</button>
<p id="results"></p>
</div>
<script>
function analyze(name, type) {
document.getElementById("results").innerHTML = "<b>NAME:</b> " + name + " <b>TYPE:</b> " + type;
}
</script>
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.