Ive have an array called $_SESSION['batcharray'], Each time the user clicks the POST button it adds a record to the array, This works fine.
I have anather button called BatchArray witch calls the function addDatadb from the class "Batcharray"
The problem is that I cannot figure out how to stop the fuction from running everytime a page is loaded or the POST button is clicked.
Here Is the class
Code: Select all
<?php
class Batcharray extends Database
{
/**
* Creates the array and echo's out array each time POST IS CLICKED
*
*
*/
function getArrayData()
{
$batchdate1 = date('Y-m-d', strtotime(preg_replace('/\//','-',$_POST['batch_date'])));
if (isset($_POST['batch_number']))
{
$_SESSION['batcharray'][] = array( "batch_date" => $batchdate1,
"batch_number" => $_POST['batch_number'],
"original_proof_num" => $_POST['original_proof_num'],
"batch_status" => $_POST['batch_status'],
"invoice_num_ccaus" => $_POST['invoice_num_ccaus'],
"carrier" => $_POST['carrier'],
"num_of_boxes" => $_POST['num_of_boxes'],
"toll_consign_num" => $_POST['toll_consign_num'],
"custId" => $_POST['custId'],
"venueId" => $_POST['venueId'],
"bpay" => $_POST['bpay'],
"proof_tracking_comments" => $_POST['proof_tracking_comments'],
"veunue_comments" => $_POST['veunue_comments'],
);
}
for ($i = 0; $i <= count($_SESSION['batcharray']); $i++)
{
print $_SESSION['batcharray'][$i]['batch_date'] . " - " . $_SESSION['batcharray'][$i]['batch_number'] . " - " . $_SESSION['batcharray'][$i]['original_proof_num'] . " - " . $_SESSION['batcharray'][$i]['batch_status'] . " - " . $_SESSION['batcharray'][$i]['invoice_num_ccaus'] . " - " . $_SESSION['batcharray'][$i]['carrier'] . " - " . $_SESSION['batcharray'][$i]['num_of_boxes'] . " - " . $_SESSION['batcharray'][$i]['toll_consign_num'] . " - " . $_SESSION['batcharray'][$i]['custId'] . " - " . $_SESSION['batcharray'][$i]['venueId'] . " - " . $_SESSION['batcharray'][$i]['bpay'] . " - " . $_SESSION['batcharray'][$i]['proof_tracking_comments'] . " - " . $_SESSION['batcharray'][$i]['veunue_comments'] . "<br />";
}
}
/**
* Increments feild in form
*/
public function incrementArray()
{
foreach($_SESSION['batcharray'] as $obj)
{
$out[] = $obj[invoice_num_ccaus];
}
$shownext = max($out);
$shownext1 = $shownext + 1 ;
$_SESSION['shownext1'] = $shownext1;
}
/**
* Increments feild in form
*/
public function incrementArray2()
{
foreach ($_SESSION['batcharray'] as $obj)
{
$tolcon[] = $obj[toll_consign_num];
}
$showntoll = max($tolcon);
$showntoll1 = $showntoll + 1;
$_SESSION['showntoll'] = $showntoll1;
}
/**
* This is the function i only want to run when button is clicked
*
*/
function addDatadb()
{
foreach ($_SESSION['batcharray'] as $obj)
{
$comma_separated = implode("','", $obj); /*implode my array*/
$myquery = "INSERT INTO batch_import VALUES(null,'$comma_separated','0');"; /*create query string */
echo $myquery . "<br />";
mysql_query($myquery); /*run query*/
}
unset($_SESSION['batcharray']); /*would like to clear array*/
/* how can i make it so this is only called when the button is clicked and not when page reloads each time */
}
}
?>this is the onclick event that calls the function
Code: Select all
$form->add( array('name' => 'arrayclear', 'type' => 'button', 'buttontype' => 'button', 'value' => 'Clear array', 'onclick'=> "location.href='batch.php?=&action=addDatadb(); return false;' " , ) );this is the javascript that calls the function
Code: Select all
function addDatadb()
{
b = "<?=$batch->addDatadb();?>";
alert(b);
return false;
}I only want the function to happen when the button is clicked