英文:
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>
$("#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>
...
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.
$("#loadingzone").load(
"file.php?text=" + encodeURIComponent(document.getElementById("div1").innerText),
(esp, stat, xhr) => { ... }
);
Now, in file.php script you can access the GET parameter
$textFromAjaxRequest = $_GET['text'];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论