3

HTML element:

<img align="center" onclick="doc()" src="images/doc.png">

JavaScript function:

function doc (){
    $.post("php/file.php", { name: "John", time: "2pm" },).done( function(data) {
        alert("Data Loaded: " + data);
    });
}

file.php:

$pi=$_POST['name'];
echo ($pi);

I don't know why, but $.post doesn't send data when I click on the image it doesn't do anything

epascarello
  • 204,599
  • 20
  • 195
  • 236
user2090823
  • 31
  • 1
  • 6

3 Answers3

0
 function doc (){
  $.post("php/file.php", { name: "John", time: "2pm" })
   .done( function(data) {
    alert("Data Loaded: " + data);
    }); 
  }
EnterJQ
  • 1,014
  • 8
  • 18
  • 4
    It looks like you corrected the extraneous comma, but you should be a lot more clear in explaining your answer, especially for such a tiny change. – jonvuri Feb 20 '13 at 14:07
  • Try to use Firebug or Webkit dev-toolbar (Chrome/Safari) to see if JavaScript errors are given by the interpreter. You can also see in there if XHR calls are done and what the traffic in it is. –  Feb 20 '13 at 14:32
  • The code doesn't work on firefox because i use phonegap and i run the app with ripple – user2090823 Feb 20 '13 at 14:39
0

Having both inline JavaScript and jQuery is not a good practice; use jQuery to bind the click event.

For this first add identifier to the image:

<img id="imgDoc" align="center" src="images/doc.png">

Then in the code:

$(document).ready(function() {
    $("#imgDoc").bind("click", function() {
        $.post("php/file.php", { name: "John", time: "2pm" }).done( function(data) {
            alert("Data Loaded: " + data);
        });
    }
});
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • I don't see how that would solve his issue. Although, it might be better practice to do it this way, it certainly isn't erroneous to do it the way he has shown in his script above. – crush Feb 20 '13 at 14:27
  • @crush the `doc()` function might be declared as part of other function, thus not global. OP didn't post full code so we can't know. – Shadow The GPT Wizard Feb 20 '13 at 14:28
  • i create a new file post.js where i wrote the code you write above but it doesn't work – user2090823 Feb 20 '13 at 14:37
  • @user2090823 Just having it in a file won't do anything. You also have to include jQuery and include that "post.js" file in your main file. Until you grasp the basics it's really better if you put all JS code in the main file. – Shadow The GPT Wizard Feb 20 '13 at 14:40
  • i have already done – user2090823 Feb 20 '13 at 14:41
  • So good chance it can't find the "php" subfolder since it's looking in the "js" subfolder. Try `$.post("/php/file.php", ...` assuming it's under the root. – Shadow The GPT Wizard Feb 20 '13 at 14:49
  • External scripts don't use their directory as the cwd as far as I know, but use the page they are included on as the cwd. I know the opposite is true with CSS, though. – crush Feb 20 '13 at 14:50
  • @crush is this behavior uniform in all browsers? Never delved into it, as I was using the root. – Shadow The GPT Wizard Feb 20 '13 at 14:51
  • i wrote the entire path $.post("http://localhost/php/file.php",... but doesn't work – user2090823 Feb 20 '13 at 14:54
  • @ShadowWizard That's a good question to which I don't know the answer. Unfortunately, I'm not sure we are going to be able to figure out this guys error without a low more information/access to his site. Something weird is going on, and I suspect it boils down to something basic. – crush Feb 20 '13 at 14:55
  • @user2090823 looks like debug is ahead of us then. Add `alert('click');` right after the line `$("#imgDoc").bind("click", function() {`. Do you see alert when clicking the image? – Shadow The GPT Wizard Feb 20 '13 at 15:10
  • no i don't see the alert: $(document).ready(function() { $("#imgDoc").bind("click", function() { alert('click'); } }); – user2090823 Feb 20 '13 at 15:15
  • @user2090823 OK let's take a step back. In the "post.js" file you have put `alert("hello");` as the very first line. Do you see the alert when loading the page? – Shadow The GPT Wizard Feb 20 '13 at 15:16
  • when the html page that recall post.js load i don't see any alert – user2090823 Feb 20 '13 at 15:20
  • @user2090823 Sorry man, looks like your problem is much more basic than just "post doesn't work". When you can get JS to show "hello" please repeat everything explained above and if still stuck feel free to let me know. – Shadow The GPT Wizard Feb 20 '13 at 15:23
  • when i use firebug, it give me these error in the jquery file: xhr.send( ( s.hasContent && s.data ) || null ); that is the 8526 row of the latest version of the uncompressed file – user2090823 Feb 20 '13 at 16:24
  • @user2090823 what is the exact error message you see in Firebug? – Shadow The GPT Wizard Feb 20 '13 at 20:45
  • Shadow thanks for your help. The error come out when i click the image, with a red colour: POST http://localhost/php/file.php 200 OK 78 ms. Then it refers to the 8526 row of jquery. I use xampp maybe this could be a problem? – user2090823 Feb 21 '13 at 09:58
  • This doesn't look like an error. Sorry, can't see what might be wrong. – Shadow The GPT Wizard Feb 21 '13 at 10:03
  • this is what chrome debugger says:XMLHttpRequest cannot load http://localhost/php/file.php. Origin http://127.0.0.1:8020 is not allowed by Access-Control-Allow-Origin. – user2090823 Feb 21 '13 at 10:19
  • Hei I solved the problem adding on the php file: header('Access-Control-Allow-Origin: *'); thank you for your patience. – user2090823 Feb 21 '13 at 10:30
  • @user2090823 So it was PHP issue after all? Weird. Glad you got it working! – Shadow The GPT Wizard Feb 21 '13 at 10:35
  • Yeah!! It now works if i run it on a browser. But something's wrong with phonegap. This is the callback function:success:function(json){alert('Success');},error:function(){alert("Error");}, in the browser i can see the alert with success instead in the emulator appears the error alert. I don't know why – user2090823 Feb 21 '13 at 15:57
  • @user2090823 read [here](http://api.jquery.com/jQuery.post/) it explains how to handle errors. – Shadow The GPT Wizard Feb 21 '13 at 16:01
0

Jquery library you mentioned used at one of your comments is:

<script src="code.jquery.com/jquery-1.8.3.min.js"></script>

It is not a correct source. please use below source:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Amir
  • 4,089
  • 4
  • 16
  • 28
  • that's not the problem the link works fine. In fact, trying to run the code in web, after have added the string that allow cross domain in the php file i have no problem and the alert appears. But tryng to make the same thing on the emulator the alert doesn't appear. – user2090823 Feb 21 '13 at 15:53