I was not clear first time about ajax
requests.
When I click on the link another one should appear.
Here it is what I'm trying to do (simplified), but the problem is when it appears the JS inside it doesn't want to run.
HTML:
<html><body>
<div id="result"> </div>
<a href="javascript:suggest('result'); return true;">click to see the link</a>
</body></html>
JavaScript
function evalResponse (request)
{
try
{
//var responseText = request.responseText.replace (/\n/g, " ");
var responseText = request.responseText;
return eval('(' + responseText + ')');
}
catch (e)
{
alert ("line: " + e.lineNumber + "\nmessage: " + e.message + "\nName: " + e.name);
}
}
function suggest(target_div)
{
var opt = {
// Use POST
method: 'post',
// Send this lovely data
postBody: 'Action=Edit&search_word=' + search_item + '&target=' + textarea,
// Handle successful response
onSuccess: function(t) {
var json = evalResponse (t);
data = json[1];
if (errors.errorText == undefined)
{
document.getElementById('target_div').update(data['link']);
}
},
// Handle 404
on404: function(t) {
alert('Error 404: location "' + t.statusText + '" was not found.');
},
// Handle other errors
onFailure: function(t) {
alert('Error ' + t.status + ' -- ' + t.statusText);
}
}
new Ajax.Request('handler.php', opt);
}
PHP handler:
$jsonArray = array();
$jsonArray[1]["link"] = '<a href="javascript:alert(\'fdg\');" onClick="return true;">link</a>';
$json = new Services_JSON();
echo $json->encode($jsonArray);