Reputation: 651
I am having trouble with a program that I have written. The idea is that I click a link and the value of the link is picked up in a JavaScript variable. I then do a GET request to send the value to a PHP variable and print it out.
Here is the code:
The HTML
<html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div id='mydiv'>
<a href='/codeigniter/index.php/hashtest/hi'>Link 1</a>
<a href='/codeigniter/index.php/hashtest/hi'>Link 2</a>
</div>
</body>
</html>
The jQuery (in a <script>
tag within the above body)
$(function()
{
var mydiv = $('#mydiv');
mydiv.on('click', 'a', function(){
var text = $(this).text();
console.log(text);
//$.get("hashtest.php", {qwerty: text});
$.ajax
({
url: "/codeigniter/index.php/hashtest/hi",
data: {
qwerty : text
},
async: "false",
success: function(data){
console.log("success!");
},
error: function(jqXHR, status, error)
{
alert("Status : " + status + " error: " + error);
}
});
});
});
The PHP
class Hashtest extends CI_Controller {
function __construct() {
parent::__construct();
}
public function hi() {
$x = $this->input->get('qwerty');
print $x;
print "";
}
}
The error:
NS_ERROR_NOT_AVAILABLE: prompt aborted by user
At the moment, the JavaScript correctly gets the right link value as the console.log() line outputs the right thing for both links. But it seems the PHP is not receiving this value because nothing is being printed out. I've tried replacing the variable with a hard-coded string but it makes no difference. I can print static PHP from within the method above so I don't think it's an issue where I can't print any PHP at all.
A few points:
I used $.ajax instead of $.get just to see what error message would appear (ideally I'd want to use $.get though). It seems it's some sort of uncaught exception based on what I've read about the above error message and the fact that the alert box that appears doesn't show an error message, just a status of "error".
I don't expect any value back, the success method is just there to see if the request is wokring or not (which it currently isn't)
I am assuming that the id of the link is unknown.
The code is all within the same class and the URL that I'm pointing to in the AJAX request is correct (I got a HTTP 200 code back in my browser console).
The PHP was written using the CodeIgniter framework.
Upvotes: 3
Views: 137
Reputation: 791
Your code is correct, but your not stopping the event when you click on the link.
Add e.preventDefault();
var mydiv = $('#mydiv');
mydiv.on('click', 'a', function(e){ e.preventDefault(); var text = $(this).text(); </code>
Upvotes: 0
Reputation: 780843
You need to prevent the default action of following the link:
$(function()
{
var mydiv = $('#mydiv');
mydiv.on('click', 'a', function(e){
e.preventDefault(); // <-- THIS IS NEEDED
var text = $(this).text();
console.log(text);
//$.get("hashtest.php", {qwerty: text});
$.ajax
({
url: "/codeigniter/index.php/hashtest/hi",
data: {qwerty : text},
async: "false",
success: function(data){
console.log("success!");
},
error: function(jqXHR, status, error)
{
alert("Status : " + status + " error: " + error);
}
});
});
});
Upvotes: 3