3

之前曾寻求帮助以使我的 php 表单在 phonegap ipa 中工作。我完全按照我被告知的那样做,一切都很好,即使在 ipa 内也是如此。我唯一的问题是我在页面加载的那一刻得到了这个警报,显然它应该在客户端忘记填写必填字段时出现。

这是我所做的:

contact.html 中的 FORM

 <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 中加载

$.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看起来像这样:

<?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";
    }
?>

一切正常,除了警报屏幕。这里有人知道出了什么问题吗?

4

1 回答 1

4

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

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

// 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从标记中的按钮中删除:

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

编辑:

您拥有的 PHP 正在寻找 中的值$_GET,但您已从 jQuery 发布它们。而是在$_POST.

$FirstName=$_POST["FirstName"];
$LastName=$_POST["LastName"];
$Email=$_POST["Email"];
$MessageText=$_POST["MessageText"];
于 2012-10-03T15:05:20.123 回答