2011-01-27 3 views
0

양식을 제출하고 자바 스크립트로 처리 한 다음 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

당신의 다른 브래킷 형태를 닫기 직전에 같은 포함 ... 사용과 같은 파일 조각을 포함하여 자신을 발견하는 경우도

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

,이 시도 :

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 폼 핸들러를 통합 한 경험이 있습니까? –

+0

죄송합니다. 내 모든 코드를 작성하고 WordPress에 대한 경험이 전혀 없습니다. – WebChemist

+0

WP는 쉽습니다. 그것은 단지 기본적인 PHP입니다. wp 변수에 액세스해야 할 때 재미있어 할 수 있지만 바보 같은 이름과 끔찍한 기능 기반 전달 시스템에 익숙해집니다. –

0

jQuery와 같은 라이브러리가 아닌 고전적인 Javascript를 사용하고 있다고 가정합니다. 그래서이 양식이 때문에 myForm을의 ID를 가진 DIV에 있다고 가정하자 고전 자바 스크립트 jQuery를에

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

그것이 $('#myForm').hide();

가 jQuery를 고려 될 것이다.

이제는 자바 스크립트에서 가능한 여러 결과를 처리하기 위해 AJAX 페이지에서 rraw 결과 텍스트가 아닌 JSON 인코딩 문자열을 반향 시키므로 echo "Congratulations"가되지 않습니다. (더 빨리의 원인을 다시 jQuery를에) 그것은 더

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

추천하고 자바 스크립트 측면에있을 것

// 여기 내 페이지에 제출해야 데이터, 나는 모든 양식 요소가 있으리라 믿고있어 ID는 자신의 이름과 같습니다 .... 양식 POST처럼 제출하고 결과가 JSON 형식이어야한다고 예상합니다. 당신이 실제로

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

FatherStorm과 마찬가지로 jQuery가이를 수행하는 훨씬 더 깨끗한 방법입니다. 아약스 전화를 살펴보십시오. 사용하기가 매우 쉽습니다.

하지만 어떤 이유로 든 원하지 않으면 세션 변수를 사용하여 양식을 표시할지 여부를 결정할 수 있습니다.

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