2011-12-29 15 views

答えて

2

:ここでは、始めるためのリンクです

function parseJsonIfPossible(code){ 
    try { 
     return $.parseJSON(code); 
    } catch (e) { 
     return code; 
    } 
} 
function cmd(command,p1,p2,p3,p4,p5,p6){ 
    return parseJsonIfPossible($.ajax({ 
     url: core_url, 
     global: false, 
     type: "POST", 
     data: {command : command,p1:p1,p2:p2,p3:p3,p4:p4,p5:p5,p6:p6}, 
     dataType: "html", 
     async:false, 
     success: function(msg){ 
      //alert(msg); 
     } 
    }).responseText); 
} 

PHP:

function cmd() 
{ 
$command = &$_POST['command']; 
$p1 = &$_POST['p1'];$p2 = &$_POST['p2'];$p3 = &$_POST['p3'];$p4 = &$_POST['p4'];$p5 = &$_POST['p5'];$p6 = &$_POST['p6']; 
if($command)echo($command($p1,$p2,$p3,$p4,$p5,$p6)); 
} 
function test($i) 
{ 
    //simple 
    return mysql_query("UPDATE ... SET b='$i'"); 
    //array 
    return json_encode(array(
     mysql_query("UPDATE ... SET b='$i'"), 
     "fklsdnflsdnl", 
     )); 
} 

使用方法(スクリプト):

$('#btn').click(function(){ 
    var i = 99; 
    var result = cmd("test",i); 
    //simple 
    alert(result); 
    //array 
    console.log([result[0],result[1]]); 
}); 
1

以下は、Ajaxリクエストのための基本的なサンプルコードです。

function postData() 
{ 
var xmlhttp; 
if (window.XMLHttpRequest) 
    {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
    } 
else 
    {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
xmlhttp.onreadystatechange=function() 
    { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) 
    { 
    // Do anything you want with the response 
    alert(xmlhttp.responseText); 
  } 
    } 
xmlhttp.open("POST","page_to_be_post.php",true); 
xmlhttp.send(); 
} 

しかし、いくつかのフレームワークを使用すれば、少ないコードで簡単にこれを行うことができます。

例:

http://www.prototypejs.org/learn/introduction-to-ajax

http://api.jquery.com/jQuery.post/

利用でき、より多くのフレームワークがあります。

乾杯!

1

以下のコードは、jquery ajaxを使用してPHPの投稿に役立つかもしれません。 id = formのフォームがあり、id = submit1のフォームを送信するボタンがあるとします。 jquery ajaxを使用してフォームデータを別のページに送信するには、bodyの最後の直前に次のコードが必要です。以下のコードを書く前にjqueryスクリプトを添付するのを忘れないでください。

$(document).ready(function(){ 
    $('submit1').click(function(event){ 
    //first stop the default submit action 
    event.preventDefault(); 
    //now we will do some ajax 
    $.ajax({ 
    url:'test.php',//the target page where to submit the data 
    type:'post', 
    cache:false, 
    data:$('#form').serialize(),//data to be sent from the form 
    dataType:'json',//expected data type from the server 
    error:function(xhr){ 
       alert("the error is"+xhr.status); 
        } 
    }) 
    //success function in terms of .done 
    .done(function(data){ 
    alert("your data successfully submitted"); 
    }); 
    }); 
});  
関連する問題