在phonegap内使用php表单 [英] Using php form inside phonegap

查看:112
本文介绍了在phonegap内使用php表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求帮助获取我的PHP表单工作在phonegap ipa里面。我做的正是因为我被告知,一切工作很好,甚至在ipa内。只有问题是,我得到这个警报正在页面加载的时刻,显然它应该显示,当客户忘记填写一个必填字段。

I have asked before for help to get my php form working inside an phonegap ipa. I did exactly as I was told and everything works just fine, even inside the ipa. Only problem I have is that I get this alert right at the moment when the page loads, obviously it should show up when client forgets to fill in a required field.

在这里是我做的:

在contact.html中的表单

FORM in contact.html

 <form action="http://mobile.alicante-intermedia.com/submit_contact.php" method="get">
          <div class="form-element">
        <label for="txtfullname">Firstname</label>
        <input id="txtfullname" name="FirstName" type="text" placeholder="required" required />
      </div>
      <div class="form-element">
        <label for="txtemail">Lastname</label>
        <input id="txtemail" name="LastName" type="text" placeholder="required" required  />
      </div>
      <div class="form-element">
        <label for="txtcontact">Email</label>
        <input id="txtcontact" name="Email" type="email" placeholder="optional" />
      </div>
      <div class="form-element">
        <label for="txtmessage">Message</label>
        <textarea id="txtmessage" name="MessageText" placeholder="required" rows="5" required ></textarea>
      </div>
      <input type="button" onclick="submit()" value="submit contact"/>
      </form>

然后我创建了一个jquery_form.js文件,只有conatct.html

Then I created a jquery_form.js file which loads only inside the conatct.html

$.post('http://mobile.alicante-intermedia.com/submit_contact.php', {

    // These are the names of the form values

    FirstName: $('#FirstName_input').val(),
    LastName: $('#LastName_input').val(),
    Email: $('#Email_input').val(),
    MessageText: $('#MessageText_input').val()

    // HTML function

    }, function (html) {
        // Place the HTML in a astring
        var response=html;

        // PHP was done and email sent
        if (response=="success") {
            alert("Message sent!"); 
        } else {

            // Error postback
            alert("Please fill all fields!"); 
        return false;
    }
});

而php看起来像这样:

And the php looks like this:

<?php

    // VARS
    $FirstName=$_GET["FirstName"];
    $LastName=$_GET["LastName"];
    $Email=$_GET["Email"];
    $MessageText=$_GET["MessageText"];
    $Headers = "From:" . $Email;

    //VALIDATION
    if(
    $FirstName=="" ||
    $LastName=="" ||
    $Email=="" ||
    $MessageText==""
    ) {
        echo "Error";
    } else {
        mail("myemail@email.com","mobile app message",$MessageText, $Headers);
        echo "Success";
    }
?>

一切正常,除了提醒屏幕。

Everything works fine except the alert screen. Anyone here who has an idea what went wrong?

推荐答案

您的JavaScript代码是裸露的,不包含在任何函数或附件中到任何事件处理程序,因此在加载后立即执行 - 因此,当第一次解析jQuery脚本时,它立即发布一个空表单。

Your JavaScript code is "bare", not wrapped in any function or attached to any event handler, and therefore executes as soon as it is loaded - so it immediately posts an empty form when the jQuery script is first parsed.

将它放入 onclick 提交按钮的事件处理程序:

Place it into the onclick event handler for the submit button:

// When the document has loaded...
$(document).ready(function() {
  // Bind this action as a function to be executed when the button is clicked...
  $('input[type="button"][value="submit contact"]').click(function() {
    $.post('http://mobile.alicante-intermedia.com/submit_contact.php', {

      // These are the names of the form values
      // EDIT: You have the wrong ids on these...

      FirstName: $('#txtfullname').val(),
      LastName: $('#txtemail').val(),
      Email: $('#txtcontact').val(),
      MessageText: $('#txtmessage').val()

      // HTML function

      }, function (html) {
          // Place the HTML in a astring
          var response=html;

          // PHP was done and email sent
          if (response=="success") {
            alert("Message sent!"); 
          } else {

            // Error postback
            alert("Please fill all fields!"); 
            return false;
          }
    });
  });
});

由于它在JavaScript代码中绑定,请删除 onclick 从您的标记中的按钮:

Since it is bound in the JavaScript code, remove the onclick from the button in your markup:

<input type="button" value="submit contact"/>



编辑:



正在查找 $ _ GET 中的值,但是您已经从jQuery发布它们。而是在 $ _ POST

The PHP you have is looking for values in $_GET, but you have posted them from jQuery. Look instead in $_POST.

$FirstName=$_POST["FirstName"];
$LastName=$_POST["LastName"];
$Email=$_POST["Email"];
$MessageText=$_POST["MessageText"];

这篇关于在phonegap内使用php表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆