I have a problem with calling function "adding" inside "a" tag in eho of PHP script. After click in button function is not calling. I have not any idea. How to solve it?
<?php
require_once 'connect.php';
$statement=$db->query('SELECT * FROM Items');
foreach($statement as $row)
{
$item="item_".$row[0];
$view="view".$row[0];
$image=$row[1];
$name=$row[2];
$price=$row[3];
$plus="+";
$minus="-";
echo "<tr>
<td class='cart_product'>
<a href=''><img src='images/cart/$image' alt=''></a>
</td>
<td class='cart_description'>
<h4><a href=''>$name</a></h4>
</td>
<td class='cart_price'>
<p>$$price</p>
</td>
<td class='cart_quantity'>
<div class='cart_quantity_button'>
<a class='cart_quantity_up' href='' onclick='adding($plus, $item);'> + </a>
<input class='cart_quantity_input' type='text' name=$item id=$item value='0' autocomplete='off' size='2'>
<a class='cart_quantity_down' href='javascript:adding($minus, $item); sumitem($item, $price, $view);'> - </a>
</div>
</td>
<td class='cart_total'>
<p style='width: 5em' class='cart_total_price' id=$view >0ドル</p>
</td>
<td class='cart_delete'>
<a class='cart_quantity_delete' href='javascript:reset($item, $view)'><i class='fa fa-times'></i></a>
</td>
</tr>";
}
?>
asked Jul 28, 2018 at 22:35
Krzysztof Piórkowski
34 bronze badges
-
How do you know the JavaScript function isn’t invoking? Are you clicking the ‘+’ or on the parent div?Adam Brinded– Adam Brinded2018年07月28日 23:58:09 +00:00Commented Jul 28, 2018 at 23:58
1 Answer 1
You can call JavaScript function and send variables to it like this:
<script>
function myFunc(var1){
alert(var1);
}
</script>
<?php
$var1= "\"test string\"";
echo "<a href='' onclick='myFunc($var1);'>Link to Click</a>";
?>
your variables sending is wrong.
Sign up to request clarification or add additional context in comments.
5 Comments
Adam Brinded
He is echoing a whole block of HTML. The variables are being passed in OPs code in exactly the same way as your example.
SajjadSo
The difference is here: $var1= "\"test string\"";
Krzysztof Piórkowski
Thank you, that's helped so much. Why I need to use this form?
SajjadSo
your welcome ;) because the '+' and other string variables must be in the quotations, so the js function can get the string values correctly, otherwise js function get those as non-string variables and values don't have any mean to function.
Adam Brinded
@SajjadSo yes, I see now what you’re saying. In OPs original post he’s not passing string values to the JS function. Good spot!
default