php中ajax请求

php中ajax请求的时候,网上一般都是如下这个样子:

<html>
<head>
<script>
function showHint(str)
{
    if (str.length==0)
    { 
        document.getElementById("txtHint").innerHTML="";
        return;
    }
    if (window.XMLHttpRequest)
    {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码
        xmlhttp=new XMLHttpRequest();
    }
    else
    {    
        //IE6, IE5 浏览器执行的代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","gethint.php?q="+str,true);
    xmlhttp.send();
}
</script>
</head>
<body>

<p><b>在输入框中输入一个姓名:</b></p>
<form> 
姓名: <input type="text" οnkeyup="showHint(this.value)">
</form>
<p>返回值: <span id="txtHint"></span></p>

</body>
</html>

服务端:

<?php
// 将姓名填充到数组中
$a[]="Anna";
$a[]="Brittany";
$a[]="Cinderella";
$a[]="Diana";
$a[]="Eva";
$a[]="Fiona";
$a[]="Gunda";
$a[]="Hege";
$a[]="Inga";
$a[]="Johanna";
$a[]="Kitty";
$a[]="Linda";
$a[]="Nina";
$a[]="Ophelia";
$a[]="Petunia";
$a[]="Amanda";
$a[]="Raquel";
$a[]="Cindy";
$a[]="Doris";
$a[]="Eve";
$a[]="Evita";
$a[]="Sunniva";
$a[]="Tove";
$a[]="Unni";
$a[]="Violet";
$a[]="Liza";
$a[]="Elizabeth";
$a[]="Ellen";
$a[]="Wenche";
$a[]="Vicky";

//从请求URL地址中获取 q 参数
$q=$_GET["q"];

//查找是否由匹配值, 如果 q>0
if (strlen($q) > 0)
{
    $hint="";
    for($i=0; $i<count($a); $i++)
    {
        if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
        {
            if ($hint=="")
            {
                $hint=$a[$i];
            }
            else
            {
                $hint=$hint." , ".$a[$i];
            }
        }
    }
}

// 如果没有匹配值设置输出为 "no suggestion" 
if ($hint == "")
{
    $response="no suggestion";
}
else
{
    $response=$hint;
}

//输出返回值
echo $response;
?>
网上都是这样,原生的ajax请求。但是php有自己封装的ajax请求。操作也比较方便。现在我附录一个比较全面的demo:

<?php
    $billingUrl = 'http://localhost/smart/billing.html';
    $amount = $_REQUEST['amount'];
    if($amount != 0){
        $result = "";
        $email = $_REQUEST['email'];
        $company = $_REQUEST['company'];
        $date = $_REQUEST['date'];
        $billingAddress = $_REQUEST['billingAddress_name'].
                        " ".$_REQUEST['billingAddress_address_line1'].
                        " ".$_REQUEST['billingAddress_postal_code']. 
                        " ".$_REQUEST['billingAddress_city'].
                        " ".$_REQUEST['billingAddress_country'].
                        " ".$_REQUEST['billingAddress_province'];
        $shippingAddress = $_REQUEST['shippingAddress_name']." ".
                            $_REQUEST['shippingAddress_address_line1']." ".
                            $_REQUEST['shippingAddress_postal_code']." ".
                            $_REQUEST['shippingAddress_city']." ".
                            $_REQUEST['shippingAddress_country']." ".
                            $_REQUEST['shippingAddress_province'];
        $currency = $_REQUEST['currency'];
        $tax = $_REQUEST['tax'];
        $itemDescription = $_REQUEST['itemDescription'];
        //$comment = $_REQUEST['comment'];
                
        $code = $_REQUEST['cardInfo_code'];
        $cardInfo_name = $_REQUEST['cardInfo_name'];

        $billingStr = '{"email":"'. $email .
            '","company":"'. $company .
            '","date":"'. $date .'","code":"'. $code .
            '","transId":"0","billingAddress":"'. $billingAddress .
            '","shippingAddress":"'. $shippingAddress .
            '","amount":"'. $amount .
            '","currency":"'. $currency .
            '","tax":"'. $tax .
            '","itemDescription":"'. $itemDescription .
            '","comment":"","paymentStatus":"false"}';
        $insertBillingReturn = json_decode(insertBillingTable('http://localhost:3000/billing',$billingStr));
        if($insertBillingReturn -> success == 1){
            $id = $insertBillingReturn -> id;
            //$paymentStr = '{"payment_method":"token","order_number":"MyOrderId000011223344","amount":"' . $amount .
            //            '","token":{"code":"' . $code . 
            //            '","name":"' . $cardInfo_name . '","complete":true}}';  
            //$beanstreamReturn = json_decode(beanstreamPayment('https://www.beanstream.com/api/v1/payments', $paymentStr));
                    
            //if($beanstreamReturn -> code != null){
            //    echo "<script>alert('".$beanstreamReturn -> message."');location.href='".$billingUrl."';</script>";
            //    exit;
            //}
            //else{
            //    $transId = $beanstreamReturn -> id;
            //    $str = '{"id":"'. $id .'","transId":"'. $transId .'","paymentStatus":"true"}';
            //    $updateBillingReturn = json_decode(updateBillingTable('http://localhost:3000/billing',$str));
            //    if($updateBillingReturn -> success == 1){
                    echo "<script>alert('success');location.href='".$billingUrl."';</script>";
                    exit;
            //    }
            //    else{
            //        echo "<script>alert('".$updateBillingReturn -> error."');location.href='".$billingUrl."';</script>";
            //        exit;
            //    }
            //}
        }
        else{
            echo "<script>alert('".$insertBillingReturn -> error."');location.href='".$billingUrl."';</script>";
            exit;
        }
    }
    else{
        echo "<script>alert('The amount is 0.');location.href='".$billingUrl."';</script>";
        exit;
    }
    function beanstreamPayment($url, $post_data) {
        $options = array(  
          'http' => array(  
            'method' => 'POST',  
            'header' => array(
              'Content-type:application/json',
              'Authorization:Passcode MzAwMjAwNTc4OjRCYUQ4MkQ5MTk3YjRjYzRiNzBhMjIxOTExZUU5Zjcw'
            ),  
            'content' => $post_data,  
            'timeout' => 15 * 60 
          )  
        );  
        $context = stream_context_create($options);  
        $result = file_get_contents($url, false, $context);  
        
        return $result;  
    }

    function insertBillingTable($url, $post_data) {
        $options = array(  
          'http' => array(  
            'method' => 'POST',  
            'header' => 'Content-type:application/json',
            'content' => $post_data,  
            'timeout' => 15 * 60 
          )  
        );  
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);  
        
        return $result;  
    } 

    function updateBillingTable($url, $post_data) {
        $options = array(  
          'http' => array(  
            'method' => 'POST',  
            'header' => 'Content-type:application/json',
            'content' => $post_data,  
            'timeout' => 15 * 60 
          )  
        );  
        $context = stream_context_create($options);  
        $result = file_get_contents($url, false, $context);  
        
        return $result;  
    }
?>
下面是我自己更改出来适用自己的ajax,完整代码如上。

$alipayStr = '{"out_trade_no":"'. $out_trade_no .'","trade_no":"'. $trade_no .
			'","trade_status":"'. $trade_status .'","subject":"'. $subject .
			'","total_amount":"'. $total_amount .'","body":"'. $body .'"}';
	$insertBillingReturn = json_decode(insertAlipayTable('http://localhost:3389/alipayZhifubao',$alipayStr));
	if($insertBillingReturn -> success == 1) {
	  
	}
	else{

	}

function insertAlipayTable($url, $post_data) {
	$options = array(  
	'http' => array(  
	  'method' => 'POST',  
	  'header' => 'Content-type:application/json',
	  'content' => $post_data,  
	  'timeout' => 15 * 60 
	)  
	);  
	$context = stream_context_create($options);
	$result = file_get_contents($url, false, $context);  
	return $result;  
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值