tags:

views:

246

answers:

5

In my code I am just hyperlinking to an anchor tag for a PHP function when I click

My code is below, another.php is called by AJAX, index.php from JQuery.

another.php

 <?php

   echo " <a href='my.php?action=show&amp;id=".$fid."'
     onclick=\"return  display('".$fid."');\"> ";  
  echo "" .$fname."</a> ";
 ?>

The hyperlink does not work when I click it.
Am I having a problem with leading spaces?
Do I need to change my code?

A: 

Why is your second echo "" .$fname."</a> "; ??? The first "" doesn't have to be there at all

Moreover it appears that the space shouldn't be in front of dot - .

Skuta
That space before the dot is meaningless really, depending on the coding standard you are using it could either be required to be there or forbidden. Personally, I remove the space but others like it there for readability.
Jayrox
Both valid points but neither would cause the problem reported.
roryf
+4  A: 

If the "display" function is returning false, your onclick handler is short circuiting the default hyperlink functionality. Instead of onclick being set to "return display($fid)", just set onclick="display($fid)" and the hyperlink will always work.

Without seeing the rest of your code or a more detailed description, I can't really tell what else might be the problem. But this is the first thing that jumps out at me. If an element's event handler returns false, the regular functionality never occurs.

Rich
is your logic same while using ajax?
venkatachalam
ajax is just a javascript
kender
A: 
<?php
   echo '<a href="my.php?action=show&id='.$fid.'"
     onclick="return  display(\''.$fid.'\');">'.$fname.'</a>';
 ?>

That should work I always use ' for php strings and " in the HTML part makes it a lot easier to read :)

Also the bigger error was that your link looked like this: Bla because you made the a self closing it probably was showing a link with no value

Thomaschaaf
Will u help for ajax calling above code ? to main file
venkatachalam
A: 

I can't quite figure out what do you want to achieve with all that stuff inside one anchor tag... react to an onclick event and redirect the browser to another location? I have learned that the easy way of tweaking an <a> tag's functionality is:

<a href="javascript:doStuffHere()" id="...">...</a>

So you don't mix onclick with the mandatory href. Try rebuilding your link to be something like this.

As to your PHP style, when you're using double quotes for delimiting Strings, you don't have to break out of the quotes to insert variable contents. You could wirte:

<?php

$fid = 'myId';
$fname = 'my link name';

echo "<a id=\"$fid\" href=\"javascript:doStuff()\">$fname</a>";

?>
Peter Perháč
this style does not allow the user to open pages in a new tab if the link just changes the page goes to an article for example the way he did it you can either stay on the page and do something or middle click and open it in a new window
Thomaschaaf
I imagine "all that stuff" is probably so the JS gracefully degrades. In your example above, what happens when the user doesn't have JS enabled? They won't go anywhere, and they'll have a broken, unusable link. Much better to provide onlick for those with JS enabled, and a href for those who don't.
Rich Adams
The duration of WWII was 6 years and changed the world forever. We're 9 years into the 21st century and some people still have JS off? And how does he want to call this page using AJAX? with JS turned off?
Peter Perháč
What about mobile devices without JS? Text based browsers? Search engines trying to index the link? Never assume things about what users are running. Just because a lot of people enable JS, doesn't mean everyone does. http://accessites.org/site/2007/02/graceful-degradation-progressive-enhancement/
Rich Adams
Please refer to the question asked: "My code: I have call the another.php by ajax, from index.php using JQuery" JQuery magic and text based browsers? I am trying to answer the question, not polemicize on whether or not it's /good/ to take this approach in EVERY web application. :)
Peter Perháč
I wasn't answering their question, I was answering yours :) "I can't quite figure out what do you want to achieve with all that stuff ...", saying it's likely for graceful degredation. Just because someone uses JQuery, doesn't automatically mean the site should only work with JS enabled ;)
Rich Adams
But yeah, I didn't mean to sound argumentative :) I was just pointing out the reason you can have href and onclick in the same anchor, and why it's generally a good idea as it will mean anyone in any browser can still use a site. Obviously if a site only works with JS on it makes no difference :)
Rich Adams
I'd like to persuade the world to use a decent browser (e.g. Firefox) and have their JS turned on :) I nurture the delusion that it's so; and hope that one day my dreams will come true :D ;)
Peter Perháč
A: 

Like the others have mentioned it is probably something with the JavaScript return. As I can't really say anything wrong with the PHP code its self that would cause the problem.

However, I do have some tips to make your code a little more easy on the eyes.

My first suggestion with dealing with strings is to never force yourself to escape ' or ". By switching your quote style, you can make it drastically easier to read your code and find issues caused by quotes much easier.

echo " <a href='my.php?action=show&amp;id=".$fid."' onclick=\"return display('".$fid."');\"/> ";

This would become:

echo ' <a href="my.php?action=show&amp;id='.$fid.'" onclick="return display("'.$fid.'");"> '.$link_title.'</a>;

Actually, while typing this out, I noticed you aren't getting a hyperlink because you aren't completing the anchor tag. More specifically, you are terminating it early with the / In the above example, I have added:

.$link_title.'</a> This should fix the problem.

Second suggestion:

echo "" .$fname."</a> "; Can just as easily be typed out as:

echo $fname.'</a> ';

Using ' instead of " for strings that don't require extra work by the server is recommended, and the leading "" is not required.

EDIT:

echo ' <a href="my.php?action=show&amp;id='.$fid.'" onclick="return display("'.$fid.'");"> '.$fname.'</a>;

Does this work? Does it display the link?

Jayrox
hyperlink is for fname and i included that also, my probs is i unable to get hyperlink in ajax
venkatachalam