【问题标题】:Convert all values in html file to MS-Word using PHP使用 PHP 将 html 文件中的所有值转换为 MS-Word
【发布时间】:2012-09-06 10:00:47
【问题描述】:

我有如下的 HTML 和 PHP 代码。我有代码一个文件html2word.php:

<form method="post" action="htmltortf.php">    
    <table>
        <tr>
            <td colspan="3">Convert html to doc</td>
        </tr>
        <tr>
            <td colspan="3">Choose the answer</td>
        </tr>
        <tr>
            <td><input type="radio"  name="004" value="1" <?=($_POST['004']=='1' ? 'checked="checked"' : '')?>/>1</td>
            <td><input type="radio"  name="004" value="2" <?=($_POST['004']=='2' ? 'checked="checked"' : '')?>/>2</td>
            <td><input type="radio"  name="004" value="3" <?=($_POST['004']=='3' ? 'checked="checked"' : '')?>/>3</td>
        </tr>
        <tr>
            <td colspan="3"><input type="submit" name="submit" value="submit" /></td>
        </tr>
    </table>
    <?php
        if(isset($_POST['submit']))
        {
            header("Content-type: application/vnd.ms-word");
            header("Content-Disposition: attachment;Filename=html2doc.doc");
        }
     ?>    
</form>

我需要什么:

当用户选中单选框时,当我将其转换为 MS-Word 时,它也会显示单选框被选中。

问题:

当我将它转换为 word 文档时,它会显示我选中的单选框,但它会显示错误消息:

Notice: Undefined index: 004 in C:\wamp\www\html to docv2\htmltortf.php on line 51.

我该如何解决这个问题?

【问题讨论】:

    标签: php cakephp cakephp-2.0 cakephp-2.1


    【解决方案1】:

    当用户点击提交按钮时,表单数据发送到服务器。它存储在 $_POST 数组中,因此当您显示此页面时,您必须提前检查是否有任何数据接收器。
    尝试类似的方法(用此代码替换单选按钮 html):

    <td><input type="radio"  name="004" value="1" <?=((isset($_POST['004']) && $_POST['004']=='1') ? 'checked="checked"' : '')?>/>1</td>
    <td><input type="radio"  name="004" value="2" <?=((isset($_POST['004']) && $_POST['004']=='2') ? 'checked="checked"' : '')?>/>2</td>
    <td><input type="radio"  name="004" value="3" <?=((isset($_POST['004']) && $_POST['004']=='3') ? 'checked="checked"' : '')?>/>3</td>
    

    查看完整解决方案: http://codepad.org/QThcTGdO

    【讨论】:

    • 非常感谢,但它会显示错误消息Notice: Undefined index: 004 in C:\wamp\www\html to docv2\htmltortf.php on line 75。当我将其转换为 Word 文档时它可以工作。
    • 此通知不是严重错误。这个可爱的因为我不检查isset($_POST['004'])。如果你给我看你的 75 行,我会更准确地说你。
    • 这是第 75 行的代码 &lt;td&gt;&lt;input type="radio" name="004" value="1" &lt;?=($_POST['004']=='1' ? 'checked="checked"' : '')?&gt;/&gt;1&lt;/td&gt;。谢谢,
    • 我使用这个它可以工作,但是当我选中一个单选框并将其转换为 Ms 字时,它会显示所有单选框都已选中。我该如何解决这个问题?谢谢。
    • 老兄,如果你只是将该代码复制到所有单选按钮......所以我想你不明白它是如何工作的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    相关资源
    最近更新 更多