使用 php 文件获取 html 文件的元素内容,而不使用表单元素的 post 方法。

huangapple go评论69阅读模式
英文:

Get element contents of an html file using a php file without using post method of Form element

问题

I've translated the text you provided:

我有一个包含一些元素及其内容(可能会更改)的HTML文件。在这个HTML文件中,在脚本部分,我使用jQuery在按下按钮时加载一个PHP文件,但我还希望在加载PHP文件时,PHP文件能够获取加载它的HTML文件的一些(修改后的)元素。

示例

<head>
   <script>
        $("#mybutton").click(function()
            {
                $("#loadingzone").load("file.php",
                        function(resp,stat,xhr)
                        {
                            if (stat=="error") alert("Error "+xhr.status+" : "+xhr.statusText);
                        });
    <script>
<head>

...

<div id="div1">this is a dynamic content that can change</div>

<button id="mybutton">Button to load call the php file</button>

<div id="loadingzone"></div>

...

我希望我的PHP文件能够在通过jQuery脚本加载PHP文件时获取具有id="div1"的div的内容。

我不能将元素放入表单中,因此无法在我的PHP文件中使用$post。我该如何做?

英文:

i've got an html file with some elements and their contents (that can change).
In this html file, in the script section, i use jquery to load a php file when a button is pressed but i also would like that when file php is loaded, the php file could get some (modified) elements of the file html that loaded it.

Example

<head>
<script>

    $(&quot;#mybutton&quot;).click(function()
        {


            $(&quot;#loadingzone&quot;).load(&quot;file.php&quot;,
                    function(resp,stat,xhr)
                    {
                        if (stat==&quot;error&quot;) alert(&quot;Error &quot;+xhr.status+&quot; : &quot;+xhr.statusText);
                    });

&lt;script&gt;

<head>

...

   &lt;div id=&quot;div1&quot;&gt;this is a dynamic content that can change&lt;/div&gt;

   &lt;button id=&quot;mybutton&quot;&gt;Button to load call the php file&lt;/button&gt;

   &lt;div id=&quot;loadingzone&quot;&gt;&lt;/div&gt;

...

I would like my php file could get content of that div ( i mean that with id="div1") when the php file is loaded thought the jquery script.

I can't put the elements in a form and so, i can't use the $post in my php file.
How can i do it?

答案1

得分: 0

你可以通过你的 AJAX 请求发送它。它不能使用 POST 方法。要将它设为 GET 方法,只需将其附加为 URL 的参数。因为它直接传递在 URL 中,所以必须进行编码。

$("#loadingzone").load(
    "file.php?text=" + encodeURIComponent(document.getElementById("div1").innerText),
    (esp, stat, xhr) => { ... }
);

现在,在 file.php 脚本中,你可以访问 GET 参数:

$textFromAjaxRequest = $_GET['text'];
英文:

You can send it via your AJAX request. It must not be the POST method. To make it GET method just append as parameter to the URL. Because it is passed directly in the URL it has to be encoded.

$(&quot;#loadingzone&quot;).load(
    &quot;file.php?text=&quot; + encodeURIComponent(document.getElementById(&quot;div1&quot;).innerText),
    (esp, stat, xhr) =&gt; { ... }
);

Now, in file.php script you can access the GET parameter

$textFromAjaxRequest = $_GET[&#39;text&#39;];

huangapple
  • 本文由 发表于 2023年5月6日 19:05:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188522.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定