使用 AJAX 执行 PHP 代码

Using AJAX to execute PHP code

本文关键字:代码 PHP 执行 AJAX 使用      更新时间:2023-09-26

我对MVC模型并不完全熟悉,我无法理解它,但是我试图通过将页面设计与逻辑分开来简化事情。 我已经设置了所有基本模板,并且我希望超链接可以在同一页面中打开PHP文件。

例如:

下一页

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

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

谢谢

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

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

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

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;
        });

});

请求.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.

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