如何通过 id 从 html 元素中提取文本并分配给 php 变量?
how to extract text from a html element by id and assign to a php variable?
我有这个:
<h4 class="modal-title" id="exampleModalLabel"> hello </h4>
我想使用 id
提取 hello 单词并将其分配给 php 变量,但我不知道。如果它是输入会更容易,但我必须使用不同的元素。
你可以使用
document.getElementById('exampleModalLabel').innerHTML
这个returnshello
.
根据 docs
The Element.innerHTML property sets or gets the HTML syntax describing
the element's descendants.
要将其分配给 php 变量,请尝试
<?php $text="<script>document.writeln(document.getElementById('exampleModalLabel').innerHTML);</script>";
更新 在你的问题中看到 If it were an input would be easier but I have yo use a different element
使用document.getElementById('exampleModalLabel').innerHTML
设置一个隐藏输入框的值,然后你就可以按照你在问题中所说的那样使用了。
好的,Rene Limon,正如你所知,PHP 变量存在于服务器端,而文本 "hello" 存在于客户端。因此,您需要将值("hello" 或任何其他值)发送到服务器。可以使用 Ajax 来完成。下一个文件 (sendhello.php) 获取标签内的值并将其发送到服务器。第二个文件(sendhelloo.php)获取值并将其存储在变量中。要测试我的代码,您必须创建两个具有给定名称的文本文件,将代码复制粘贴到其中,打开浏览器并键入 "localhost/sendhello.php" :
sendhello.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : {'action':document.getElementById('my_h4').innerHTML },
url : 'sendhelloo.php',
success: function ( data ) {
alert( data );
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<h4 id="my_h4"> hellooo </h4>
<br/>
<button onclick="myAjax()">Send hello to server</button>
</body>
</html>
sendhelloo.php
<?php
session_start();
$_SESSION["my_data"] = $_POST['action']; // STORE VALUE IN VARIABLE.
echo "data received = " . $_POST['action']; // RETURN VALUE TO CONFIRM.
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fltNo= $_REQUEST["txt_fltNo"];
} else{
echo "<form action='' method=POST>
<input type=text name='txt_fltNo'>
<input type=submit value='Get Value'></form>";
}
?>
我有这个:
<h4 class="modal-title" id="exampleModalLabel"> hello </h4>
我想使用 id
提取 hello 单词并将其分配给 php 变量,但我不知道。如果它是输入会更容易,但我必须使用不同的元素。
你可以使用
document.getElementById('exampleModalLabel').innerHTML
这个returnshello
.
根据 docs
The Element.innerHTML property sets or gets the HTML syntax describing the element's descendants.
要将其分配给 php 变量,请尝试
<?php $text="<script>document.writeln(document.getElementById('exampleModalLabel').innerHTML);</script>";
更新 在你的问题中看到 If it were an input would be easier but I have yo use a different element
使用document.getElementById('exampleModalLabel').innerHTML
设置一个隐藏输入框的值,然后你就可以按照你在问题中所说的那样使用了。
好的,Rene Limon,正如你所知,PHP 变量存在于服务器端,而文本 "hello" 存在于客户端。因此,您需要将值("hello" 或任何其他值)发送到服务器。可以使用 Ajax 来完成。下一个文件 (sendhello.php) 获取标签内的值并将其发送到服务器。第二个文件(sendhelloo.php)获取值并将其存储在变量中。要测试我的代码,您必须创建两个具有给定名称的文本文件,将代码复制粘贴到其中,打开浏览器并键入 "localhost/sendhello.php" :
sendhello.php
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type = "text/javascript">
function myAjax () {
$.ajax( { type : 'POST',
data : {'action':document.getElementById('my_h4').innerHTML },
url : 'sendhelloo.php',
success: function ( data ) {
alert( data );
},
error: function ( xhr ) {
alert( "error" );
}
});
}
</script>
</head>
<body>
<h4 id="my_h4"> hellooo </h4>
<br/>
<button onclick="myAjax()">Send hello to server</button>
</body>
</html>
sendhelloo.php
<?php
session_start();
$_SESSION["my_data"] = $_POST['action']; // STORE VALUE IN VARIABLE.
echo "data received = " . $_POST['action']; // RETURN VALUE TO CONFIRM.
?>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$fltNo= $_REQUEST["txt_fltNo"];
} else{
echo "<form action='' method=POST>
<input type=text name='txt_fltNo'>
<input type=submit value='Get Value'></form>";
}
?>