JQuery AJAX feedback not working

I have an issue with PHP and JQuery integration:

My main file MyFile.html

, and the AJAX call file ajax.php

.

The function ajax.php

returns references to MyFile.html

how

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

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

)

When I click on the returned link from MyFile.html

, it performs as expected. I need how to properly change the equivalent code to work in MyFile.html

.

My motivation is that the backlink ajax.php

should work in HTML.

Any ideas?

0


source to share


2 answers


It looks like you are trying to simply set the content of the HTML element as a result of the PHP script execution. Here is an example PHP script that will simply print an HTML link depending on which parameter you pass 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 this PHP script from your HTML file using Javascript (jQuery). Here's a simple page demonstrating this.

<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:

  • This demo assumes that the "ajax.php" and "demo.html" files are in the same directory.
  • It also assumes that the jQuery Javascript file is located in the jquery.js file in a directory called "js".
  • You need to run this example from a live web server. In other words, it won't 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 prevent AJAX calls from local files. Also, the PHP page won't 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 files:

  • /www/data/demo.html
  • /www/data/ajax.php
  • /www/data/js/jquery.js
0


source


You are missing the =

quotes too :

<a href Link.php?action=Function >

      

it should be

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

      



You need to get rid of them in PHP like so:

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

      

Or, alternatively, use single quotes:

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

      

+2


source







All Articles