views:

43

answers:

2

Dear all

I have database of website link, it list out in main file ,when i try to click that link it get to redirect on that database link. my code is test.php

<?php

// getting from database

 echo '<li onclick=\"window.location='.$result->website.'\">
       <a href="#">'.$result->option.'</a></li>';
?>

The Main.html calls that test.php while ajax

$.post("test.php", {queryString: ""+inputString+""}, function(data){

});

how to do it? any idea Is it possible with serverside script? whats wrong with my php code?

A: 

Your code does not work for invalid URLs. www.google.com is not an URL, just a domain name. So skip the silly Javascript links, instead use:

 echo "<li><a href=\"$link\">$link</a></li>\n";

And your Javascript success function seems somewhat empty, so instead use .load() like:

 $("ul").load("links.php", {queryString: ""+inputString+""})
mario
A: 

Edited after venkat's comment:-

According to your last comment, the code you have problems with is the following:-

<?php
$link="www.google.com";
echo "<a href='#' onclick=window.location='$link'>Click here</a>";
?>

This above code should actually be the following:-

<?php
$link = "http://www.google.com/";
echo '<a href="'.$link.'">Click here</a>';
?>

The reason for adding the "http://" string is that the variable "$link" is going to be used as an HTTP URL, which requires mentioning of this "http://" string, mainly because of the protocol to be used by the browser. In this case, the protocol is HTTP.
Always remember that for any URL, there must be a string "http://" at the beginning of the URL string, when stored in either a database / variable.

Coming back to the code in your question, which was:-

<?php
// getting from database

echo '<li onclick=\"window.location='.$result->website.'\"><a href="#">'.$result->option.'</a></li>';
?>

Now here the position of "window.location" is not totally correct. It should have been in "href" attribute of "a" element, instead of putting it in "onclick" attribute of "li" element.
So the code should actually be:-

<?php
// getting from database
echo '<li><a href="'.$result->website.'">'.$result->option.'</a></li>';
?>

Hope it helps.

Knowledge Craving
Your code getting database seemsto be not work however i fix the problem!
venkat
@venkat - can you please post the fix of the problem? Also it would be good if you can point as to where my code is wrong, so that I can correct it too. Thanks
Knowledge Craving
your code echo '<li><a href="window.location=\''.$result->website.'\';">'.$result->option.'</a></li>'; wrong.Hope i too try the solution ,However mario tricks helps me
venkat
Knowledge Craving
@venkat - Please check my edited answer. Thanks for your comment.
Knowledge Craving
Good I It works Craving!!
venkat