用JS执行php url

时间:2011-08-14 15:15:34

标签: javascript jquery

是否可以简单地使用带有js的URL加载php脚本?

$(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);

 }

3 个答案:

答案 0 :(得分:3)

简单

jQuery和:

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

稍后编辑:

表单使用序列化:

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

答案 1 :(得分:1)

像这样?

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

答案 2 :(得分:1)

使用jQuery执行服务器端页面有多种方法。每个方法都有自己的配置,至少您必须指定要请求的URL。

<强> $。AJAX

$.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