Reputation: 13
I work on MySQL PHP
while($row = mysqli_fetch_array($result))
{
echo "<a href=javascript:click('$row[Name]')>".$row['Name']."</a>";
echo "<br><br><br>";
}
But in result:
<a href="javascript:click('Slow" cooker="" pepper="" steak')="">Slow Cooker Pepper Steak</a>
javascript
clikc(name)
{
alert("test");
}
what is error.
Upvotes: 0
Views: 93
Reputation: 91734
You need to quote your href
value and you should encode the data for the medium you are outputting to in case your data contains characters that might break your html or javascript.
So you could use something like:
echo "<a href=\"javascript:click(" . htmlspecialchars(json_encode($row['Name']))) . ");\">"
. htmlspecialchars($row['Name']) . "</a>";
Upvotes: 1
Reputation: 1955
If you plan to use vars like this, you should escape them
version 1:
echo "<a href=\"javascript:click('".addslashes($row['Name'])."');\">".$row['Name']."</a>";
version 2,cleaner:
$value = addslashes($row['Name']);
echo <<<EOD
<a href="#" onclick="click('{$value}');return false;">{$row['Name']}</a>
EOD;
version 3,clean js also:
a) php:
$value = addslashes($row['Name']);
echo <<<EOD
<a href="#" class='js-clicky' data-value="{$value}">{$row['Name']}</a>
EOD;
b) html, using jQuery
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
$('.js-clicky').click(function(){
alert($(this).data('value');
})
</script>
Read here about :
heredoc: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
addslashes: http://php.net/manual/en/function.addslashes.php
jquery : http://jquery.com/
Upvotes: 0
Reputation: 42166
Something wrong with your concatenation. Try this:
echo "<a href=javascript:click('".$row['Name']."')>".$row['Name']."</a>";
And clikc(name)
must be click(name)
.
Upvotes: 0