2011-01-27 16 views
0

我希望能夠提交表單,使用Javascript處理它,然後PHP +進行API調用,然後呈現感謝消息或顯示錯誤數組與API。尋找處理和隱藏HTML表單的提示

到目前爲止,我已經能夠成功地打電話了,謝謝,但之後一直無法隱藏表格。

http://www.wilsonkeenan.com/learningphp/DoDirectPayment.php

什麼,我做錯了什麼想法?任何指導將不勝感激。

<?php 

if (isset($_POST['submitted'])){ 
session_start(); 
require_once 'CallerService.php'; 

/** 
* Get required parameters from the web form for the request 
*/ 
$paymentType =urlencode($_POST['paymentType']); 
$firstName =urlencode($_POST['firstName']); 
$lastName =urlencode($_POST['lastName']); 
$creditCardType =urlencode($_POST['creditCardType']); 
$creditCardNumber = urlencode($_POST['creditCardNumber']); 
$expDateMonth =urlencode($_POST['expDateMonth']); 

// Month must be padded with leading zero 
$padDateMonth = str_pad($expDateMonth, 2, '0', STR_PAD_LEFT); 

$expDateYear =urlencode($_POST['expDateYear']); 
$cvv2Number = urlencode($_POST['cvv2Number']); 
$address1 = urlencode($_POST['address1']); 
$address2 = urlencode($_POST['address2']); 
$city = urlencode($_POST['city']); 
$state =urlencode($_POST['state']); 
$zip = urlencode($_POST['zip']); 
$countrycode = urlencode($_POST['countrycode']); 
$amount = urlencode($_POST['amount']); 
//$currencyCode=urlencode($_POST['currency']); 
$currencyCode="USD"; 
$paymentType=urlencode($_POST['paymentType']); 

/* Construct the request string that will be sent to PayPal. 
    The variable $nvpstr contains all the variables and is a 
    name value pair string with & as a delimiter */ 
$nvpstr="&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber&EXPDATE=".   $padDateMonth.$expDateYear."&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName&STREET=$address1&CITY=$city&STATE=$state". 
"&ZIP=$zip&COUNTRYCODE=$countrycode&CURRENCYCODE=$currencyCode"; 



/* Make the API call to PayPal, using API signature. 
    The API response is stored in an associative array called $resArray */ 
$resArray=hash_call("doDirectPayment",$nvpstr); 

/* Display the API response back to the browser. 
    If the response from PayPal was a success, display the response parameters' 
    If the response was an error, display the errors received using APIError.php. 
    */ 
$ack = strtoupper($resArray["ACK"]); 

if($ack!="SUCCESS") { 
    $_SESSION['reshash']=$resArray; 
    $location = "APIError.php"; 
     header("Location: $location"); 
    } elseif ($ack =="SUCCESS") { 
     echo '<h1>Thank you</h1>'; 
    } 
} 
    else { 
    // Display Form 
    } 
    ?> 

<form method="POST" id="donate" action="" name="DoDirectPaymentForm"> 
<!--Payment type is <?=$paymentType?><br> --> 
<input type=hidden name=paymentType value="<?php echo $paymentType?>" > 
    <fieldset> 
    <div> 
     <label class="label">First Name:</label> 
     <input type=text size=36 maxlength=32 name=firstName class="required" value=John> 
    </div> 
    </div> 
<input type="hidden" name="submitted" value="1"> 
<input type=Submit value=Submit> 
</div> 

回答

1

您的其他支架形式關閉之前,試試這個:

else { /*Display Form*/ ?> 


<form method="POST" id="donate" action="" name="DoDirectPaymentForm"> 
<!--Payment type is <?=$paymentType?><br> --> 
<input type=hidden name=paymentType value="<?php echo $paymentType?>" > 
    <fieldset> 
    <div> 
     <label class="label">First Name:</label> 
     <input type=text size=36 maxlength=32 name=firstName class="required" value=John> 
    </div> 
    </div> 
<input type="hidden" name="submitted" value="1"> 
<input type=Submit value=Submit> 
</div> 
<?php } ?> 
+0

非常感謝你們。很有幫助。現在完美運作。現在我的挑戰是要把我的工作,並使其在Wordpress中的所有功能。任何人有任何經驗在Wordpress中集成PHP表單處理程序? – 2011-01-28 18:07:55

0

我假設你使用的是經典的Javascript而不是像jQuery這樣的庫。 讓我們假設你的形式是用myForm會的ID的DIV所以在經典的JavaScript

document.getElementById('myForm').style.display='none'; 
jQuery中

這將是$('#myForm').hide();

考慮jQuery的。

現在,要在Javascript中處理多個可能的結果,您將希望您的AJAX頁面回顯JSON編碼的字符串,而不僅僅是rraw結果文本,因此它不會回顯「恭喜」;它會更喜歡

echo json_encode(array('result'=>'success','html'=>'Congratulations')); 

,並在JavaScript端(jQuery中再次導致它的速度更快)

//我需要提交到我的頁面在這裏的數據,我假設所有的表單元素都有ID與它們的名稱相同....它將以表單提交POST的形式,並返回期望它爲JSON格式的結果。

$.getJSON("handleAjax.php", { paymentType: $('#paymentType').val(), name: $('#name').val(),[...more_fields_here...]}, function(data){ 
    alert(data.html); 
    if(data.result=='success'){ 
     $('#myForm').hide(); 
    } 
    }); 

也,當你發現自己,包括像這樣的文件碎片,實際使用包括......像

if(Condition){ 
include('/templates/paymentForm.phtml'); 
}else{ 
    include('/templates/paymentThanks.phtml'); 
} 
0

就像FatherStorm說的那樣,jQuery是一個更簡潔的方法。看看ajax電話。它非常易於使用。

但是,如果您不想出於某種原因,可以使用會話變量來決定是否顯示錶單。

if($ack == 'SUCCESS') 
    $_SESSION['success'] = true; 
else 
{ 
    //bunch of processing here for whatever your api returns 
    $_SESSION['success'] = false; 
} 

if($_SESSION['success']): ?> 
    Thank You 
<?php else: ?> 
    <form>...</form> 
<?php endif; ?>