简体   繁体   中英

How can I submit a form which is inside a parent form? PHP

I have a parent form which "saves" the settings and changes user puts. But I also have a logo upload form inside the parent form.

When I try to upload the logo and submit my upload it seems that my form does not process anything. If i put the upload form outside the parent form, it works.

I can't process forms which are under forms in php? :S

<form action="" method="post">

....

My upload form:

<form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
            <label for="async-upload">Upload</label>
            <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload">
            <?php wp_nonce_field('client-file-upload'); ?>
            <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
</form>
</form>

Does not work.

    <form id="file-form" enctype="multipart/form-data" action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="POST">
        <label for="async-upload">Upload</label>
        <input type="file" id="async-upload" name="async-upload"> <input type="submit" value="Upload" name="html-upload">
        <?php wp_nonce_field('client-file-upload'); ?>
        <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
</form>

    <form>

    ...

    </form>

works.

Why?

表单元素不能嵌套:

<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->

您不能嵌套表单,请Just a try为什么不删除其中的表单,并在“提交”按钮中传递URL的作用。

You should first try to find the reason as to what makes you nest a form inside a form. Form elements should not be nested. Once you are wid a reason try to find a alternate approach for what you want to achieve.

You cant/shouldnt nest forms - to accomplish what you are trying to achieve either:

  1. Spin out the upload into a seperate form and use AJAX to handle the file movements/result/response to prevent a page reload and to show whatever effect you want before the user submits the main form

  2. Enclose the file upload form in another page, and reference it in an iframe.

Out of the two, the first is the better course of action.

If you "think" you need to nest a form inside another, your probably need to adjust your thinking.

There is no need to direct a form to a page other than the form action states. you can manage the all of the submits inside one script.

Submit the form to the file and then separate the posted data and link the submit buttons to their associated inputs using logic.

i will give you a brief example, all form elements, inputs and button need a unique name.

<?php
    // setup the filterd for each field, what's expected, what to sanitize etc
    $filters = ['input_name'        => [array, of, filters],
                'another_name'      => [another, array, of, filters],
                'and_another_name'  => [yet, another, array, of, filters]
               ];
    // Pull the filtered $_POST var into your application.
    $posted = filter_var_array(INPUT_POST, $filters);

    // check the post actually has been submitted
    if (!empty($posted)) {

        // check this button was submitted
        if (!empty($posted['sumbit_button_name'])) {

            // assign associated post date to $var
            $var = $posted['input_element_name'];

        } elseif (!empty($posted['another_submit_button'])) {

            // assign more vars as needed
        } else {
            // must be the main button submitted the form.
            // associated vars set here.

        }
    }

If you have to do different things with the data set later on in the code then instantiate a var within each case of the elseif block. EG $test = 1; $test = 2; that you can check against further into the script, Or just do your different actions within the elseif cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM