I have a HTML achor tag like below:

echo '<a href="javascript:tempBuy('.$res_get_price[0][0].','.$res_get_price[0][1].','.$res_get_price[0][2].','.$dt_str.')">'.$res_get_price[0][0];

And the corresponding javascript function tempBuy() is

function tempBuy(rate,veg_name,market_name,dt)
{
      alert(dt);
}

But the problem is it does not alert at all ! May be I need to include the variable names within single quotes in tempBuy() function. I tried tempBuy(\'var1'\,\'var2\'...) but it shows error. How can I able to to that. Thanks .

Source for the part shows like this:

<td width="120px" class=""><a href="javascript:tempBuy(56.0,Apple,Bangalore,2013-05-18)">56.0</a>                                
                                 </td>
                                <script>
                                    function tempBuy(rate,veg_name,market_name,dt)
                                    {
                                        alert(rate);

                                    }
                                </script>
有帮助吗?

解决方案

You didn't wrap your javascript arguments in quotes. You need to wrap each variable in single quotes, since you used double quotes for "href" attribute. Another thing is that you didn't close up "a" HTML tag.

echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\',\''.$res_get_price[0][1].'\',\''.$res_get_price[0][2].'\',\''.$dt_str.'\')">'.$res_get_price[0][0].'</a>';

其他提示

If there is anything in your variables that is not a valid javascript literal you have to make it a string like:

echo '<a href="javascript:tempBuy(\''.$res_get_price[0][0].'\' ...

If there are ' in your variables you have to replace them with \' as well.

As you can see form the rendered output, you need to quote the last 3 arguments which are non-numeric. The correct output should be: javascript:tempBuy(56.0,'Apple','Bangalore','2013-05-18')

The corrected PHP code is:

echo '<a href="javascript:tempBuy('.$res_get_price[0][0].',\''.$res_get_price[0]`[1].'\',\''.$res_get_price[0][2].'\',\''.$dt_str.'\')">'.$res_get_price[0][0].'</a>';`
echo "<a href=\"javascript:tempBuy('".$res_get_price[0][0]."','".$res_get_price[0][1]."','".$res_get_price[0][2]."','".$dt_str."')\">".$res_get_price[0][0];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top