连接到HTML5应用程序的外部服务器

问题描述:

我正在使用一个表单来提交和更新位于外部服务器上的数据库.当我将所有文件放在同一服务器(PHP和html)上时,我可以进行更新而没有任何问题.但是,当我将其分离出来时,仅将php文件留在服务器上并从计算机上操作html文件时,我将无法再进行更新.

I am using a form to submit and update my database sitting on an external server. When I place all the files on the same server (PHP and html), I am able to update without any issues. But when I separate it out, leaving only the php file on the server and operating the html files from my computer, I am no longer able to update.

当我单击表单的提交时,JavaScript中的第一个 alert("submit") 都不会触发.我无法将所有文件保留在服务器上,因为html部分将被转换为HTML5应用程序.无论如何,我可以解决这个使文件分开的问题.

The first alert("submit") in the JavaScript doesn't even fire off when I click submit for the form. I can't keep all the files on the server as the html part is to be converted to a HTML5 app. Is there anyway I can resolve this being abel to keep the files separate.

HTML代码

<html>
    <head>
        <title></title>
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    </head>
    <body>
        <form method="post" id="infoForm">
            <input type="text" name="first_name" id="first_name" value="" placeholder="First Name"  />
            <input type="text" name="last_name" id="last_name" value="" placeholder="Last Name"  />   
            <input type="text" name="email" id="email" value="" placeholder="Email"  />
            <button type="submit">Submit</button> 
        </form>

        <script>
            $('#infoForm').submit(function() {
                alert("submit");
                var postTo = 'http://www.examplesite.com/add.php';

                $.post(postTo,({first_name: $('[name=first_name]').val(), last_name: $('[name=last_name]').val(), email: $('[name=email]').val()}),
                function(data) {
                    alert("data is " + data);
                    if(data != "") {
                        // do something
                    } else {
                        // couldn't connect
                    }
                    },'json');
                return false;

            });
        </script>
    </body>
</html> 

PHP代码:

<?php

$server = "localhost";
$username = "user";
$password = "pass";
$database = "db";

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);

$firstname = mysql_real_escape_string($_POST["first_name"]);
$lastname = mysql_real_escape_string($_POST["last_name"]);
$email = mysql_real_escape_string($_POST["email"]);

$sql = "INSERT INTO personnel (first_name, last_name, email) ";
$sql .= "VALUES ('$firstname', '$lastname', '$email')";

if (!mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
} else {
    echo "Comment added";
}

mysql_close($con);

您说的是警报未触发.检查jQuery是否正确加载. 从本地主机运行时尝试更改:

You saying that even alert is not firing. Check if jQuery is loaded properly. Try changing while runing from localhost:

//code.jquery.com/jquery-1.11.3.min.js 到 http://code.jquery.com/jquery-1.11.3.min.js

//code.jquery.com/jquery-1.11.3.min.js to http://code.jquery.com/jquery-1.11.3.min.js

这是因为本地提供服务的浏览器不知道应该从Web加载外部库.明确设置协议将使他不在文件系统中搜索它们,而是通过url加载.

That is because browser serving files localy doesn't know that it should load external libraries from web. Explicitly setting protocol will make him search for them not in the filesystem, but load via url.