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>";
Related
I'm trying to pass an id in a link href and I need it to be printed in the URL, my code is:
echo " $ville_nom ";
But the id is not print in the url, could you help me with the syntax ?
You have mistake in your usage of echo and your concat was wrong.
The corrected code :
echo "<a href='ville.php?id='".$ville_id."'> $ville_nom ";
When you used the double quote it's not necessary to concat variables to show their content. Be careful: it can be dangerous sometimes.
echo "<a href='ville.php?id=$ville_id'> $ville_nom ";
Try to use this:
echo "<a href='ville.php?id=" . $ville_id . "'>"
i am trying to create a dynamic link. $path="uploads/" and $fileName="Data Communication and Networking.pdf" which is retrieved from database. But the link gets created with href="upload/Data" ignoring the " Communication and Networking.pdf" part. How to add $fileName with spaces between the content.
$message=$row["message"];
$fileName=$row["filename"];
$date=$row["date"];
echo "<tr>";
echo "<td>".$Serial."</td>";
echo "<td>".$message."</td>";
echo "<td>Download</td>";
echo "<td>".$date."</td>";
echo "</tr>";
$Serial++;;
href value must consist in quotes. Place single quotes around your value.change your href as below:
echo "<td><a href='".$path.$fileName."'>Download</a></td>";
You can replace any space with %20
$CompletePath = str_replace(" ", "%20", $path . $fileName);
echo "<td>Download</td>";
//uploads/Data%20Communication%20and%20Networking.pdf is a valid URL
$path="/path";
$filename="/file with space.name";
echo "<td>Download</td>";
Will output:
`<td><a href=/path/file with space.name>Download</a></td>`
So you need to put the path between the quotes like this:
$path="/path";
$filename="/file with space.name";
echo "<td><a href='".$path.$fileName."'>Download</a></td>";
I am working on my college web design course during my easter break and have run into a problem...
I have a menu on each page that I have removed from the html pages and written into a php file and that is used with an include in each page. Everything works fine apart from the onclick events that when clicked SHOULD load another page but at the moment does not do anything.
Here is my php file...
<?php
echo "<div id='leftPnl'>";
echo "<div class='navcon'>";
echo "<button class='static'
onclick='window.location.href='"."./index.html'".">Home</button>";
echo "<button class='accordion'>Gallery</button>";
echo "<div class='panel'>";
echo "<button class='static' onclick='window.location.href='"."./html/countryside.php'".">Countryside</button>";
echo "<div id='socialmedia'>";
echo "<img src='../images/sitewide/map.png'>";
echo "<br>Contact us on...<br>";
echo "<a class='fab fa-facebook-square fa-2x'
onclick='msg('"."Facebook"."')"."></a>";
echo "<a class='fab fa-instagram fa-2x' onclick='msg('"."Instagram"."')".">
</a>";
echo "<a class='fab fa-twitter-square fa-2x'
onclick='msg('"."Twitter"."')"."></a>";
echo "<a class='fab fa-tumblr-square fa-2x' onclick='msg('"."Tumblr"."')".">
</a>";
echo "<div id='clock'>";
echo "<p class='date'>{{ date }}</p>";
echo "<p class='time'>{{ time }}</p>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
on running the file and inspecting the element in Chrome, the url that the href uses is not formatted correctly the ./ are missing and a single quote is also missing.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Thanks.
PS, I apologise if this has already been asked - I looked and could not find the answer.
Is it simply that I am getting confused with the position of my single and double quotes in the php?
Yes.
Look at the output of the PHP. To take one example:
onclick='msg('Facebook')>
You start the attribute value with '.
Then you try to use ' inside it.
Then you forget the ' at the end!
There is no reason to nest any of this code inside PHP string literals. All that achieves it making it harder to debug.
Just write the HTML directly, outside of <?php ... ?> sections.
It wouldn't be a bad idea to get rid of the intrinsic event attributes too, and use JavaScript to bind event handlers.
The onclick='return confirm('Delete this comment?')' seems to be not working at all:
<?php
if($sesType == 'Admin') {
echo "<a href = 'editCom.php?id=$commentID'>Edit</a> ";
echo "<a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
} else if($_SESSION["username"] != NULL AND $_SESSION["username"] == $postname) {
echo "<a href = 'editCom.php?id=$commentID'>Edit</a> ";
echo "<a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
}
?>
But the confirmation box does not appear.
You're using single quotes inside of single quotes. Use double quotes to wrap your code instead:
Link
Just change this code, and it will work.
echo "Delete";
You have misused the single quote and double quotes. You have to put slash () on quotes to escape. The html should look like this:
Link
Take note that single quote should be inserted inside the double quotes ""
from your code:
echo " <a href='deleteCom.php?id=$commentID' onclick='return confirm('Delete this comment?')'>Delete</a>";
It should be:
echo "Delete";
I'm trying to echo a js function in HTML string inside PHP echo.
And I can't figure it out :
$MY_JS_FUNCTION = '<script type="text/javascript">item + getLastField2()</script>';
if($row2->type == 'text') {
echo "<li id='item-".$row2->rank."' class='list_item'>";
echo "<textarea rows='2' id='".$row2->single_id."' cols='90' name='field[".$MY_JS_FUNCTION."]' data-kind='text' >".$row2->content."</textarea>";
echo "</li>";
echo '<br />';
}
Any ideas to get this work? I think I have so much quotes in it or something like that...
Any help would be very very appreciated, thanks!
I'd reccommend storing the name in the database as well.
Then you can use $row2->name to insert the right name
Your variable $MY_JS_FUNCTION contains an HTML <script> tag with some (strange) JavaScript code (missing a semi-colon). Based on your code the echo on line 5 results in this HTML:
<textarea ... name='field[<script type="text/javascript">item + getLastField2()</script>]' ... >...</textarea>
This is definitively not valid HTML. And there is your problem...
It appears your intent is to echo that JS so that when the page loads, the JS actually sets the value of the name field for that textarea. If that's the case, a simpler way might be something like this:
$MY_JS_FUNCTION = '<script type="text/javascript">document.getElementById("myTextArea").name = item + getLastField2()</script>';
if($row2->type == 'text') {
echo "<li id='item-".$row2->rank."' class='list_item'>";
echo "<textarea rows='2' id='".$row2->single_id."' cols='90' id='myTextArea' data-kind='text' >".$row2->content."</textarea>";
echo $MY_JS_FUNCTION;
echo "</li>";
echo '<br />';
}
That will produce valid HTML. The JS function will fire once that line is reached and update the "name" value to whatever the result of the function is. Be sure to add the "id" field so that the JS knows which element to target.