Php help to copy form field if empty


 
Thread Tools Search this Thread
Top Forums Web Development Php help to copy form field if empty
# 1  
Old 09-22-2014
Php help to copy form field if empty

I have an input form with several fields. What I would like to achieve is to auto populate or copy certain fields if they are empty when the form is submitted. I would like to use php if not then javascript but not jquery if possible - I have sort of had a go but I really have no idea Smilie

The bit of logic I need is:
If rNradd is empty then do nothing;
If rNradd and rVprism contain data and rNrVprism is empty then copy rVprism to rNrVprism;
If rNrVprism already contains data then do nothing.

There are a few other fields I need to do this for at the same time so I need the solution to be expandable.


Here is the section of code in question (written in DW - please do not be too derogatory):
Code:
 $insertSQL = sprintf("INSERT INTO spec_rx (spec_rx_id, FK_px_id, spec_rx_date, FK_user_id, spec_rx_rsph, spec_rx_rcyl, spec_rx_raxis, spec_rx_rhprism, spec_rx_rhprismbase, spec_rx_rvprism, spec_rx_rvprismbase, spec_rx_rnadd, spec_rx_rnhprism, spec_rx_rnhprismbase, spec_rx_rnvprism, spec_rx_rnvprismbase, spec_rx_rintadd, spec_rx_rinthprism, spec_rx_rinthprismbase, spec_rx_rintvprism, spec_rx_rintvprismbase, spec_rx_lsph, spec_rx_lcyl, spec_rx_laxis, spec_rx_lhprism, spec_rx_lhprismbase, spec_rx_lvprism, spec_rx_lvprismbase, spec_rx_lintadd, spec_rx_linthprism, spec_rx_linthprismbase, spec_rx_lintvprism, spec_rx_lintvprismbase, spec_rx_lnadd, spec_rx_lnhprism, spec_rx_lnhprismbase, spec_rx_lnvprism, spec_rx_lnvprismbase, ext_rx) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['SpecRxID'], "int"),
                       GetSQLValueString($_POST['pxID'], "int"),
                       GetSQLValueString($_POST['specRxDate'], "date"),
                       GetSQLValueString($_POST['userID'], "int"),
                       GetSQLValueString($_POST['rsph'], "double"),
                       GetSQLValueString($_POST['rcyl'], "double"),
                       GetSQLValueString($_POST['raxis'], "double"),
                       GetSQLValueString($_POST['rHprism'], "double"),
                       GetSQLValueString($_POST['rHprismbase'], "text"),
                       GetSQLValueString($_POST['rVprism'], "double"),
                       GetSQLValueString($_POST['rVprismbase'], "text"),
                       GetSQLValueString($_POST['rNradd'], "double"),
                       GetSQLValueString($_POST['rNrHprism'], "double"),
                       GetSQLValueString($_POST['rNrHprismbase'], "text"),
                       GetSQLValueString($_POST['rNrVprism'], "double"),

I have tried to add the following code without success:
Code:
    extract ($_POST);
    if (empty($_POST['$rNrVprism']) && !empty($_POST['$rNradd']) && !empty($_POST['$rVprism'])){
        $_POST['$rNrVprism'] = '$rVprism';
    }

I would really be grateful for some help Smilie
# 2  
Old 09-22-2014
Bug

Quote:
Originally Posted by barrydocks
Code:
    extract ($_POST);
    if (empty($_POST['$rNrVprism']) && !empty($_POST['$rNradd']) && !empty($_POST['$rVprism'])){
        $_POST['$rNrVprism'] = '$rVprism';
    }

I marked four dollar signs red because they seem to be superfluous. I also think because of this bug the POST variables weren't even recognized.

And shouldn't
Code:
        $_POST['$rNrVprism'] = '$rVprism';

be
Code:
        $_POST['rNrVprism'] = $_POST['rVprism'];

Smilie
# 3  
Old 09-23-2014
Don't use empty unless you are sure that's what you want:

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

Code:
       "" (an empty string)
       0 (0 as an integer)
       0.0 (0 as a float)
       "0" (0 as a string)
       NULL
       FALSE
       array() (an empty array)
       $var; (a variable declared, but without a value)

Usually the array_key_exists check should suffice.

Code:
<?php

$arr = array('a' => 0); $key = 'a';
var_dump(array_key_exists($key, $arr)); // -->  TRUE
var_dump(empty($arr[$key])); // -->  TRUE

if(array_key_exists($key, $arr)) {
    echo "key $key exists\n";
}

if(empty($arr[$key])) {
    echo "empty\n";
}
?>

# 4  
Old 09-23-2014
Thanks for the replies guy but I think javascript would be the best way to go as I want the user to see the changes as they happen.
I have started a new thread:
https://www.unix.com/web-programming/251378
# 5  
Old 09-24-2014
Since new thread is started, this one is closed...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Copy non-empty files into subdirectory

How to Copy non-empty files into subdirectory I have directory A there are bunch of files: a.txt a.txt.target b.txt b.target.txt....... How can i find out the non-empty file within *.txt.target and copy them into a subdirectory B? Thank you. (2 Replies)
Discussion started by: grace_shen
2 Replies

2. Shell Programming and Scripting

Cgi to dump xml data from form input field

Hi All, I am trying to write a shell script which takes parse the web form find the input field and dump the data of that field into one xml file. The form looks like, <input type="button" id="btnSave" value="Save" onclick="saveXmlData()"/> <form name="submitForm"... (1 Reply)
Discussion started by: jdp
1 Replies

3. Shell Programming and Scripting

How to detect empty field in awk ?

Hi ! programmers I have a need of detecting empty field in file my file looks like this 40.900|-71.600|1.6|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|5.7|20|1|1961|21.00|3.700||1|US|28035|10029370|31 40.900|-71.600|7.8|20|1|1961|21.00|3.700||1|US|28035|10029370|31... (7 Replies)
Discussion started by: Dona Clara
7 Replies

4. Shell Programming and Scripting

Copy last third char form string

HI Input A.txt ABC907 ABC907_1B_9 ABC985 ABC985_1A_9 ABC985 ABC985_1B_9 ABC985 ABC985_1C_9 ABC05037 ABC05037_1A_9 ABC05037 ABC05037_1B_9 Base of column 2 last third char. If It is A the 1,if B then 2 If C then 3 File B.txt ABC907 ABC907_1B_9 2 ABC985 ABC985_1A_9 1 ABC985... (8 Replies)
Discussion started by: asavaliya
8 Replies

5. Shell Programming and Scripting

print non empty column/field value

I have below file 25-09-2012 24-09-2012 19-09-2012 31-07-2012 30-04-2012 30-03-2012 ASIAEXFVNV N/A CEU 4 DMIRSOA N/A CAS 2 2 2 DMIRSOA N/A MIDMT 2 NFIAL22 N/A HVNY 11 11 11 NFIAL22 N/A NYAL3 11 11 11 NFIAL22 N/A NYCN 11 11 11 ... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

6. UNIX for Dummies Questions & Answers

remove empty field

Hi all ! I'm sure it is a basic question but I didn't find any threads that fit my need. How to remove empty fields with awk? Or in other words, how to shift all the fields after an empty field on the left? input: 1|2||3|4|5||6 wanted: 1|2|3|4|5|6 I tried: awk '{for(i=1; i<=NF;... (7 Replies)
Discussion started by: lucasvs
7 Replies

7. Shell Programming and Scripting

Converting form field to table format

May data Name = Andi Address = none Phone = 82728 Name = Peter Address = none Phone = 98799 The expected output Name,Address,Phone Andi,none,82728 Peter,none,98799 what i have done (6 Replies)
Discussion started by: before4
6 Replies

8. Shell Programming and Scripting

Format the file by deleting empty field

I have the test data with 10 column separated by comma and each column has more than 1000000 rows. Can anyone help me to find empty field in all columns and delete that empty field alone and lift that column up by one row. Data with empty field: A74203XYZ,A21718XYZ,A72011XYZ,A41095XYZ,... (7 Replies)
Discussion started by: zooby
7 Replies

9. Filesystems, Disks and Memory

DD copy excluding empty blocks?

I'm not 100% sure if this was the right place to post in but here i go: I'm attempting to migrate a physical windows disk onto my SAN through the use of the dd copy command. I'm booting into knoppix, running dd, and then bringing the server back up into windows running on the SAN. Everything... (2 Replies)
Discussion started by: Merkilo
2 Replies

10. UNIX for Dummies Questions & Answers

Count of Field for Non-Empty

Hi Guys, I wanted to count the number of records for a particular field of a file. whose fields are separated by comma"," I fI use this command. cat "filename" cut -sd "," -f13 | wc -l This shows all the lines count including the blank values for the field number 13. I wanted to count... (2 Replies)
Discussion started by: Swapna173
2 Replies
Login or Register to Ask a Question