0

Possible Duplicate:
phonegap form submission to remote server

Another question. If this one can be solved, I'll definitely can solve this one phonegap form submission to remote server.

So here's the code index.html

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link href="jquery.mobile-1.0rc1.min.css" rel="stylesheet" type="text/css"/>
<link href="jquery.mobile.scrollview.css" rel="stylesheet" type="text/css"/>
<link href="index.css" rel="stylesheet" type="text/css"/>
<script src="jquery-1.6.4.min.js"></script>    
<script src="jquery.mobile-1.0rc1.min.js"></script>
<script src="phonegap-1.0.0.js"></script>

<script>
function onLoad()
{
    $('#content').load('http://xxx.com/xxx/get.php');

}
</script>

</head>
<body>

<div data-role="page" id="header">

<div data-role="header">
<h1>Header</h1>
</div>

<div data-role="content" id="content">
<input type="button" value="click" onClick="onLoad();">
</div>

<div data-role="footer" id="footer">
<h4>Footer</h4>
</div>

</div>
</body>
</body>
</html>

get.php

<?php
echo '<p>this is a test</p>';
?>

A very simple code, but it doesn't work. Can someone point out where did I do wrong? What I want to do is to get the response from get.php(remote server). So it would print "this is a test" from get.php into #content div in index.html. Thanks in advance.

Community
  • 1
  • 1
ShuFaz
  • 39
  • 1
  • 8

1 Answers1

1

Your onLoad function is only called when you click on your input (as jensgram pointed out):

<script>
function onLoad()
{
    $('#content').load('http://xxx.com/xxx/get.php');

}
$(window).load(function () {
  onLoad();
});
</script>

This way your onLoad function will be loaded when the page is fully loaded.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 1
    Cf. `` (but the naming seems a little off :) ) – jensgram Nov 01 '11 at 08:54
  • He/she wants to run the onLoad function when the document is fully loaded if I'm not mistaken, so if you have a button where you set the onClick event to onLoad, you are using the wrong event. – Lajos Arpad Nov 01 '11 at 09:00
  • Yes, the naming suggests that. I was just objecting to the "Your onLoad function is never called" part of your answer. I should read "Your onLoad function is only called when the button is clicked." – jensgram Nov 01 '11 at 09:06
  • Sorry for the confusion. Yes, originally I want the function to load when the document is loaded. but here I just want to call it by onClick. So we can just consider that onLoad here is just a function name. Also, I've tried the solution above. Still don't work. :( – ShuFaz Nov 01 '11 at 09:11
  • Yes, jensgram, you're right, I'll edit my answer. – Lajos Arpad Nov 01 '11 at 09:28
  • ShyFaz, then the content of your function is incorrect. Have you tried something like $('#result').load('http://xxx.com/xxx/get.php');? Also, did you doublecheck the URL whether it's correct? – Lajos Arpad Nov 01 '11 at 09:32
  • the full link is : http://www.onepixeltech.com/livetraffic/get.php you guys can check by clicking it. And yes, I've tried creating a new div (div id=result) inside the content div, still doesn't work. – ShuFaz Nov 01 '11 at 10:02
  • Please read the documentation from here: http://api.jquery.com/load/, you'll be able to solve your problem after that. – Lajos Arpad Nov 01 '11 at 10:06
  • thanks for the reply, but I've already did that and this is what I've come out with. I've just tested, the code works if I load get.php when it's in the same folder of my project, $('#content').load('get.php');, but once I've move it to the server and change to $('#content').load('http://xxx.com/xxx/get.php'); it failed to work... any ideas on this? – ShuFaz Nov 01 '11 at 10:14
  • No, unfortunately I don't know why does this happening to you. – Lajos Arpad Nov 01 '11 at 10:34
  • hello everyone. this problem is solved! turns out that I'm missing the line < uses-permission android:name="android.permission.INTERNET"> in android manifest. thank you so much for trying to help me... – ShuFaz Nov 03 '11 at 16:05