寻找有关处理和隐藏HTML表单的提示

时间:2011-01-27 22:59:57

标签: php validation forms webforms

我希望能够提交表单,使用Javascript处理它,然后PHP +进行API调用,然后提供感谢信息或显示API的错误数组。

到目前为止,我已经成功地拨打了电话,谢谢你,但之后无法隐藏表格。

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>

3 个答案:

答案 0 :(得分: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 } ?>

答案 1 :(得分: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');
}

答案 2 :(得分: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; ?>