Question

Here's my problem: I have a form in index.html, it will have a text input, and the value will be $.post to a php page that should process it and will return a value. My code:

index.html:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script>

<script>
    function get(){
        $.post('http://xxx/xxx/get.php', {name: form.name.value},
            function(output){
                $('#result').html(output).show();
            });
    }
</script>

</head>
<body>

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

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

<div data-role="content">

<form name="form" id="form">
    <input type="text" name="name"><input type="button" value="Get Output" onClick="get();">
</form>

<p> result here: </p>
<div id="result"></div>

</div>

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

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

and get.php

<?php
$name = $_POST['name'];
echo $name;
?>

This is simple coding for my testing purpose. As we can see from the code, whatever text we input in the textfield will be processed in get.php. Since I put echo $name there, the name SHOULD be shown in div id = result in index.html. I think I got the code right but it just didn't work. So I need help from the experts here... what did I do wrong?

Was it helpful?

Solution

A few points:

1. Add the remote server to the whitelist...if you are using blackberry ou must edit the config.xml file, if is a ios app, you must change the plist file of the project.

EDIT 2011-11-03: In Android: Make sure this line is in phonegap app manifest file: < uses-permission android:name="android.permission.INTERNET">

2. Try adding this header to the php file header('Access-Control-Allow-Origin: *');

EDIT 2011-11-03: this header must be included on the beginning of the PHP file.

  1. $('#result').html(output).show(); the show method is not necesary, you must use that only if the field has the hidden attribute.

Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top