简体   繁体   中英

Second submit button with PHP outside of a form

I have a form with 2 mandatory fields and one optional.

I want to add a submit button outside of this form, so that when we have a value for folder_name and you click on it, it runs php and searches for all the files under that server:

<form name="runConsultation" method="post" action="">   
    <input type="text" name="folder_name">          
    <select name="files" size="3">
        <?php
            // pupulate the list with values found outside the form
        ?>
    <select>    
    <select name="category">
        <option value="">Select...</option>
        <option value="a">A</option>
        <option value="b">B</option>
    </select>  
    <input type="submit" name="run-consultation" value="RUN CONSULTATION">
</form>


  <input type="submit" name="find-files" value="FIND FILES">

My problem is that I don't know how to do that without using 2 forms.

If I add 2 forms, I need to separate folder_name from the of the fields and when I run the consultation, I lose access to folder_name.

Can anybody help? Thank you in advance.


Example :

<form name="fileSearch" method="post" action="" >
        <input type="text" name="folder_nr">                                                                            
        <input type="submit" value="Search For Files" name="search-folder">                                                                                                 
</form> 


<form name="runConsultation" method="post" action="">                           
    <select name="files" size="3">
       <?php
        // building my list with the files found using the first form
       ?>   
     ...
    <input type="submit" name="run-consultation" value="Run Consultation">
</form>

when I run seach-folder, folder_number disappears.

Just place the submit button inside the form and check, if the button is clicked

if (isset($_POST['find-files']) && $_POST['find-files']) {
    // find files..
}
else
{
    // run consulation
}

You can do this with PHP only or you can add some javascript + Ajax.

Clean HTML & PHP solution:

place 2 submit buttons inside the form, having the same name but different values.

<form method="POST">

    <!-- code omitted on purpose -->

    <input type="submit" name="btnSubmit" value="RUN CONSULTATION" />
    <input type="submit" name="btnSubmit" value="FIND FILES" />
</form>

Pressing each button will submit the form. On server side you can check which button was pressed by inspecting $_POST['btnSubmit] .

The value can be:

  1. empty - meaning the form was not submited
  2. 'RUN CONSULTATION'
  3. 'FIND FILES'

Dependding on which value you get, you either display the form initially (1), run the consultation (2), or display the form with the select populated with options (3).

Javascript + Ajax solution:

I'm not gonna give you the code but rather tell you what steps this involve:

  1. With javascript, monitor click event on "Find Files" button;
  2. prevent event default behaviour (ie submit)
  3. Do an AJAX request to the server and send the value of the folder_nr textfield.
  4. On the server, read the folder_nr and find the files. Return data with the files found
  5. (back on the client) Construct the select options with the data you get at step 4.

These are the steps. You can do this with plain Javascript or you can use any javascript library like jQuery.

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