multiple text field, save to db if not empty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

multiple text field, save to db if not empty

Post by zplits »

Hello there everyone, good day. Can any one please help me? Here is my problem i have a page, here is it below,
Image

I want that if the user clicks the save button, all text field which is not empty will be stored into the database. Otherwise, if it is empty, it will be ignored.

here are the names of the text fields in order.
legend: quantity =qty ; amt = amount

qty1 dropdown1 price1 amt1
qty2 dropdown2 price2 amt2
qty3 dropdown3 price3 amt3
qty4 dropdown4 price4 amt4
qty5 dropdown5 price5 amt5

values will be save in database table named "tbl_sales"

I really don't know how to deal with this, should i use "array"? or just "IF". Any helping hand?
Thanks a lot in advance.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: multiple text field, save to db if not empty

Post by s.dot »

check out empty() and yes, use if() statements. :) Be careful though, empty returns TRUE for 0, so use type checking (=== & !==).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: multiple text field, save to db if not empty

Post by zplits »

thanks Scottayy.... IF statement? not array?
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: multiple text field, save to db if not empty

Post by wolfwood16 »

you could use both.. the array and the empty()

but question is that, hence the user must select the dropdown, Is he gonna fill in the other fields(price, amount, qty)? or it'll automaticaly retrieve values from the db?
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: multiple text field, save to db if not empty

Post by zplits »

Thanks wolfwood16. I know how to get data from the database and put it in the drop-down, but when i select an item in the drop-down, i can't make it to appear it's values in another text field for example the price.

I think i'll go with the IF. I don't know much about array.
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: multiple text field, save to db if not empty

Post by wolfwood16 »

ok i misunderstood, the user must fill in the fields and select values


on the form fields

Code: Select all

 
<input type="text" name="qty[]"> and so on...
<input type="text" name="qty[]"> and so on...
<input type="text" name="qty[]"> and so on...
 


and on the process...

Code: Select all

 
 
//set the values to the ff variables
$qtyval = $_POST[qty];
$dropdownval = $_POST[dropdown];
$priceval = $_POST[price];
$amountval = $_POST[amt];
 
//and then we retrieve the array by foreach()
foreach($qtyval as $s){
   // set the empty condition here
 
   //set the query variable
   $insertToDB = "INSERT INTO tbl_name(id, qty_field, dropdown_feld, price_field, amt_field ) VALUES (
           '.$_POST[id].', '.$qtyval[$s].', '$dropdown_val[$s]', '.$priceval[$s].', '$amountval[$s]'
      )";
    //and insert
    mysql_query($insertToDB,$connString) or die(mysql_error());
}
 
 
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: multiple text field, save to db if not empty

Post by wolfwood16 »

zplits wrote:Thanks wolfwood16. I know how to get data from the database and put it in the drop-down, but when i select an item in the drop-down, i can't make it to appear it's values in another text field for example the price.

I think i'll go with the IF. I don't know much about array.

you may try the conditional linking through $_REQUEST statement. but still you need to post submit it first. else, ajax... :)
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: multiple text field, save to db if not empty

Post by zplits »

<input type="text" name="qty[]"> and so on...
<input type="text" name="qty[]"> and so on...
<input type="text" name="qty[]"> and so on...
should i name all my textfields as "qty[]"?
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: multiple text field, save to db if not empty

Post by wolfwood16 »

no that is only for quantity..

Code: Select all

qty[] for quantity
dropdown[] = for dropdown   // 
    <select name="dropdown[]" >
        <option value="value1"> this is the first value </option>
        <option value="value2"> this is the second value </option>
        <option value="value3"> this is the third value </option>
        <option value="value4"> this is the fourth value </option>
    </select>
price[] = price
amt[] = amount
Post Reply