0

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.

4
  • Please explain why you cant use 2 forms, as this is the obvious solution. Commented Jul 10, 2014 at 11:21
  • Edited my post and put an example Commented Jul 10, 2014 at 11:29
  • use <input type="text" name="folder_nr" value="<?php echo @$_REQUEST['folder_nr'];?>"> Commented Jul 10, 2014 at 11:32
  • I think ajax will be a good solution. Just merge two forms as one. On the blur event of folder_nr fire ajax request and do the things you need to. Commented Jul 10, 2014 at 11:32

2 Answers 2

1

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 } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked! I don't know why I had the impression that you can only use have submit button in a form... Probably because I'm a noob :) Thanks again
0

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 (i.e. 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.

3 Comments

When I evaluate this with an if: if ($_POST['btnSubmit] == "RUN CONSULTATION") , and reload the page, I get: Notice: Undefined index. Am I doing something wrong?
You get that because first time there is no index in $_POST with that value. You should check first if $_POST['btnSubmit'] is set.
Like this: if (isset($_POST['btnSubmit']) && $_POST['btnSubmit] == "RUN CONSULTATION")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.