将数据发送到外部服务器的PHP页面,并在本地计算机中从JavaScript加载内容


post data to PHP page in external server and load content from JavaScript in local computer

我想使用位于计算机/移动应用程序的JavaScript将数据发布到服务器(www.domaine.com)中的PHP文件

示例:test.php
<?php
    $x = $_POST['count'];
    for ($i = 0; $i < $x; $x++)
        echo $x;
?>

数据将使用JavaScript和PSOT方法post到test.php

例子
input
    test.php / post data : count=5
output
    01234

我想让JavaScript得到我的输出(01234)后张贴(count=5)到(test.php)位于外部服务器(www.domaine.com)

我基本上在c#中开发,但由于我不得不做一个跨平台的移动应用程序,我切换到JavaScript(不会使用Xamarin)为c#我能够做的一切使用WebBrowser,但不再在JavaScript中,是不是有任何对象等效于。net中的WebBrowser ?

我需要它的移动应用程序,将从GPS跟踪网站加载数据,API返回数据在XML和JSON

注意:我没有访问外部服务器

这里我将给你一个很好的例子,说明这些事情通常是如何管理的。
然而,掌握它的含义取决于您和您的编程经验。

html和js示例:

<form action="" id="formId" method="post" accept-charset="utf-8">
    <label for="inputNumber">Input something: </label>
    <input type="number" id="inputNumber" name="count"></input>
</form>
<span id="submit">Submit</span>
<script>
var getPhpResponse = function( data ) {
    console.log("manage php response HERE");
}
$("#submit").click(function(){
    $("#formId").submit();
});
$(document).ready(function () {
        $("#formId").bind("submit", function (event)
        {
            $.ajax({
                async: true,
                data: $("#formId").serialize(),
                success: function(data, textStatus) {
                    getPhpResponse( data )
                },
                type:"POST",
                url:"name/and/location/of/php/file.php"
            });
            return false;
        });
});
</script>

file.php例子:

<?php
$x = $_POST['count'];
echo '{"response":"';
for ($i = 0; $i < $x; $i++)
{
    echo $i;
}
echo '"}';

Poxriptum:

  1. 应该有进一步的输入验证,现在还不能相信type="number"
  2. 提交按钮是span而不是input是一个个人选择,只是为了造型目的而有所不同。
  3. 你应该好好学习AJAX和JSON。
  4. 考虑使用PHP框架,比如CakePHP;它可能对你有好处。
  5. 此答案假设您可以访问服务器。如果你不知道,那么你应该阅读API文档,而不是在没有详细说明你在谈论哪个API的情况下问SO问题。
编辑:

$less的版本

<form action="" id="formId" method="post" accept-charset="utf-8">
    <label for="inputNumber">Input something: </label>
    <input type="number" id="inputNumber" name="count"></input>
</form>
<span id="submit">Submit</span>
<script>
document.getElementById("submit").onclick = function () {
    var url = 'name/and/location/of/php/file.php';
    var userInput = encodeURIComponent(document.getElementById("inputNumber").value);
    var data = "count=" + userInput;
    makeRequest( data, url );
};
var getPhpResponse = function( data ) {
    console.log("manage php response HERE");
    console.log(data);
    parsed = JSON.parse(data);
    console.log(parsed);
}
var xhr = new XMLHttpRequest();
var makeRequest = function( data, url ) {
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
    xhr.send(data);
};
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function()
{
    if ( xhr.readyState == 4 )
    {
        if ( xhr.status == 200 || window.location.href.indexOf("http") == -1 )
        {
            getPhpResponse(xhr.responseText);
        }
        else
        {
            console.log("Manage error here");
        }
    }
}
</script>