【问题标题】:Batch input - Send textarea content to a hidden field before the final submit批量输入 - 在最终提交之前将 textarea 内容发送到隐藏字段
【发布时间】:2011-09-10 03:18:31
【问题描述】:

我发布这个是为了寻求帮助,它可能对其他人有用。我不清楚如何做到这一点,或者是否有可能。帖子内容来自文本区域,我想将内容重新创建到隐藏字段中以发布到另一个页面。起初以为可以用 Ajax 完成,但不确定。

if(isset($_POST["submit"]))
{
$total = $_POST["textarea"];    
if($total != "") 
    {                       
        $pieces = explode("\n",$total);         
        $i=0;
        foreach($pieces as $piece)
        {               

        $pieces2 = $piece;  
        $piece3 = array_map('trim', explode(" ", $pieces2));

        // gets the 1st offset in the array
            if(strpos($piece3[0], 'http') !== FALSE)
            {       
                $output .= '<input type="hidden" name="url['.$i.']" value="'.$piece3[0].'" />'."\r\n";
            }

// no links in the textarea box then error message                  
    }else{
        $output .= "NO READABLE IMPORT<br>";
    }

}

至于 html。此表单通过第 2 步发送到另一个页面。就像这样。

    <form name="batch_import" action="addurls.php?step=2" method="post">            
            <textarea id="textarea" cols="100" rows="10"></textarea>
            <?php echo $output; ?>  
            <input type="button" name="submit" value="Import" class="inputbutton" />

        </form>

【问题讨论】:

    标签: php javascript ajax curl


    【解决方案1】:

    您正在尝试在服务器端执行此操作?那么 page1 发布到 page2,然后通过 AJAX 发布到 page 3?这是你想要做的吗?您绝对可以在 DOM 中添加一个隐藏字段并将 textarea 数据复制到其中,然后通过 AJAX 将其发送到另一个页面,但为什么不这样做:

    1) 在 Page1 上创建一个隐藏字段 2)使用jQuery或JavaScript将数据复制到隐藏字段 3) 在将表单提交到第 2 页之前,先通过 AJAX 提交到第 3 页。

    是这样,还是我错过了什么?也许让我知道你想要完成什么,我可以更好地回答。

    【讨论】:

    • 我的想法与使用 jquery 获取 textarea 内容类似。然后循环内容以在将帖子字符串提交到其他页面之前使用 PHP 创建一个查询字符串。然后,我应该获取 document.getElementById() 而不是使用 textarea 提交表单。出于这个原因,我从 textarea 中删除了 name 属性,这样它就不会提交无意义的数据。
    【解决方案2】:
    $total = $_POST["textarea"]; 
    //.....
    <textarea id="textarea" cols="100" rows="10"></textarea>
    

    为了使 POST 数据对 PHP 可用,textarea 需要具有 name-attribute "textarea"(发送表单时 id-attribute 没有意义,只有带有name-attribute 将被发送)

    【讨论】:

    • 感谢您的帮助。我根本不想发送文本区域。我想用文本区域中的每一行填充隐藏字段。所以数组中可能有很多隐藏字段。
    • 而数组来自$_POST["textarea"],如果不是来自发送的textarea,这应该来自哪里?
    猜你喜欢
    • 2023-02-09
    • 2012-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    相关资源
    最近更新 更多