2011-08-14 58 views
1

簡單地用js加載一個帶有url的php腳本是否可行?用JS執行php url

$(function() { 

      $('form').submit(function(e) { 

       e.preventDefault(); 

       var title = $('#title:input').val(); 
       var urlsStr = $("#links").val(); 
       var urls = urlsStr.match(/\bhttps?:\/\/[^\s]+/gi); 
       var formData = { 
        "title": title, 
        "urls": urls 
       } 
       var jsonForm = JSON.stringify(formData);     

       $.ajax({ 
        type: 'GET', 
        cache: false, 
        data: { jsonForm : jsonForm }, 
        url: 'publishlinks/publish'      
       }) 

       //load php script 

      }); 

}); 

編輯:

function index() { 

      $this->load->model('NewsFeed_model'); 
      $data['queryMovies'] = $this->NewsFeed_model->getPublications();   
      $this->load->view('news_feed_view', $data); 

} 
+1

你是什麼意思加載? AJAX有什麼問題? – JoshB

+1

您通過*加載URL *瞭解什麼?你期望發生什麼?您想做什麼? –

+0

@Felix Kling url腳本 –

回答

1

這樣嗎?

$.get('myPHP.php', function(data) { 
    $('.result').html(data); 
    alert('Load was performed.'); 
}); 
3

簡單

jQuery和:

<script> 
    $.get('myPHP.php', function(data) {}); 
</script> 

後來編輯:

的形式使用連載:

<script> 
    $.post("myPHP.php", $("#myFormID").serialize()); 
</script> 
+0

你確定執行它嗎?對我而言,它只是向網址發送獲取請求。 –

+0

只要URL被擊中該網址內的代碼執行,因爲PHP代表預處理超文本... –

+0

在我執行的腳本有一個視圖,假設被加載,爲什麼不加載? –

1

有執行服務器端的多種方式使用jQue的頁面RY。每種方法都有自己的配置,至少你必須指定你想要請求的url。

$就

$.ajax({ 
    type: "Get",//Since you just have to request the page 
    url:"test.php", 
    data: {},//In case you want to provide the data along with the request 
    success: function(data){},//If you want to do something after the request is successfull 
    failure: function(){}, //If you want to do something if the request fails 
    }); 

$不用彷徨

$.get("test.php");//Simplest one if you just dont care whether the call went through or not 

$。員額

var data = {}; 
$.post("test.php", data, function(data){}); 

您可以獲取表單數據作爲JSON對象如下

var data = $("formSelector").searialize();//This you can pass along with your request