最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using AJAX to execute PHP code - Stack Overflow

programmeradmin9浏览0评论

I'm not entirely familiar with the MVC model and I cannot get my head around it, however I am trying to simplify things by having my page design separate from my logic. I have my basic template all set up, and I would like hyperlinks to open PHP files within the same page.

For instance:

Next Page

rather than opening nextpage.php, I would like the contents of nextpage.php to open the code into a div on the same page. The contents of nextpage.php will contain some PHP code, and some HTML forms

I'm assuming AJAX is the right approach to this, but any suggestions is greatly appreciated.

Thanks

I'm not entirely familiar with the MVC model and I cannot get my head around it, however I am trying to simplify things by having my page design separate from my logic. I have my basic template all set up, and I would like hyperlinks to open PHP files within the same page.

For instance:

Next Page

rather than opening nextpage.php, I would like the contents of nextpage.php to open the code into a div on the same page. The contents of nextpage.php will contain some PHP code, and some HTML forms

I'm assuming AJAX is the right approach to this, but any suggestions is greatly appreciated.

Thanks

Share Improve this question asked Sep 6, 2013 at 10:59 Dave MeliaDave Melia 3172 gold badges5 silver badges17 bronze badges 3
  • Yes, Ajax is appropriate for this. – The Alpha Commented Sep 6, 2013 at 11:02
  • include your PHP and Ajax code in question – Tahir Yasin Commented Sep 6, 2013 at 11:02
  • link that's the code I wish to display from a .php file onto the same page. – Dave Melia Commented Sep 6, 2013 at 11:23
Add a ment  | 

3 Answers 3

Reset to default 3

take a look to: http://api.jquery./load/

you can load only on part of a page in a html element

$('#result').load('ajax/test.html #container');

Ajax function

$(document).ready(function(){
        $("#submitBtn").click(function() {
                    $.ajax({
                type: "POST",
                url: "request.php", //Your required php page
                data: "id="+ studentid, //pass your required data here
                success: function(response){
                    $('#output').html(response);
                }
            });
        return false;
        });

});

request.php

<?php
$student_id= $_POST['id']; //The data that is passed from tha ajax function
$message = "The id you have entered is".$student_id;
echo $message;//This will be obtained in the ajax response.

I would use a framwork like jQuery and use the ajax() function. Then you must simple execute the statement and in the success handler set content of the div to the received data.

发布评论

评论列表(0)

  1. 暂无评论