简体   繁体   English

通过AJAX将HTML表单发送到PHP服务器

[英]Sending HTML form via AJAX to PHP server

I'm working on phonegap, basically its like making mobileapps crossplatform by using HTML, JS and CSS. 我正在研究phonegap,基本上就像通过使用HTML,JS和CSS使mobileapps跨平台一样。 On the device i currently have the JS and the HTML (form) in same document. 在设备上,我目前在同一文档中具有JS和HTML(表单)。

What I'm trying to do is to pass email and password to my server, and then process it there through a login. 我想做的是将电子邮件和密码传递到我的服务器,然后通过登录名在那里处理它。 I've tested the login script on the server and it works with hardcoded data. 我已经在服务器上测试了登录脚本,该脚本可用于硬编码数据。 So I'm guessing somewhere when sending the data from the device its failing.. I'm fairly new to JS too. 所以我在从设备发送数据失败时在某个地方猜测。.我对JS还是很陌生。

I tried to hardcode the data in the AJAX but it doesnt seem to work. 我试图对AJAX中的数据进行硬编码,但它似乎不起作用。 Preferebly I would like to use something like var pdata = $('#form').serialize(); 我希望使用类似var pdata = $('#form')。serialize();的方法。 or something else if its better. 或者其他更好的东西。

Any ideas? 有任何想法吗?

EDIT: Forgot to say that the PHP on the server auto submits by using JS when $_POST is set (isset) 编辑:忘了说,设置$ _POST(isset)时,服务器上的PHP通过使用JS自动提交。

The form 表格

<form id="form" onsubmit="dologin()">
    <div class="form-group">
        <label for="email">Epost</label>
        <input type="email" class="form-control" name="email" value="" placeholder="Epost">
    </div>
    <div class="form-group">    
        <label for="password">Passord</label>
        <input type="password" class="form-control" name="password" value="" placeholder="Passord">
    </div>
    <div class="checkbox">
        <label>
        <input type="checkbox" name="remember_me">
        Husk meg
        </label>
    </div>
        <button type="submit" class="btn btn-primary">Logg inn</button>
</form>

The javascript JavaScript

<script>
        function dologin() {
            //var pdata = $('#form').serialize();
            //alert(pdata);

            $.ajax({
                type: 'POST',
                data: {email:"test@test.no",password:"test"},
                url: 'LINK',
                success: function(data) {
                    alert(data);
                },
                error: function() {                 
                    alert("error");
                } 
            });
            return false;
        };
</script>

The PHP 的PHP

<form id="form" method="post">
    <!-- {{ Form::label('email', 'Email Address') }} -->
    <div class="form-group">
            <input type="text" name="email" value="<?php if(isset($_POST["email"])) echo $_POST['email'];?>">
    </div>

    <div class="form-group">
        <!-- {{ Form::label('password', 'Password') }} -->
        <input type="text" name="password" value="<?php if(isset($_POST["password"])) echo $_POST['password'];?>">
    </div>
</form>

Are you able to hit your server via through phonegap? 您可以通过phonegap打服务器吗? If no then please check your config.xml for white list urls - change access control property to 如果否,请检查您的config.xml中是否有白名单网址-将访问控制属性更改为

access origin = "*" 访问来源=“ *”

Hopeful you will be able to hit your server with data. 希望您能够使用数据访问服务器。

You can use weinre to debug your app. 您可以使用weinre调试应用程序。 That way you will be able to see if the request was placed from the app or not. 这样,您将可以查看是否从应用程序发出了请求。 http://people.apache.org/~pmuellr/weinre/docs/latest/Home.html http://people.apache.org/~pmuellr/weinre/docs/latest/Home.html

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

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