【问题标题】:Using AJAX to execute PHP code使用 AJAX 执行 PHP 代码
【发布时间】:2013-09-10 11:01:10
【问题描述】:

我并不完全熟悉 MVC 模型,也无法理解它,但是我试图通过将页面设计与我的逻辑分开来简化事情。我的基本模板已经全部设置好,我想要在同一页面中打开 PHP 文件的超链接。

例如:

下一页

而不是打开nextpage.php,我希望将nextpage.php 的内容打开代码到同一页面上的一个div 中。 nextpage.php 的内容将包含一些 PHP 代码和一些 HTML 表单

我假设 AJAX 是解决此问题的正确方法,但非常感谢任何建议。

谢谢

【问题讨论】:

  • 是的,Ajax 适合这个。
  • 包含您的 PHP 和 Ajax 代码
  • link 这是我希望从 .php 文件显示到同一页面上的代码。

标签: php javascript jquery html ajax


【解决方案1】:

看看: http://api.jquery.com/load/

您只能在 html 元素中加载页面的一部分

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

【讨论】:

  • 啊,正确的加载确实比 ajax() 更好。我保留我的答案作为替代。 +1
  • 对于 php,您也可以像这样将值传递给脚本:$("#divcontainer").load("phpscript.php?value1=number");
【解决方案2】:

我会使用像 jQuery 这样的框架并使用 ajax() 函数。然后您必须简单地执行该语句并在成功处理程序中将 div 的内容设置为接收到的数据。

【讨论】:

【解决方案3】:

Ajax 函数

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 1970-01-01
    • 2018-11-16
    • 2015-07-11
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多