2014-01-28 28 views
1

我爲這個組件創建了一個joomla.in組件我想向用戶顯示一個表單並獲取用戶名並通過ajax.i將她的名字發送到服務器這個代碼用於從default.php文件中獲取用戶的數據。如何在joomla中使用jQuery.post()

<?php 
defined('_JEXEC') or die('Restricted access');?> 
<div id="results"></div> 
    <input type="text" name="name" id="name" value=""> 
    <input type="button" class="button" id="savename" value="Save Name"> 
    <?php 
    $document = JFactory::getDocument(); 
JHtml::_('jquery.framework'); 
    ?> 
    <script> 
    jQuery(document).ready(function(){ 
jQuery("#savename").click(function(){ 
    var name = jQuery('#name').val(); 
    jQuery.post("/components/contactf/ajax.php?name="+name , function(response){ 
       jQuery('#results').html(jQuery(response).fadeIn('slow')); 
    }); 
    }); 
}); 
</script> 

我在「contactf」 .jquery.post組件名稱()不能調用ajax.php file.I認爲問題是 "/components/contactf/ajax.php?name="+name。我不知道如何設置參數爲jQuery.post()正常。我需要使用JPATH_BASE ??

回答

6

嘗試類似,

jQuery("#savename").click(function(){ 
     var name= jQuery("#name").val(); 
     var data = 'name='+name; 
    jQuery.ajax({ 
     type: "POST", 
     url: "index.php?option=com_yourcomponent&task=yourcontroller.your_function", 
     data: data, 
     success: function(data){ 
      alert(data); 

     } 
    }); 
    }); 

這裏yourcontroller是相關的控制器文件和your_function是內部控制的功能。

內,您的控制器功能

function your_function(){ 

    $name = JRequest::getVar('name'); 
    //Do what ever you want 

    exit; 
} 

如果您使用Joomla 3 X請求參數如下。

$jinput = JFactory::getApplication()->input; 
$name = $jinput->get('name'); 

更多receiving post data

希望它是有意義..

+3

'JHTML :: _( 'jquery.framework');'= 3的Joomla,所以你不應該使用'JRequest';) – Lodder

+0

@Lodder我excatly像這樣使用,但不工作:( –

+0

ajax alert返回html頁面的源碼 –

0

創建一個視圖,MVC,例如幫助

在組件的AJAX/view.json.php

,然後在view.json.php運行後的數據發送到這個觀點

jQuery.post("index.php?option=com_contactf&view=ajax?name="+name+"&tmpl=component" , function(response){ 
        jQuery('#results').html(jQuery(response).fadeIn('slow')); 

然後,所有php代碼並以json格式返回結果。