Reputation: 1563
I'm trying to make a link which will execute a function and open the page #item-info
simultaneously but I'm having trouble. (I'm using JQuery which is why I have the #)
The part which is causing the problem seems to be the javascript function updateItem(item, type, stock)
<?php
for ($row = 0; $row < $arrlength; $row++)
{
echo "<li>";
echo "<a href='javascript:updateItem($items[$row]["Item"], $items[$row]["Type"], $items[$row]["Stock"]); #item-info'";
echo $items[$row]["Item"];
echo "</a>";
echo "</li>";
}
?>
I think the problems arise with the quote usage but I'm not too sure what I'm doing wrong with them.
Upvotes: 2
Views: 388
Reputation: 690
When updateItem() is called, use window.location.href. As for the quote issues, use single or doubles quotes around your parameters to updateItem()
Use something like Update and define update as:
function updateItem(url) {
// do work updating here, etc.
// ---
// next, redirect
window.location.href=url;
}
Upvotes: -1
Reputation: 5374
You can't use associative arrays inside of strings in the same way as a string variable. Use concatenation instead:
<?php
for ($row = 0; $row < $arrlength; $row++)
{
echo "<li>";
echo "<a href='javascript:updateItem(" . $items[$row]["Item"] . "," . $items[$row]["Type"] . "," . $items[$row]["Stock"] . "); #item-info'";
echo $items[$row]["Item"];
echo "</a>";
echo "</li>";
}
?>
Upvotes: 0
Reputation: 26066
Your initial <a href=
is not closed & the PHP code is not really quoted well within the echo
command:
<?php
for ($row = 0; $row < $arrlength; $row++)
{
echo "<li>";
echo "<a href='javascript:updateItem(" . $items[$row]["Item"] . ", " . $items[$row]["Type"] . ", " . $items[$row]["Stock"] . "); #item-info'>";
echo $items[$row]["Item"];
echo "</a>";
echo "</li>";
}
?>
But I prefer to do things like this with sprintf
to avoid going nuts keeping track of quotes:
<?php
for ($row = 0; $row < $arrlength; $row++)
{
echo "<li>";
echo sprintf("<a href='javascript:updateItem(%s,%s,%s; #item-info'>", $items[$row]["Item"], $items[$row]["Type"], $items[$row]["Stock"]);
echo $items[$row]["Item"];
echo "</a>";
echo "</li>";
}
?>
Upvotes: 1
Reputation: 16595
Try using the onclick event.
<?php
for ($row = 0; $row < $arrlength; $row++)
{
echo "<li>";
echo "<a href='#item-info' onclick='updateItem(\"".$items[$row]["Item"]."\", \"".$items[$row]["Type"]."\", \"".$items[$row]["Stock"]."\");'>";
echo $items[$row]["Item"];
echo "</a>";
echo "</li>";
}
?>
Upvotes: 4