Passing PHP Variable in Onclick Function

passing php variable in onClick function

Variable parsing is only done in double quoted strings. You can use string concatenation or, what I find more readable, printf [docs]:

printf('<a href= "#" onClick="showDetails(\'%s\');">%s</a> ', $node, $insert);

The best way would be to not echo HTML at all, but to embed PHP in HTML:

<?php
$node = $name->item(0)->nodeValue;
$insert = "cubicle" . $node;
?>

<td>
<a href= "#" onClick="showDetails('<?php echo $node;?>');">
<?php echo $insert; ?> <br />
</a>
</td>

You have to think less about quotes and debugging your HTML is easier too.

Note: If $node is representing a number, you don't need quotations marks around the argument.

Pass PHP variable on OnClick method

gotta quote it if it's a string or else js will assume it's a variable

echo '<td><a href="#" onclick="delete(\''.$contact['idContact'].'\')"> Delete </a></td>';

note the escaped quotes on either side \'

a better way of passing variable from php to js is to json encode them, that will take care of quotes and things...

How to send php variable onclick of button to javascript function as parameter

You have forgot to add quote on '<?php echo $row->poll_question ?>'. Check updated code below

<h3 class="val" id="val" style="color:rgb(158, 158, 158);font-family: 
arial;margin-top: 2%;" ><?php echo $row->poll_question; ?></h3>
</td>

<td>

<a class="btn btn-lg btn-primary view_button" id="view_button"
style="margin-left: 18%;padding:10%;border-radius: 10px;"
onclick="fetch_val('<?php echo $row->poll_question ?>')">View Poll</a>

Can't pass php variables in parameter in a HTML onclick

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>

How to Pass PHP String Variable To JS From an Onclick Event

You need to enclose the string in quotes when passing to the JS function, as well as in PHP:

$ratelink = 'text string...';
echo '<div><span id="anything" onclick="updatePos(\''.$ratelink.'\')">Save</div>';

How to pass two PHP variables in HTML button onClick function?

The quotes are messed.

Change your code to this:

<button id='myBtn' onclick='showproduct("<?php echo $value['_id'];?>" , "<?php echo $sellerInfo[0]['City']; ?>")'>Pending</button>

Passing a PHP variable as a parameter using HTML onClick

Try this..

echo('<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt(\'' . $name . '\')" >Track It!</button></td>');

Note: you have to use your way if the input parameter is a numeric value..


You are printing HTML as,

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt(test)" >Track It!</button></td>

But because the function trackIt needs a string as the input parameter, you have to print this..

<td colspan="3" style="background-color:#005673; text-align:right; padding: 4px 0px;">
<button class="btnTrack" onclick="trackIt('test')" >Track It!</button></td>

As you are using ' as boundaries to define strings in PHP, you have to escape it using \' in order to make ' character part of the string.

Pass multiple php variables in onclick function

Your method parameters arn't escaped properly. You need single quotes around each parameter, not the whole group. Try the following.

->extras("onclick = helper_reschedule_in_reschedule('".$row->company_id."', '".$row->ppt."', '".$row->test."', '".$row->interview."')

Also you need to modify your helper_reschedule_in_reschedule method to accept the extra params.

Check your reschedule_in_reschedule method also, as it looks like you've got PHP mixed with your javascript.



Related Topics



Leave a reply



Submit