views:

1143

answers:

2

I have a problem in integrating PHP and JQuery:

My main file is MyFile.html and the AJAX call file is ajax.php.

The ajax.php function returns links to myFile.html as

<a href Link.php?action=Function ></a> (i.e echo " <a href Link.php?action=Delete";)

When I click the returned link from MyFile.html it's performing as expected. I need how to modify the equivalent code to work correctly in Myfile.Html.

My motivation is that the ajax.php return link should work in HTML.

Any ideas?

+2  A: 

You're missing the = and quotes:

<a href Link.php?action=Function >

should be

<a href="Link.php?action=Function">

You'll need to escape these in PHP like so:

echo "<a href=\"Link.php?action=Function\">";

Or alternatively use single quotes:

echo '<a href="Link.php?action=Function">';
Greg
Thanks RoBorg , Its Now Working
venkatachalam
A: 

It sounds like you are trying to simply set the contents of an HTML element to the result of executing a PHP script. Here is a sample PHP script that will just print an HTML link depending on what parameter you pass it in the 'foo' parameter.

<?
  // Get the parameter "foo" from the URL string.
  $action = $_GET['foo'];

  // Return a different link depending on what 'foo' is.
  switch ($action) {
    case 'a':
      print('<a href="Link.php?action=Delete">Delete</a>');
      break;
    case 'b':
      print('<a href="Link.php?actiom=Edit">Edit</a>');
      break;
    default:
      print('<a href="Link.php?action=New">New</a>');
      break;
  }
?>

Now you need to load that PHP script from inside your HTML file using Javascript (jQuery). Here is a simple page demonstrating that.

<html>
<head>
<title>Demo Page</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
  $(function() {
    $.get('ajax.php?foo=a', function(data) {
      $('#result').html('Received response: ' + data);
    });
  });
</script>
</head>
<body>
<!-- This div will contain a link to the "Delete" -->
<div id="result"/>
</body>
</html>

Some things to remember:

  1. This demo assumes the "ajax.php" and "demo.html" files are in the same directory.
  2. It is also assumed you have the jQuery Javascript file located in a file called "jquery.js" in a directory called "js".
  3. You need to run this example from a live web server. In other words, this will not work if you put these files on your desktop and open "demo.html" in a web browser. There are two reasons for this. Security restrictions in modern browsers often times prevent AJAX calls from local files. Also, the PHP page will not run, rendering the whole exercise useless.

Here is the directory structure you should have, assuming /www/data is the root directory of your web server's files:

  • /www/data/demo.html
  • /www/data/ajax.php
  • /www/data/js/jquery.js
William Brendel
Thanks William Brendel,I worked with your example its working fine now, I have another doubt in your example .The Demo.html has the link of Link.php . When I click this new window opens, I need the Link.php inside the Demo.html ,Any Idea for do this
venkatachalam
I'm not sure I understand the problem you are having, but it sounds like you might want to enclose it in an iframe tag?
William Brendel