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"&g开发者_Python百科t;
<?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?
Form elements may not be nested:
<!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form -->
You can't nest form, Just a try
why don't you remove the form which is inside and pass the action of the URL in the submit button.
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:
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
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.
精彩评论