How to use 2 Submit separately in html?

Asked

Viewed 809 times

0

Hello, I’m making an upload system, but when I give Submit in the following code both exchange the value of the variable ID.

My idea is to change the ID value so that the folder that will be uploaded will change, as can be seen in the 1st echo, while in the 2nd echo shows the upload button itself.

I want only the 1st echo to change the ID value when I use Submit.

echo "<form action='' method='POST' enctype='multipart/form-data'>
    <input type='text' name='ID' id='ID' placeholder='INSIRA O ID'>
    <input type='submit' value='confirmar' id='ID'>";
    $ID = $_POST['ID'];
    echo "<form action='' method='POST' enctype='multipart/form-data'>
    <input type='file' name='allfiles' />
    <input type='submit'/>
    </form>";

  • I don’t understand why you have two form and only one of them is closed. You can explain ?

  • because they were together previously, but this does not interfere in this case for what I tested.

  • I tested your code here, and it’s working normal. Or I didn’t quite understand your question. I fill in for example '12' in the form, and that’s what var $ID received, not exchanged for another value.

  • yes 12 is successfully received, but after entering 12 and using the send button of the other form that in the case of upload it changes the ID value again which was not supposed to happen.

  • You tried to give the attribute name different for each form of this?

  • yes, I just tried to replace Submit with the button and it didn’t work either.

Show 1 more comment

2 answers

1


I split into 2 forms different. I think this solves your problem:

    echo 
    "<form action='' method='POST' enctype='multipart/form-data'>
        <input type='text' name='ID' id='ID' placeholder='INSIRA O ID'>
        <input type='submit' value='confirmar' id='ID'>   
    </form>";
    $ID = $_POST['ID'];
    echo 
    "<form action='' method='POST' enctype='multipart/form-data'>
        <input type='hidden' name='ID' value='{$ID}'>
        <input type='file' name='allfiles' />
        <input type='submit'/>
    </form>";
  • did not understand the purpose of this input line Hidden

  • She takes the $ID that came from the first form and keeps in the second form in a hidden field. You tested the code ?

  • yes, it worked, thank you!

0

You need to close the first </form>because the way this code of yours is all one form, and when you click on any Ubmit all data is submitted.

When you click on the confirmation of the 1st form it generates a POST, we will call it POST_1, and when the 2nd form is submitted, we will call POST_2, POST_1 no longer exists and its data also does not, what exists now is POST_2. It’s as if you update the page after submitting the form, that is, the memory is recreated from scratch.

What you can do: Save somewhere (BD or SESSION) the information from id which comes in POST_1 and retrieves it to use in POST_2.

Send posts to different php pages, which would have the need to save the id the same way.

Send everything in one form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.