php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實(shí)例
來源:易賢網(wǎng) 閱讀:885 次 日期:2016-08-26 15:22:40
溫馨提示:易賢網(wǎng)小編為您整理了“php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實(shí)例”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了php基于jquery的ajax技術(shù)傳遞json數(shù)據(jù)簡單實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

html頁面:

<html>

<head>

<meta http-equiv="content-type" content="text/html;charset=utf-8" />

<script type="text/javascript" src="jquery-1.8.2.min.js"></script>

<script type="text/javascript">

 $(function(){

   $("#send").click(function(){

    var cont = $("input").serialize();

    $.ajax({

      url:'ab.php',

      type:'post',

      dataType:'json',

      data:cont,

      success:function(data){

       var str = data.username + data.age + data.job;

       $("#result").html(str);

    }

  });

 });

 });

</script>

</head>

<body>

<div id="result">一會看顯示結(jié)果</div>

<form id="my" action="" method="post">

 <p><span>姓名:</span> <input type="text" name="username" /></p>

 <p><span>年齡:</span><input type="text" name="age" /></p>

 <p><span>工作:</span><input type="text" name="job" /></p>

</form>

<button id="send">提交</button>

</body>

</html>

php頁面:

<?php

header("Content-type:text/html;charset=utf-8");

    $username = $_POST['username'];

    $age = $_POST['age'];

    $job = $_POST['job'];

    $json_arr = array("username"=>$username,"age"=>$age,"job"=>$job);

    $json_obj = json_encode($json_arr);

    echo $json_obj;

?>

使用post方式:

<script type="text/javascript">

 $(function(){

 $("#send").click(function(){

   var cont = {username:$("input")[0].value,age:$("input")[1].value,job:$("input")[2].value};

   var url = 'ab.php';

   $.post(url,cont,function(data){

    var res = eval("(" + data + ")");//轉(zhuǎn)為Object對象

   var str = res.username + res.age + res.job;

  $("#result").html(str);

  });

 });

 });

</script>

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)