doucaishou0074 2013-06-18 15:24
浏览 41
已采纳

动态地用$ _POST ['city']替换<input name =“city”type =“text”/>

I've been two days searching how to do this. I have this form:

<form action="preview.php" method="post">
In the city of <input name="city" type="text" /> at <input name="days" type="text" /> days of <input name="month" type="text" /> gathered the following people: <input name="name1" type="text" /> and <input name="name2" type="text" /> with the objective of...

<button type="submit">Preview</button></form>

And preview.php should have this:

In the city of <?php echo $_POST['city'];?> at <?php echo $_POST['days'];?> days of <?php echo $_POST['month'];?> gathered the following people: <?php echo $_POST['name1'];?> and <?php echo $_POST['name2'];?> with the objective of...

The thing is the form is created via CMS so I have no way of knowing what the names of the inputs will be. Is there a way to dinamically replace, for example, <input name="city" type="text" /> with <?php echo $_POST['city'];?>? There are some thing I have in mind but since I'm new to PHP I don't know how to implement them.

Maybe preg_replace could do it but I don't know how to prevent the name of the input from changing. Or maybe I could use an array, for example <input name="data[]" type="text" /> and something like:

for($i=0;"";$i++){

if( isset( $_POST["data{$i}"] )) {

$string = $form;
$patterns = '<input name="data[]" type="text" />';
$replacement = $_POST["data{$i};
preg_replace($patterns, $replacements, $string);

}

}

I'm really lost here and I'd appreciate it if someone could help me with this.

PS: I'm not a native english speaker son I'm sorry if I made some mistake.

UPDATE: The user can make different forms in the CMS that will be saved in a database. Then he can choose which one he wants to fill. Once he fills it he can preview it and save it as a pdf. The preview.php will have the same text in the form but instead of the inputs it will have the value that the user entered.

  • 写回答

1条回答 默认 最新

  • drxzpo70788179614 2013-06-18 15:41
    关注

    (I have been a bit out of touch with PHP.)

    Capture the HTML of the form with ob_ functions (output buffer).

    if ($_SERVER['REQUEST_METHOD'] == 'POST' {
    
        ob_start();
    
        ... the CMS outputs HTML
    
        ob_end_flush();
        $s = ob_get_contents();
    

    Use the name attribute to replace things

        function postText($matches) {
           return $_POST[$matches[1]];
        }
    
        $s = preg_replace_callback('/<[^>]* name="([^">]+)"[^>]*>/',
            "postText",
            $s);
        echo $s;
    

    This uses a function for replacing a HTML tag with a name attribute.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用