473,469 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dynamic Conditions/Criteria in PHP form

67 New Member
I have this:
Expand|Select|Wrap|Line Numbers
  1. SELECT $items FROM $table WHERE $conditions
in my form I'm asking the user to select items from a list - multible selections
and I'm asking the user to select from which table he/she would like to extract the data

now my problem with the conditions selection ! (CRITERIA)

here's an image of what I'm looking for:

http://img76.imageshack.us/my.php?image=criteriafz2.jpg
Aug 11 '07 #1
18 3130
coool
67 New Member
I'm wondering how can i by a click have a new criteria line

then all criteria lines should be saved to thier variables ! so i can use them later inside a sql query statement after the word "WHERE"

do i have to use java script ! .. or do i have to use php functions ! or multiple forms submitions !! ..
Aug 11 '07 #2
pbmods
5,821 Recognized Expert Expert
Heya, coool.

To actually create the inputs, you'll need to use JavaScript.

But if you name them criteria[], field[], etc., then PHP will automatically create arrays so that you don't have to keep track of individual variable names.

For more information, check out this page:
http://php.net/manual/en/language.va....php#id2536797
Aug 11 '07 #3
coool
67 New Member
well, I've tried the technique of hidden elements

because I only want 10 criteria rows

so I displayed one.. and make 9 rows hidden using CSS

now when user click on "ADD CRITERIA ROW"

there should be a javascript function that change the element to display

my problem now with this javascript function :(

i'm using <tr> not <div> ----- becuase with <div> i couldn't hide using css - i don't know why !

so do you have any javascript function that change <tr> to show !
Aug 12 '07 #4
kovik
1,044 Recognized Expert Top Contributor
CSS works on <tr> and <div>. Changing the class of a <tr> or <div> with JavaScript is done the same way.

Expand|Select|Wrap|Line Numbers
  1. element.className = 'foo';
Aug 12 '07 #5
pbmods
5,821 Recognized Expert Expert
Heya coool.

Check this out:
Expand|Select|Wrap|Line Numbers
  1. <form id="thePfhorm" ...>
  2. </form>
  3.  
  4. <div id="dolly" style="display:none">
  5.     <input name="criteria[]" ... />
  6.     .
  7.     .
  8.     .
  9.     <input type="button" value="Remove" onclick="this.parentNode.parentNode.removeChild(this.parentNode);" />
  10. </div>
  11.  
  12. <input type="button" value="Add" onclick="addInput();" />
  13.  
  14. <script type="text/javascript">
  15. // <![CDATA[
  16.     // Add the first set of inputs.
  17.     addInput();
  18. // ]]>
  19. </script>
  20.  
Expand|Select|Wrap|Line Numbers
  1. function addInput()
  2. {
  3.     var $newElement = document.getElementById('dolly').cloneNode(true);
  4.     $newElement.id = '';
  5.     $newElement.style.display = '';
  6.  
  7.     document.getElementById('thePfhorm').appendChild($newElement);
  8. }
  9.  
Instead of creating all the inputs up front (which clutters up your form values when submitting and limits the total number of inputs), you instead can clone a template and dynamically create exactly as many or as few as you need.
Aug 12 '07 #6
coool
67 New Member
what do you think of this code? - it's not working what's wrong with it !

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function Show(e)
  3. {
  4.         if(document.getElementById(e).style.display == 'none')
  5.         {
  6.                 document.getElementById(e).style.display = 'block';
  7.         }
  8. }
  9. </script>
  10.  
  11. <tr id='criteria1'>
  12.         <?echo CriteriaRow();?>
  13.  //this function have some form elements/html similar to the picture in one of my posts
  14. </tr>
  15.  
  16.               <input type="button" value="Display Criteria" onClick="Show('criteria1');">
  17.  
Expand|Select|Wrap|Line Numbers
  1. #criteria1
  2. {
  3. display: none;
  4. }
  5.  
when i click on the button.. nothing appear !
Aug 12 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, coool.

Try removing the conditional from your Show() function.

P.S. instead of using '<?', consider using '<?php', as that will make your code more portable. "Short tags", as they are called, have been deprecated; not all servers recognize them, and they will be removed from PHP in a future release.
Aug 12 '07 #8
coool
67 New Member
You are really SMART

It's WORKING

I feel I wonna CRY ...... THAAAAAAAAANKS VeRy MuCh :)
Aug 12 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, coool.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 12 '07 #10
coool
67 New Member
oh I've a little question..

why the if statement didn't work ! .. I really need to use it

because I'm going to hide 9 <tr>s

and with every click I need to show one of them

------

I tried this code.. and i know it's wrong ! ..

[html]

<tr id='row2'>
<?php echo newRow(); ?>
</tr>
<tr id='row3'>
<?php echo newRow(); ?>
</tr>
<tr id='row4'>
<?php echo newRow(); ?>
</tr>

<input type="button" value="Add Row" onClick="Show('row');">
[/html]

[php]
<script>
function Show(e)
{
var i;
i = 2;

while(document.getElementById(e+i).style.display != 'none')
{
document.getElementById(e+i).style.display = 'block';
else if(document.getElementById(e+i).style.display == 'block')
i++;
}
}
</script>
[/php]
Aug 13 '07 #11
pbmods
5,821 Recognized Expert Expert
Heya, coool.

The display property of a visible element is not always 'block', but it will always be 'none' for an invisible element.

But in reality, if you know that the element is supposed to end up with a display of 'block', it does no harm to set it anyway, even if that element is already visible.

Consider:
Expand|Select|Wrap|Line Numbers
  1. function Show(e)
  2. {
  3.         var i = 2;
  4.         while(var element = document.getElementById(e+i))
  5.         {
  6.                 element.style.display = 'block';
  7.                 ++i;
  8.         }
  9. }
  10.  
Aug 13 '07 #12
coool
67 New Member
Thanks for your help :)

things are working just as i want :D
Aug 14 '07 #13
pbmods
5,821 Recognized Expert Expert
Heya, coool.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Aug 14 '07 #14
BigM
10 New Member
pbmods,

Thanks for that excellent script, just wanted to add though, there were two bits with extra spaces in it that stopped it working, drove me nuts before checking with the javascript console!!

Here's a working version below for anyone else who comes across this:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.         <script type="text/javascript">
  4.             function addInput(){
  5.                 var $newElement = document.getElementById('dolly').cloneNode(true);
  6.                 $newElement.id = '';
  7.                 $newElement.style.display = '';
  8.                 document.getElementById('thePfhorm').appendChild($newElement);
  9.             }
  10.         </script>
  11.     </head>
  12.     <body>    
  13.         <form id="thePfhorm"> 
  14.         </form>
  15.  
  16.         <div id="dolly" style="display:none">
  17.             <input type="text" name="criteria[]">
  18.             <input type="button" value="Remove" onclick="this.parentNode.parentNode.removeChild(this.parentNode);">
  19.         </div>        
  20.         <br>
  21.         <input type="button" value="Add" onclick="addInput();">
  22.  
  23.         <script type="text/javascript">
  24.             // <![CDATA[
  25.             // Add the first set of inputs.
  26.             addInput();
  27.             // ]]>
  28.         </script>
  29.     </body>
  30. </html>
  31.  
JM
Aug 31 '07 #15
BigM
10 New Member
Sorry guys, could I ask a follow up question on this.

Once you've got this working, how do you harvest the entered info to post to a database?

I've been playing around with some different php ideas to do it but no luck so far.
Aug 31 '07 #16
ronnil
134 Recognized Expert New Member
Sorry guys, could I ask a follow up question on this.

Once you've got this working, how do you harvest the entered info to post to a database?

I've been playing around with some different php ideas to do it but no luck so far.
Hi BigM

Taking the code you posted above, you see you named your textfield "criteria[]"

as you know the field's values can normally be collected with $_POST or $_GET. This is actually still the case, the only thing is that when you put in the square brackets, your result will be an array :)

if you wanted to gather all the inputfields you would use something like

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. foreach($_POST['criteria'] as $value)
  3. {
  4.     echo $value;
  5. }
  6. ?>
  7.  
if you try to print out $_POST['criteria'] you will most likely see "Array".
Aug 31 '07 #17
ronnil
134 Recognized Expert New Member
@BigM again :)

you need to put your formfields inside the form tag, also if you wrap a div or table tag around them, if you don't, nothing will get send with the form :)

and make sure to set the method attribute too, cause the GET method might not always be the browser default :)
Aug 31 '07 #18
BigM
10 New Member
Thanks very much ronnil, working perfectly now. :)
Sep 2 '07 #19

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when...
7
by: Jack | last post by:
Hi, I am trying to test a sql statement in Access which gives me the error as stated in the heading. The sql statement is built as a part of asp login verification, where the userid and password...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
3
by: Peter Bailey | last post by:
Could someone please tell me how to pass criteria as if it were a parameter. I have a routine now that creates the sql string (well almost). at present the parameter is so I can pass one item ie...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
3
by: Erland Sommarskog | last post by:
I've uploaded a new version of my article on Dynamic Search Conditions on http://www.sommarskog.se/dyn-search.html. I've revised the article to cover SQL 2005, and made a general overhaul of the...
1
by: 6afraidbecause789 | last post by:
Hi - I'm trying to make a Where statement but can't. I''ve put multiple yes/no fields in a listbox as a field list according Don Leverton's comments in the thread "query by field heading options"...
14
ollyb303
by: ollyb303 | last post by:
Hi, I am trying to create a dynamic crosstab report which will display number of calls handled (I work for a call centre) per day grouped by supervisor. I have one crosstab query (Query1) which...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.