doufan9395 2015-06-18 19:43
浏览 19
已采纳

同时更新不同的表单

Current Situation

So, this is a follow up question from my previous question (Submit button to affect multiple files)

The approach was good in terms of how to "click" multiple buttons together by using a "super-button" (patent pending) by @oMiKeY. It does what it is supposed to do, clicking all buttons.

The mark up is the following:

Title.php

    <form role="form" method="post">                    
        <div class="edit_title">
             <input type="hidden" value="<?php echo $post_id; ?>">
             <?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
            <div class="update-button-wrap">
                 <input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
            </div>  
        </div>                  
    </form>

Content.php

 <form role="form" method="post">                   
        <div class="edit_content">
            <?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
        </div>
        <div class="update-button-wrap">
            <input id="save_button" type="submit" name="update_product" value="<?php esc_attr_e( 'Update', 'site' ); ?>"/>
        </div>                          
</form>

Article.php

<button type='button' onclick="$('[type="submit"]').click()">Submit</button>

Problem

So, when the "Submit" button is clicked, both buttons in each title.php and content.php are also clicked (ie. in regards to clicking buttons, it works fine). However, because two forms are simultaneously clicked, only the second one is updated (either content or title) while the first one is ignored, when both data are needed to be updated.

My approach

Now, I can merge two files together and have both title and content within a single form, but that really messes up my overall setup and I heard it is better to have multiple smaller php files for updating and speed than a large one big file.

Or here is my another approach.

In the article.php, I will have the form and submit button while the title.php and content.php only has the editable forms. Then these two forms are somehow linked to the form in article.php, like the image below.

enter image description here

Do you think the second approach can be achieved or any other suggestions?

Thanks

  • 写回答

1条回答 默认 最新

  • doubi2228 2015-06-18 19:48
    关注

    Just add a global form on article.php and drop the title and content forms (and submit buttons). Every named input inside the global form will be submitted together no matter what php file generated them.

    Edit: Title.php

    <div class="edit_title">
         <input type="hidden" value="<?php echo $post_id; ?>">
         <?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
    </div>
    

    Content.php

    <div class="edit_content">
        <?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
    </div>
    

    Article.php

    <form role="form" method="post">
    <?php include "Title.php"; include "Content.php"; ?>
    <button type='submit'>Submit</button>
    </form>
    

    Option 2: Alternatively you could use some sort of functionality that allows you to create forms only once, even if you have "inner forms". This way Title.php and Content.php would also work as standalone code snippets.

    $formDeep = 0;
    function openForm() {
        global $formDeep;
        if ($formDeep == 0) {
            echo "<form role=\"form\" method=\"post\">";
        }
        $formDeep++;
    }
    
    function closeForm() {
        global $formDeep;
        $formDeep--;
        if ($formDeep == 0) {
            echo "</form>";
        }
    }
    

    Title.php

    <?php openForm(); ?>
    <div class="edit_title">
         <input type="hidden" value="<?php echo $post_id; ?>">
         <?php post_input_box( $post_id, 'post_title', array( 'placeholder' => 'Article title..', 'value' => $post->post_title ) ); ?>
    </div>
    <?php closeForm(); ?>
    

    Content.php

    <?php openForm(); ?>
    <div class="edit_content">
        <?php post_input_box( $post_id, 'post_content', array( 'placeholder' => 'Short description..', 'value' => $post->post_content ), 'textarea' ); ?>
    </div>
    <?php closeForm(); ?>
    

    Article.php

    <?php openForm(); ?>
    <?php include "Title.php"; include "Content.php"; ?>
    <button type='submit'>Submit</button>
    <?php closeForm(); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法