【问题标题】:Echo back from PHP include file not working in IE从 PHP 包含文件回显在 IE 中不起作用
【发布时间】:2011-03-06 16:01:31
【问题描述】:

我有一个简单的表单,可以将注册信息发布到 mailchimp 帐户。从包含文件返回的表单和消息在 Firefox 中完美运行,但在 IE 中没有发生任何事情。如果没有输入电子邮件地址,它应该会返回一个错误,就像在 Firefox 中一样。我不知道如何解决此类问题。如果出现错误,它应该将错误回显到表单上的 div 中,该表单在 Firefox 中再次完美运行,在 IE 中没有任何反应。对我来说完全是无稽之谈。我需要在今天之前完成这项工作。我尝试将文件移动到与 index.php 相同的位置,更改权限等,但没有成功。我需要一些重要的帮助!我什至无法让这段代码在 IE 中返回: if(!$_GET['email']){ return "No email address provided"; }

您可以尝试以下网址:www.terrybryan.com/index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
        <title>Ajax Mailing List Sign Up System</title>
        <link type="text/css" rel="stylesheet" href="css/default.css" />

    </head>

    <body>
        <form id="signup" action="<?=$_SERVER['PHP_SELF']; ?>" method="get" style="width:250px;">
          <fieldset>

              <label for="email" id="email-label">Email Address</label>
              <input type="text" name="email" id="email" />


              <label for="zipcode" id="zipcode-label">Zip Code</label>
              <input type="text" name="zipcode" id="zip" />

               <label for="events" id="events-label">Receive future info from us?</label>
               Yes<input type="radio" name="events" value="Yes" checked="checked" />
               No<input type="radio" name="events" value="No" />

               <label for="hearabout" id="hearabout-label">How did you hear about us?</label>
               <select name="hearabout" id="hearabout">
<option value="Ice">Ice</option>
<option value="Radio">Radio</option>
<option value="Friend">Friend</option>
<option value="Door Hanger">Door Hanger</option>
</select>
            <br /><br />
              <input type="image" src="i/join.jpg" name="submit" value="Join" class="btn" alt="Join" />
              <br /><br />
            <div id="response">
                <? require_once 'store-address.php'; if($_GET['submit']){ echo storeAddress(); } ?>
              </div>
          </fieldset>
        </form>    

        <!-- Some fancy Ajax stuff to store the sign up info. If you don't want to use it, just delete it and the form will still work  -->

    </body>
</html>

这是返回消息的包含文件:

<?php
/*///////////////////////////////////////////////////////////////////////
Part of the code from the book 
Building Findable Websites: Web Standards, SEO, and Beyond
by Aarron Walter (aarron@buildingfindablewebsites.com)
http://buildingfindablewebsites.com

Distrbuted under Creative Commons license
http://creativecommons.org/licenses/by-sa/3.0/us/
///////////////////////////////////////////////////////////////////////*/
require_once('MCAPI.class.php');

function storeAddress(){

    // Validation
    if(!$_GET['email']){ return "No email address provided"; } 

    if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $_GET['email'])) {
        return "Email address is invalid"; 
    }


    // grab an API Key from http://admin.mailchimp.com/account/api/
    $api = new MCAPI('********');

    // grab your List's Unique Id by going to http://admin.mailchimp.com/lists/
    // Click the "settings" link for the list - the Unique Id is at the bottom of that page. 
    $list_id = "********";

    // Merge variables are the names of all of the fields your mailing list accepts
    // Ex: first name is by default FNAME
    // You can define the names of each merge variable in Lists > click the desired list > list settings > Merge tags for personalization
    // Pass merge values to the API in an array as follows
    $mergeVars = array('ZIPCODE'=>$_GET['zipcode'],
                        'EVENTS'=>$_GET['events'],
                        'HEARABOUT'=>$_GET['hearabout']);

    if($api->listSubscribe($list_id, $_GET['email'], $mergeVars) === true) {
        // It worked!   
        return 'Success! Check your email to confirm sign up.';
    }else{
        // An error ocurred, return error message   
        return 'Error: ' . $api->errorMessage;
    }

}

// If being called via ajax, autorun the function
//if($_GET['ajax']){ echo storeAddress(); }
?>

有人知道为什么这个简单的表单在 IE 中不能像在 Firefox 和其他浏览器中那样工作吗?

谢谢,

T 布莱恩

【问题讨论】:

  • 我建议尝试的一件事是不要使用简短的 php 标签。 IE。总是使用
  • 除此之外,化繁为简。浏览器的差异意味着 html 的问题,而不是 php 的问题(除非有短标签问题)。在 ie 中尝试一个简单的测试用例,例如在ie中检查回显,然后if得到回显。在其他一切正常之前,尽量不要涉及那些庞大而复杂的表单 html 代码。
  • 我不知道如何解决 PHP 的问题。如何在 IE 中测试回声?任何帮助将不胜感激。谢谢,
  • @Tchalvak,使用您的信息,我将其缩小到 IE 无法识别: if($_GET['submit']){ echo storeAddress(); } ?&gt; 它没有触发回声,因为它无法识别:if($_GET['submit']) 有没有更好的方法来做到这一点?
  • 哦,您可能还需要澄清您对 $_GET 与 $_POST 的理解。如果它没有显示在 url 中,那么它不是 $_GET。所以这可能是你的问题。我建议通常只使用 $_REQUEST。这样做的另一个好处是消除了 $_POST 是比 $_GET 更安全的用户输入的错觉,因为当您将它们放入 sql 时,两者都应该同样不受信任。

标签: php internet-explorer echo


【解决方案1】:

原因如下:在 IE 中使用图像按钮时,当您单击它时,它不会发送$_POST['nameofbutton'],而是发送$_POST['nameofbutton_x']$_POST['nameofbutton_y']。现在,Firefox 同时发送$_POST['nameofbutton_x']$_POST['nameofbutton_y'] 以及$_POST['nameofbutton']。 _x 和 _y 值存储您单击图像按钮的位置的 x 和 y 坐标,因此您可以根据用户单击的位置执行不同的操作,例如图像地图。

因此,当您测试 if($_GET['submit']){ echo storeAddress(); } ?&gt; 时,您不会在 IE 上得到它,因为 IE 没有 $_GET['submit'] 它有 $_GET['submit_x']$_GET['submit_y'],所以您必须测试 $_GET['submit_x'] 或 y。

至少我认为格式是 _x 和 _y,可能略有不同。简单的方法是print_r($_GET);

哦,顺便提一下,在表单中使用 Get 时要小心,特别是如果它们必须传输大量可能很长的信息,IE 在地址栏中有 255 个字符的限制,之后的任何内容都会被截断并遗漏了 $_GET 或格式错误。最好将 Post 用于表单。 Get 仅适用于为 CMS 控制器构建链接或注册电子邮件验证链接等等。

【讨论】:

    【解决方案2】:

    我建议编辑您的 html 代码以进行提交:

    发件人:

    <input type="image" src="i/join.jpg" name="submit" value="Join" class="btn" alt="Join" />
    

    收件人:

    <input type="submit" name="submit" value="Join" class="btn" alt="Join" />
    

    类型提交通常是可靠的,其他类型(包括,遗憾的是,图像提交等)通常在不同的浏览器中以不同的方式实现。另一件不依赖的事情是提交的内容,因为 ie 在某些情况下提交 html 而不是值(通常与标准提交值以外的东西),或者在页面上提交多个表单,诸如此类的烦人的事情。

    所以一般来说,最好使用简单的&lt;input type='submit' ...etc,并简单地检查提交的 -presence- 并且不要太具体地确定提交包含的内容。

    【讨论】:

      【解决方案3】:

      我建议一种非常简单的方法,使用 CSS 设置按钮样式,或者使用 &lt;a href="javascript:;" onclick="document.yourformname.submit"&gt;&lt;img src="path/to/your/image" alt="my image button" /&gt;&lt;/a&gt;

      【讨论】:

        【解决方案4】:

        由于 PHP 在服务器上运行,它应该(通常)为相同的输入向浏览器提供相同的响应。由于表单使用“get”方法,输入完全由 URL 指定(加上标头,包括 cookie,尽管此处似乎没有考虑在内)。

        无论如何,当您从 Firefox 提交表单时,您应该会看到表单提交的 URL 出现在 Firefox 地址栏中。如果 PHP 脚本发出重定向作为响应,则 URL 可能只会出现非常短暂的时间。无论哪种方式,它都应该在您的浏览器历史记录中可用。

        如果您可以将该 URL 复制到 IE 的地址栏中并从 IE 提交相同的请求(再次假设不涉及 cookie),那么您应该从服务器获得几乎相同的响应。如果您在 IE 的浏览器窗口中看到与在 Firefox 中看到的不同,可能有多种原因。

        1. 查看源代码。 (查看 -> 查看源代码)。如果它完全空白,则脚本可能无法正确执行。

        2. 如果您有权访问服务器错误日志,请检查与您的请求相关的错误消息。一个健全的 PHP 实现(与一个健全的服务器合作)将提供错误日志记录。有时这确实是了解脚本正在发生什么的唯一方法。如果您可以通过 FTP 帐户访问 Web 服务器,您可能会在顶级或靠近顶级的地方找到日志。

        3. 如果您无权访问服务器,您仍然可以通过使用Firebug LiteFiddler2 来了解您的请求发生的情况。

          李>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-09-22
          • 2016-11-11
          • 1970-01-01
          • 2016-02-17
          • 2014-05-15
          • 2015-02-05
          • 1970-01-01
          • 2010-10-20
          相关资源
          最近更新 更多