PHP a href echo with span not functioning properly - javascript

I have the code
echo '<span class="imperial-username">';
echo'<a target="_blank" href="profile/'.$row['name'].'">'.$row['name'];'</a>';
echo'</span>';
which functions fine when i press the name it takes me to the profile. But if I click the image also above the name it will go to the person who is above on the list, as I have a list of links. And only goes to there profile if you click the name specifically.
Also all other links on the page seem to be not functioning properly and going to random profiles when pressed, the links are carrying over down the page or something is what I assume, maybe code not closed properly, but I see nothing that should be causing this.

You have a semicolon ; instead of a dot . in .$row['name'];'</a>';.
It is not a syntax error, because the instruction '</a>'; is valid but has just no effect.

please try this
echo '<span class="imperial-username">';
echo'<a target="_blank" href="profile/'.$row["name"]>'.$row["name"].'</a>';
echo'</span>';

echo '<span class="imperial-username">';
echo '<a target="_blank" href="profile/'.$row['name'].'">'.$row['name'].'</a>';
echo '</span>';

Related

Calling javascript Function from HTML with multiple parameters

I'm trying to call a javascript function called confirmRemove(title, id) to remove an element from a table. This function should be called when the user clicks on a link that looks like an "X". I attached my code below to show how I went about doing this, but I'm seeming to be having some errors and I'm not sure why. Most of the elements are able to be removed, but for a few when I click the link to call the function, nothing happens. Is there a better way of doing this? Not sure why this would happen.
<td><?php echo '<a onclick="confirmRemove(\'' . $title . '\',\'' . $id . '\')"
href="javascript:void(0)">X</a>';?></td>
You can try this code
<td> <a onclick="confirmRemove('<?php echo $title; ?>','<?php echo $id; ?>')"
href="javascript:void(0)">X</a></td>
You asked if there is a better way of doing this - I would suggest that there is and that it is to use an externally registered event handler bound to the specific HTML elements in question - notably a hyperlink in this example.
If you assign relevant attributes to the hyperlink ( dataset attributes are very useful for this sort of task ) you can process them within your event listener very easily. There is not need for complicated escaping for quotes by adopting this approach and best of all you can separate HTML from Javascript - the event handlers can be in another file
<td>
<a class='removal' href='javascript:void(0)' data-title="<?php echo $title;>" data-id="<?php echo $id;?>">X</a>
</td>
<script>
Array.from( document.querySelectorAll('a.removal') ).forEach( a=>{
a.addEventListener('click',function(e){
confirmRemove( this.dataset.title, this.dataset.id )
});
});
</script>

including html tag in a php code, specifically dealing with href function inside a php block

enter the html code once a condition is satisfied but then i aim to use php values and html tags together.
tried tag etc and got most of the part in running just unable to deal with href beacuse it is referencing to some values.
not sure where to use "" or ``.
<?php.....
echo `Yes`;
?>
"yes" should work as a hyperlink but at the moment the php values are not processed that`s why no result.
You are using wrong sequence of quote
<?php
...
?>
<?php
echo '<a href="limitdatabase.php?Dropdown=' .
$_GET['Dropdown'].
';&search='.
$search_name .
';&wise='.
$_GET['wise'].
';">Yes</a>';
?>
and you should use single quote and not backtics for string
You are using incorrect concatenation. Whenever you want to add any PHP variable then you need to close the braces and need to start after PHP variable as below:
<?php
echo 'Yes';
?>
Also you should wrap Dropdown and wise in braces as it will not work directly in get as you have used. Hope it helps you!
You can use combination of html with php inside:
<a href="limitdatabase.php?Dropdown=<?PHP echo $_GET[Dropdown] . "&search="
. $search_name . "&wise=" . $_GET[wise]; ?>">Yes</a>
Also, you can input whole link in string:
<?php
$mylink = "limitdatabase.php?Dropdown=" . $_GET[Dropdown] . "&search=" . $search_name . "&wise=" . $_GET[wise];
?>
YES

How to add ' to generated link in php

$output .= '<a class="dropdown-toggle has-category" data-toggle="dropdown" href="'.$this->getLink($menu).'" target="'.$menu['target'].'">';
I have php file with this code href="'.$this->getLink($menu).'"
its generates in html this href="https://www.myweb.com/"
Now i need to do it with onClick so that the output turned out like this
onclick="location.href='https://www.myweb.com'"
Changed php code onclick="location.href='.$this->getLink($menu).'"
and result is onclick="location.href=https://www.myweb.com"
Link not working cuz lacks '
Can someone help me?
Try this
onclick="location.href=\''.$this->getLink($menu).'\'">
Example:-
echo '<a onclick="location.href=\''.$this->getLink($menu).'\'">';
Can you add this escaped single quote: \'?
echo '...onclick="location.href='.$this->getLink($menu).'\'"...';
(I added the echo to complete the code example).

Open link in a new window with javascript

I want to open links in a new pop up, I have my link in a PHP Code but when I try to add a on-click I keep getting errors.
This is my code:
echo "<td>Edit</td>";
Where exactly do I need to add the on-click function? Because it's easy in html but I keep getting errors because it's in PHP
Thanks
Add target="_blank"
_blank : Opens the linked document in a new window or tab
echo "<td><a href=\"edit.php?id=$row[id]\" target='_blank'>Edit</a></td>";
For a popup solution add this (attention to the various " ' )
target="popup" onclick="window.open('yourlink','name','width=xxx,height=xxx')"
echo "<td><a href=\"edit.php?id=$row[id]\" target='_blank'>Edit</a></td>"
another way is write a js function open_window = function(){your stuff} and
echo "<td><a href=\"edit.php?id=$row[id]\" onclick='open_window()'>Edit</a></td>"
You need to use window.open javascript functoin. Here is an example:
<a href="#" onclick='javascript:window.open(url, "", "width=200, height=100");' >Edit</a>
You may want to specify the javascript code in a separate function as well.
See http://www.w3schools.com/jsref/met_win_open.asp for more information on window.open function.

PHP link written into page not being retrieved by a JAVASCRIPT function

I'm writing on a page the following code with PHP. This code creates a A HREF link with the ID equal to $intIdType, which is the value of the PRIMARY KEY in a database, and $count gets the amount of records per category on the database. All of this is inside a WHILE that reads each record and writes on the PAGE the results
echo "<a href='#' id='$intIdType'>";//Starts creating the link
echo "$intIdType -";
echo $arrBusinessTypes["business_Description"];
echo "(";
echo " $count1 )"."</a>";
Now, after the results are on the page it will look like this:
$intIdType $arrBusinessTypes $count
--------------------------------------------
1 -Auto Sales ( 1 )
2 -Auto Repair ( 1 )
5 -Web Desig & I.T. Services( 2 )
6 -Computer Shop ( 1 )
The above result displays each rown as a link where I can click on it, but nothing happens. Even just a simple Alert in javascript does not show up. It seems that it never reaches even the Javascript at all.
What I need now is to retrieve that PHP generated code on the page by a Javascript file, that will allow me to use the hiperlink generated by PHP into the .HTML page
It works if I write directly into the page what PHP is suppose to write. I wonder if this happens because Javascript can not read posted data from PHP into the page.
The Javascript File looks like this:
window.onload=post_result
function post_result() {
$("#1").click(function() { //This is the HREF ID that is written by PHP into the page
$('#list_results').load("results.php");//This seeks to run a PHP file in a DIV
$('.title').text('Listing Categories');//This just replaces the Title in the page
})
I'm just a beginner trying. Thanks for any help.
echo "<a id='$intIdType'>";//Starts creating the link echo "$intIdType -";
echo "$intIdType -";
echo $arrBusinessTypes["business_Description"];
echo "("; echo " $count1 )"."</a>";
just remove href attribute and try
Remark #1: quote custom data when outputs it to HTML:
echo $arrBusinessTypes["business_Description"]; must be
echo htmlspecialchars($arrBusinessTypes["business_Description"]);
Remark #2: you don't need window.onload handler here. In this piece of code you select complex way to do simple thing. Why do not write direct onclick handler? Write function loading some data depending of parameter and do something like:
$htmlspecialchars = function($s){return htmlspecialchars($s);};
echo <<<HTML
<a href='#' id='$intIdType' onclick='loadInfo($intIdType)'>
$intIdType -{$htmlspecialchars($arrBusinessTypes["business_Description"])} ( $count1 )
</a>
HTML;
(converted to heredoc to make HTML more clean)

Categories

Resources