473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Dynamic Conditions/Criteria in PHP form

67
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 3128
coool
67
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 Expert 4TB
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
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 Expert 1GB
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 Expert 4TB
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
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 Expert 4TB
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
You are really SMART

It's WORKING

I feel I wonna CRY ...... THAAAAAAAAANKS VeRy MuCh :)
Aug 12 '07 #9
pbmods
5,821 Expert 4TB
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
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 Expert 4TB
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
Thanks for your help :)

things are working just as i want :D
Aug 14 '07 #13
pbmods
5,821 Expert 4TB
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
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
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 Expert 100+
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 Expert 100+
@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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.