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

Php 查找有关处理和隐藏HTML表单的提示,php,validation,forms,webforms,Php,Validation,Forms,Webforms,我希望能够提交一个表单,用Javascript处理它,然后PHP+调用API,然后用API显示感谢信息或显示错误数组 到目前为止,我已经成功地打了那个电话,并向您表示感谢,但之后无法隐藏表格 有没有想过我做错了什么?任何指导都将不胜感激 <?php if (isset($_POST['submitted'])){ session_start(); require_once 'CallerService.php'; /** * Get required parameters from

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

到目前为止,我已经成功地打了那个电话,并向您表示感谢,但之后无法隐藏表格

有没有想过我做错了什么?任何指导都将不胜感激

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

我假设您使用的是经典Javascript,而不是像jQuery这样的库。
因此,让我们假设您的表单位于一个DIV中,ID为myForm,在经典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();
    }
    });
此外,当您发现自己包含这样的文件片段时,请实际使用includes。。。像

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

您的else括号在窗体之前已关闭,请尝试以下操作:

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 } ?>
else{/*显示表单*/?>

正如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; ?>
if($ack==“SUCCESS”)
$\u会话['success']=true;
其他的
{
//无论api返回什么,这里都会进行大量处理
$\u会话['success']=false;
}
如果($_会话['success']):?>
非常感谢。
...

非常感谢大家。非常有帮助。现在效果很好。现在我的挑战是接受我的工作,让它在Wordpress中发挥所有功能。有没有人有过在Wordpress中集成PHP表单处理程序的经验?对不起,我自己写了所有代码,从来没有使用WordPressWP的任何经验都很简单。这只是基本的PHP。当你需要访问wp变量,但您会习惯它们愚蠢的名称和糟糕的基于函数的交付系统。