This topic is locked

Can I use an onload event to give values to an add form

11/8/2006 1:42:54 AM
PHPRunner General questions
jwoker author

I have a 1 to 1 relationship between my customer and order table. Customer is the master and order is the detail. When I click on order from the customer list page I get an empty list page for order, then I click on "add new". On the add page for order my customer id field is preloaded (because it is the key field). I would like to use that value to read other values from the customer table when the page loads so that they will fill the form fields the same way the customer id does. Are the values for the order fields available to the onload event? I tried the code below based on some code I used in a beforeadd event but it didn't work.
Thanks

function AddOnLoad()

{

//********** Check if specific record exists ************

global $conn;

$strSQLExists = "select * from customer where id='".$values["cus_id"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

$values["addr1"]=$data["address1"];

$values["addr2"]=$data["address2"];

$values["city"]=$data["city"];

$values["state"]=$data["state"];

$values["zip"]=$data["zip"];

}

else

{

// if dont exist do something else

}
}
J
Jane 11/8/2006

Hi,
there is no $values variable in the AddOnLoad event.

You can do the following:

  1. use following AddOnLoad event.
    function AddOnLoad()

    {

    global $strTableName, $conn;

    $strSQLExists = "select * from customer where id='".$_SESSION[$strTableName."_masterkey1"]."'";

    $rsExists = db_query($strSQLExists,$conn);

    $data=db_fetch_array($rsExists);

    if($data)

    {

    $_SESSION["addr1"] = $data["address1"];

    $_SESSION["addr2"] = $data["address2"];

    $_SESSION["city"] = $data["city"];

    $_SESSION["state"] = $data["state"];

    $_SESSION["zip"] = $data["zip"];

    }

    }



2. use $_SESSION["addr1"], $_SESSION["addr2"] etc. as default values on the "Edit as" settings dialog on the Visual editor tab.

jwoker author 11/8/2006

I put that code in but it only works if I refresh the page, or go back to list and "add new" again? If I come back to the add page under a different master key the default values that show are from the previous master key.
I thought about coding to refresh the page but I think that would create an endless loop.
Thanks for your help

jwoker author 11/9/2006

Here's where I am with the ode:

function AddOnLoad()

{

global $strTableName, $conn;

$strSQLExists = "select * from customer where id='".$_SESSION[$strTableName."_masterkey1"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if ($_SESSION["flag"]== $_SESSION[$strTableName."_masterkey1"])

{

if($data)

{

$_SESSION["addr1"] = $data["address1"];

$_SESSION["addr2"] = $data["address2"];

$_SESSION["city"] = $data["city"];

$_SESSION["state"] = $data["state"];

$_SESSION["zip"] = $data["zip"];

$_SESSION["flag"]= $_SESSION[$strTableName."_masterkey1"];

}

}

else {

header("Location: Sales_job_add.php?a=return");

exit();

}

}


I am trying to refresh the page if the key is not matching with the data in $_session. But I am getting the following error.
Cannot modify header information - headers already sent by (output started at D:\_web\xampp\htdocs\firstchoice\templates_c\%%C1^C14^C14473C9%%Sales_job_add.htm.php:8)
any help?

jwoker author 11/9/2006

I guess the default values are read in before the onload functions run. So here is what I got to work:

function AddOnLoad()

{

global $strTableName, $conn;

$strSQLExists = "select * from customer where id='".$_SESSION[$strTableName."_masterkey1"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);



if($data["id"] != $_SESSION["mkey"])

{

$_SESSION["addr1"] = $data["address1"];

$_SESSION["addr2"] = $data["address2"];

$_SESSION["city"] = $data["city"];

$_SESSION["state"] = $data["state"];

$_SESSION["zip"] = $data["zip"];

$_SESSION["mkey"] = $data["id"];

echo "<meta http-equiv='refresh' content='0'>";

}

}