0

Have a interesting problem. I make a little ajax mechanism to get info from php file and add a function for delete the data.
phpfile.php:

<?php
if(isset($_POST['bu'])){
$data = 'text<a onclick="something();" href="#">alert</a>';
echo $data;
}
?>

js file:

function getdatafromfile(){
var bu = 'bu';
$.ajax({
			type: 'POST',
			url: 'phpfile.php',
			data: {bu: bu},
			success: function(msg){ 
			$("#msg").html(msg);
			getContent();
			}
		});
}

function something(){
alert("alert");
}

Data from the php file is fine, but when I click on the link (<a onclick="something();" href="#">alert</a>) getted from ajax, function don't work.
How to fix that?
Thanks in advance!

Edited by gorleone: n/a

2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by Airshow
1

That approach can be made to work but it is generally considered better to attach event handlers in javascript rather than HTML.

<?php
if(isset($_POST['bu'])){
  $data = 'text <a href="#">alert</a>';
  echo $data;
}
?>
function getdatafromfile(){
  var bu = 'bu';
  $.ajax({
    type: 'POST',
    url: 'phpfile.php',
    data: {bu: bu},
    success: function(msg){ 
      $("#msg").html(msg).find('a').click(function(){
        something();
        return false;//which ever way you attach it, be sure to return false from the handler to suppress the <a> tag's default hyperlink action.
      });
    }
  });
}

Airshow

0

That is my mistake, but the script still does not work. And I apply handlers in the html, because my php script is a get data from my database and I try to make unique delete button for each row(item). Thank you. I'm really not good js writer, but I like the idea to get,change,delete data from database without refresh the page and everything went well until I came here ( to this problem)

Edited by gorleone: n/a

0

What you are trying to write is generally called a "Create, Read, Update, Delete" (CRUD) interface.

There's no reason why handlers for all the buttons should not be attached in javascript.

Try a web search and you should find some sample scripts.

Airshow

0

Thanks for the help, solution of the problem is stop using jQuery and come back to old school ajax & javascript. I just don't understand the idea of jQuery. He is great library, but not for me.

0

Personally, I wouldn't attempt this sort of thing without jQuery. Raw javascript would take longer to write and would be far more volumous (though it might actually be more efficient).

jQuery takes some getting used to but is very rewarding once you have made the investment.

The best jQuery programmers are the ones who were good javascript programmers first, so yes carry on with your raw js approach with a view to translating your code to jQuery at some point in the future when time permits.

Airshow

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.