I have a php program which is suppose to alert the links clicked.For example I have a link hello
and when I click on that link javascript should alert hello
. It works fine without spaces, but when I have a link like hello world
it does not alert anything.These words are extracted form a database.
My code is given below
function gmail(val)
{
alert(val);
}
For php
<?php
$name="raj"; //this is just a dummy value
$include "database_connectivity.php";
$conn=odbc_connect($dsn,$database_username,$database_password);
if(!$conn)
{
die('Could not connect to database.'.odbc_error());
}
$select="SELECT WHERE_TO_CHANGE FROM REQUEST_SEND_TABLE WHERE SENT_FROM ='$name'";
$exe=odbc_exec($conn, $select);
if(!$exe)
{
die("Could not execute query".odbc_error());
}
while($row_user=odbc_fetch_array($exe))
{
$show=$row_user['WHERE_TO_CHANGE'];
echo "<input type='hidden' id='".$show."' value='".$show."'>";
echo "<a href='#' id='check' onClick='gmail(".$show.".value)' >".$show." </a>";
echo"<br>";
}
odbc_close($conn);
?>
Can anyone tell me whats wrong here ?
while($row_user=odbc_fetch_array($exe))
{
$show=$row_user['WHERE_TO_CHANGE'];
$show_nospace = str_replace(' ', '_', $show);
echo "<input type='hidden' id='".$show_nospace."' value='".$show."'>";
echo "<a href='#' id='check' onClick='gmail(".$show_nospace.".value)' >".$show." </a>";
echo"<br>";
}
And if $show
can contain other characters that aren't allowed in IDs, you'll need to replace them as well. You'll also need to escape any quotes when using it in the value
attribute.
in your onclick event, change it to this:
gmail(document.getElementById($show).value)
it might be better to do that in your function though and just pass in the id:
gmail($show)
That way you can check the existence of the element first before trying to call .value on it.
Use quotes when ever there is a space
onClick="gmail('".$show_nospace.".value');"
see the single quotes ('test test')
Try this:
echo "<input type='hidden' id='".$show."' value='".$show."'>";
echo "<a href='#' id='check' onClick='gmail(".$show.")' >".$show."</a>";
instead of:
echo "<input type='hidden' id='".$show."' value='".$show."'>";
echo "<a href='#' id='check' onClick='gmail(".$show.".value)' >".$show."</a>";