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 :confused:

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):

 $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:

    extract ($_POST);
    if (empty($_POST['$rNrVprism']) && !empty($_POST['$rNradd']) && !empty($_POST['$rVprism'])){
        $_POST['$rNrVprism'] = '$rVprism';
    }

I would really be grateful for some help :slight_smile:

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

        $_POST['$rNrVprism'] = '$rVprism';

be

        $_POST['rNrVprism'] = $_POST['rVprism'];

:confused:

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:
       "" (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.

<?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";
}
?>

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:
http://www.unix.com/web-programming/251378

Since new thread is started, this one is closed...