Using the same script multiple times for one form (using PHP) -
i relatively new php , programming, please pardon ignorance , language.
i have form has multiple instances of same input fields (see below). have script process input data, if submitted 1 set @ time(also below). have 1 submit button @ button of form execute script many multiples of data set have on page.
does have idea how can work? cannot rename variables (i tried that) because receiving server needs data labels appear below. help/direction appreciated. thanks!
my form:
<form method="post" action="<?php echo (authorizenet_sandbox ? authorizenetdpm::sandbox_url : authorizenetdpm::live_url)?>" id="checkout_form"> <?php $time = time(); $fp_sequence = $time; $fp = authorizenetdpm::getfingerprint(authorizenet_api_login_id, authorizenet_transaction_key, $amount, $fp_sequence, $time); $sim = new authorizenetsim_form( array( 'x_amount' => $amount, 'x_fp_sequence' => $fp_sequence, 'x_fp_hash' => $fp, 'x_fp_timestamp' => $time, 'x_relay_response'=> "true", 'x_relay_url' => $coffee_store_relay_url, 'x_login' => authorizenet_api_login_id, 'x_test_request' => test_request, ) ); echo $sim->gethiddenfieldstring(); } ?> <fieldset> <h3>card #1</h3> <div> <label>amount</label> <input type="text" class="text required" size="4" name="amount" value=""></input> </div> <div> <label>credit card number</label> <input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input> </div> <div> <label>exp.</label> <input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input> </div> <div> <label>ccv</label> <input type="text" class="text required" size="4" name="x_card_code" value="782"></input> </div> </fieldset> <fieldset> <div> <label>first name</label> <input type="text" class="text required" size="15" name="x_first_name" value="john"></input> </div> <div> <label>last name</label> <input type="text" class="text required" size="14" name="x_last_name" value="doe"></input> </div> </fieldset> <fieldset> <div> <label>address</label> <input type="text" class="text required" size="26" name="x_address" value="123 4 street"></input> </div> <div> <label>city</label> <input type="text" class="text required" size="15" name="x_city" value="san francisco"></input> </div> </fieldset> <fieldset> <div> <label>state</label> <input type="text" class="text required" size="4" name="x_state" value="ca"></input> </div> <div> <label>zip code</label> <input type="text" class="text required" size="9" name="x_zip" value="94133"></input> </div> <div> <label>country</label> <input type="text" class="text required" size="22" name="x_country" value="us"></input> </div> </fieldset> <fieldset> <h3>card #2</h3> <div> <label>amount</label> <input type="text" class="text required" size="4" name="amount" value=""></input> </div> <div> <label>credit card number</label> <input type="text" class="text required creditcard" size="15" name="x_card_num" value="6011000000000012"></input> </div> <div> <label>exp.</label> <input type="text" class="text required" size="4" name="x_exp_date" value="04/15"></input> </div> <div> <label>ccv</label> <input type="text" class="text required" size="4" name="x_card_code" value="782"></input> </div> </fieldset> <fieldset> <div> <label>first name</label> <input type="text" class="text required" size="15" name="x_first_name" value="john"></input> </div> <div> <label>last name</label> <input type="text" class="text required" size="14" name="x_last_name" value="doe"></input> </div> </fieldset> <fieldset> <div> <label>address</label> <input type="text" class="text required" size="26" name="x_address" value="123 4 street"></input> </div> <div> <label>city</label> <input type="text" class="text required" size="15" name="x_city" value="san francisco"></input> </div> </fieldset> <fieldset> <div> <label>state</label> <input type="text" class="text required" size="4" name="x_state" value="ca"></input> </div> <div> <label>zip code</label> <input type="text" class="text required" size="9" name="x_zip" value="94133"></input> </div> <div> <label>country</label> <input type="text" class="text required" size="22" name="x_country" value="us"></input> </div> </fieldset> <input type="submit" value="buy" class="submit buy"> </form>
my script:
<?php require_once 'coffee_store_settings.php'; if ($method_to_use == "aim") { $transaction = new authorizenetaim; $transaction->setsandbox(authorizenet_sandbox); $transaction->setfields( array( 'amount' => $amount, 'card_num' => $_post['x_card_num'], 'exp_date' => $_post['x_exp_date'], 'first_name' => $_post['x_first_name'], 'last_name' => $_post['x_last_name'], 'address' => $_post['x_address'], 'city' => $_post['x_city'], 'state' => $_post['x_state'], 'country' => $_post['x_country'], 'zip' => $_post['x_zip'], 'email' => $_post['x_email'], 'card_code' => $_post['x_card_code'], ) ); $response = $transaction->authorizeandcapture(); if ($response->approved) { // transaction approved! logic here. header('location: thank_you_page.php?transaction_id=' . $response->transaction_id); } else { header('location: error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text); } } elseif (count($_post)) { $response = new authorizenetsim; if ($response->isauthorizenet()) { if ($response->approved) { // transaction approved! logic here. // redirect user site. $return_url = $site_root . 'thank_you_page.php?transaction_id=' .$response->transaction_id; } else { // there problem. logic here. // redirect user site. $return_url = $site_root . 'error_page.php?response_reason_code='.$response->response_reason_code.'&response_code='.$response->response_code.'&response_reason_text=' .$response->response_reason_text; } echo authorizenetdpm::getrelayresponsesnippet($return_url); } else { echo "md5 hash failed. check make sure md5 setting matches 1 in config.php"; } }
php supports array notation form fields, can force values show multiple times. e.g.
<input type="text" name="data[1]" value="foo" /> <input type="text" name="data[a]" value="bar" /> <input type="text" name="data[abcef]" value="baz" />
would give $_post structure like:
$_post['data'] = array( 0 => 'foo' 'a' => 'bar' 'abcef' => 'baz' )
since you'd have multiple copies of multiple fields, requiring data kept together, structure form like:
set #1 <input name="somefield[1]" /> <input name="otherfield[1]" /> ... set #2 <input name="somefield[2]" /> <input name="otherfield[2]" />
and on server upon submission:
foreach(array_keys($somefield) $key) { $somefield = $_post['somefield'][$key]; $otherfield = $_post['otherfield'][$key]; ... ... }
Comments
Post a Comment