Page 1 of 1

multiple text field, save to db if not empty

Posted: Tue Sep 16, 2008 7:48 pm
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.

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

Posted: Tue Sep 16, 2008 8:35 pm
by s.dot
check out empty() and yes, use if() statements. :) Be careful though, empty returns TRUE for 0, so use type checking (=== & !==).

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

Posted: Tue Sep 16, 2008 8:46 pm
by zplits
thanks Scottayy.... IF statement? not array?

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

Posted: Wed Sep 17, 2008 12:38 am
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?

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

Posted: Wed Sep 17, 2008 12:42 am
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.

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

Posted: Wed Sep 17, 2008 12:50 am
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());
}
 
 

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

Posted: Wed Sep 17, 2008 12:53 am
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... :)

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

Posted: Wed Sep 17, 2008 12:59 am
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[]"?

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

Posted: Wed Sep 17, 2008 1:04 am
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