简体   繁体   English

在AJAX中嵌入PHP

[英]Embedding PHP within AJAX

First of all, let me supply what I'm disposing with in this scenario: 首先,让我提供在这种情况下要处理的内容:

index.php index.php

<script text='type/javascript'>
    $(document).ready(function(){
        $('#load').click(load);
    });

    function load(){
        $.ajax({
            data:<?php header('HTTP/1.0 404 Not Found');echo 'Ok'; ?>
        }).done(function(data){
            $('#content').append(data);
        }
    }
</script>

<select id='load'>
   <option value='1'>1</option>
</select>

I need whenever the select menu is clicked, a PHP code snippet to be executed. 每当单击选择菜单时,都需要执行一个PHP代码段。 In this case let's I need the string 'Ok' to be executed (just an example so as to see how it would work). 在这种情况下,我们需要执行字符串“ Ok” (仅是一个示例,以了解其工作方式)。 However I'm not quite confident as far as Ajax is concerned but I'm certain the following is the issue: 但是,就Ajax而言,我不是很自信,但我可以肯定以下是问题所在:

data:<?php header('HTTP/1.0 404 Not Found');echo 'Ok'; ?>

I do not prefer to use url:'' and redirect to a page where the PHP I want to execute is located. 我不喜欢使用url:''并重定向到我要执行的PHP所在的页面。 I need it all in the index.php file. 我需要index.php文件中的所有内容。 As well as that I expect this to be done with Ajax because I need the requests being asynchronous so that the page won't be refreshed. 除此之外,我希望可以使用Ajax完成此操作,因为我需要异步请求,以免刷新页面。

In fact I'm developing a database modifier and I need to list all tables from a corresponding database and I need to validate that through Ajax as you might guess. 实际上,我正在开发数据库修饰符,我需要列出相应数据库中的所有表,并且您需要通过Ajax进行验证。 So I need whenever a database has been chosen to list the tables from within it. 因此,无论何时选择数据库以从其中列出表,我都需要。 I'm posting all this in case it may help. 我会发布所有这些信息,以防可能有所帮助。

代码结构

Thank you in advance for any answers and possible solutions! 预先感谢您提供任何答案和可能的解决方案!

You can't embed PHP into javascript. 您无法将PHP嵌入javascript。 You will have to use AJAX to load the data from another PHP page. 您将必须使用AJAX从另一个PHP页面加载数据。

Something like this: 像这样:

<script text='type/javascript'>
    $(document).ready(function(){
        $('#load').click(load);
    });

    function load(){
        $.ajax({
            url: http://www.example.com/phppage.php,
            type: POST,
            data: { name: "John", location: "Boston" }
        }).done(function(data){
            $('#content').append(data);

        }
    }
</script>

<select id='load'>
   <option value='1'>1</option>
</select>

You can read more on the documentation page for jQuery AJAX . 您可以在jQuery AJAX文档页面上阅读更多内容。

First of all ,there is no way to Embedding PHP within AJAX .PHP is the language which running on the server, and ajax is just the javascript running on your browser. 首先,无法在AJAX中嵌入PHP。PHP是在服务器上运行的语言,而ajax只是在浏览器上运行的javascript。 PHP cant't running on the browser. PHP无法在浏览器上运行。 But your brower can't send http requerst to the PHP server and get the reply from it .So, like Alex Dumitru answered .You need a php page like 'phppage.php' the example which Alex Dumitru mentioned to get your request via ajax.Then you can finsh other functions. 但是您的浏览器无法将http requerst发送到PHP服务器并从中获取答复。因此,就像Alex Dumitru回答的那样。您需要一个像'phppage.php'这样的php页面,Alex Dumitru提到的示例通过ajax来获取您的请求然后,您可以刷新其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM