2012-09-14 69 views
0

這是我的html頁面:交表格後,阿賈克斯負載頁面內容

<body> 
    <div id="containt"> 
     <p>already have containt.</p> 
    </div> 
    <div id="other"> 
     <form id="test"> 
      <input id="sth" name="sth" value="123456"/> 
      <div id="submit"></div> 
     </form> 
    </div> 
<body> 

和我的PHP腳本: 「abc.php」

$happy['verymuch'] = $_POST['sth']; 
include('needtogetcontent.php');//and then use extract() to extract $verymuch in "needtogetcontent.php" 

「needtogetcontent.php」

<a href="<?=$verymuch?>"><?=$verymuch?></a> 

現在我需要製作這樣的html頁面:

<body> 
    <div id="containt"> 
     <p>already have containt.</p> 
    </div> 
    <div id="other"> 
     <a href="123456">123456</a> 
    </div> 
<body> 

感謝您的幫助:D!

更新:我以前

$('#submit').click(function() { 
    $.ajax({ 
     url: 'abc.php', 
     type: 'POST', 
     data: $('#test').serialize(), 
     success: function(data){ 
      //data will return anything echo/printed to the page. 
      //based on your example, it's whatever $happy is. 
      $('#other').text(data); 
     } 
    }); 
    return false; 
}); 
+0

我們不是做你的工作適合你。顯示你所寫的任何代碼來嘗試這個,我們將盡力幫助解決它。但在此之前,這只是「給我編碼」 –

+0

對不起,如果我的問題讓別人誤解。我的英語不好,所以我很抱歉! 沒有發佈我的真實代碼的原因是爲了讓每件事情都清楚。請原諒我,如果我讓一些瘋狂的男人!:D –

回答

0
$('#test').submit(function(){ 
    $.ajax({ 
     url: 'abc.php', 
     type: 'POST', 
     data: 'sth='+$('#sth').val(), 
     success: function(data){ 
      //data will return anything echo/printed to the page. 
      //based on your example, it's whatever $happy is. 
      $('#other').text(data); 
     } 
    }); 
    return false; 
}); 

我沒有看到一個提交按鈕,所以我不知道,如果這個過程將實際工作。如果<div id="submit">是元素你點擊提交表單,那麼我們就改變 - >

$('#test').submit(function(){ 

$('#submit').click(function(){ 
+0

謝謝男人!但我怎麼能張貼多個數據? –

+0

如果你有多個輸入,你可以使用'$('#test')。serialize()';,如果你有一個名字如'name =「的輸入,那麼值'=」123456「'和'name =」ppi 「value =」7890「',當你使用'serialize()'時,它將被髮送到服務器,像 - >'sth = 123456&ppi = 7890',我們在ajax調用中聲明瞭一種'POST',所以你可以像 - >'$ _POST ['sth']'和'$ _POST ['ppi']'檢索它。 – Ohgodwhy

+1

如果你不想使用serialize方法,你可以像''sth =「+ $('#sth')。val()+」&ppi =「+ $('#ppi' ).val()',它仍然會產生像'sth = 123456&ppi = 7890'這樣的字符串,它仍然可以像php_file中的$ _POST ['sth']和$ _POST ['ppi']'一樣被解析。 – Ohgodwhy

0

執行以下操作:

HTML:

<body> 
    <div id="containt"> 
     <p>already have containt.</p> 
    </div> 
    <div id="other"> 
     <form id="test"> 
      <input id="sth" name="sth" value="123456"/> 
      <div id="submit">Submit</div> 
     </form> 
    </div> 
<body> 

jQuery代碼:

$(document).ready(function() { 

    $('#submit').click(function() { 
     //get sth value 
     var sth = $('#sth').val(); 
     //make ajax call to 'abc.php' passing 'sth' parameter 
     $.post('abc.php', { sth:sth }, function(data) { 
      //if $_POST['sth'] at PHP side is not null 
      if (data != null) { 
       //show the data (in this case: $happy) in the div with id 'other' using '.html()' method 
       $('#other').html(data); 
      } 
     }); 
    }); 

});​ 
+0

謝謝你!但是我怎麼發佈多個數據呢? –

+0

是這樣的:'$ .post('abc.php',{sth:sth,other:other} ...' –