简体   繁体   English

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

[英]Connecting to external server for HTML5 app

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. 当我将所有文件放在同一服务器(PHP和html)上时,我可以进行更新而没有任何问题。 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. 但是,当我将其分离出来时,仅将php文件留在服务器上并从计算机上操作html文件时,我将无法再进行更新。

The first alert("submit") in the JavaScript doesn't even fire off when I click submit for the form. 当我单击提交表单时,JavaScript中的第一个alert("submit")甚至不会触发。 I can't keep all the files on the server as the html part is to be converted to a HTML5 app. 我无法将所有文件保留在服务器上,因为html部分将被转换为HTML5应用程序。 Is there anyway I can resolve this being abel to keep the files separate. 无论如何,我可以解决这个使文件分开的问题。

HTML Code 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 Code: 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);

You saying that even alert is not firing. 您说即使警报也不会触发。 Check if jQuery is loaded properly. 检查jQuery是否正确加载。 Try changing while runing from localhost: 从本地主机运行时尝试更改:

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

That is because browser serving files localy doesn't know that it should load external libraries from web. 那是因为浏览器本地提供文件,不知道它应该从Web加载外部库。 Explicitly setting protocol will make him search for them not in the filesystem, but load via url. 明确设置协议将使他不在文件系统中搜索它们,而是通过url加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM