Javascript 将JS变量发送到PHP
我将使用getElementById()获取该值,并将其传递给PHP上的一个变量Javascript 将JS变量发送到PHP,javascript,php,Javascript,Php,我将使用getElementById()获取该值,并将其传递给PHP上的一个变量 <script> function addMachine() { var ip = document.getElementById('ipTextBox').value; document.getElementById('ipTextBox').submit; } <\script> 函数addMachine(){ var ip=document.getElementById('
<script>
function addMachine() {
var ip = document.getElementById('ipTextBox').value;
document.getElementById('ipTextBox').submit;
}
<\script>
函数addMachine(){
var ip=document.getElementById('ipTextBox')。值;
document.getElementById('ipTextBox')。提交;
}
HTML看起来是这样的
<div class="container">
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#addMachine">
<div class="modal fade" id="addMachine" role="dialog">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Add new machine</h4>
</div>
<div class="modal-body">
<label>Insert machine IP<input id="ipTextBox"></input></label>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="addMachine()">ADD</button>
</div>
</div>
</div>
</div>
<\div>
&时代;
添加新机器
插入机器IP
添加
我不知道如何将其传递给PHP变量。PHP是一种后端语言,用于呈现HTML并在页面加载时将其发送给客户端。Javascript是一种客户端语言。如果要将变量从JS发送到PHP,基本上不需要重新加载页面就可以将信息从客户端发送到服务器,则需要使用AJAX: AJAX代表“异步JavaScript和XML”。尽管名称中包含XML,但JSON由于格式更简单、冗余度更低而更常用。AJAX允许用户与外部资源通信,而无需重新加载网页。 Stackoverflow关于Javascript AJAX的文档
- 使用AJAX
function ajax(file, params, callback) {
var url = file + '?';
// loop through object and assemble the url
var notFirst = false;
for (var key in params) {
if (params.hasOwnProperty(key)) {
url += (notFirst ? '&' : '') + key + "=" + params[key];
}
notFirst = true;
}
// create a AJAX call with url as parameter
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
};
xmlhttp.open('GET', url, true);
xmlhttp.send();
}
下面是我们如何使用它:
ajax('cars.php', {type:"Volvo", model:"300", color:"purple"}, function(response) {
// add here the code to be executed when data comes back to this page
// for example console.log(response) will show the AJAX response in console
});
下面显示了如何在cars.php
中检索url参数:
if(isset($_REQUEST['type'], $_REQUEST['model'], $_REQUEST['color'])) {
// they are set, we can use them !
$response = 'The color of your car is ' . $_REQUEST['color'] . '. ';
$response .= 'It is a ' . $_REQUEST['type'] . ' model ' . $_REQUEST['model'] . '!';
echo $response;
}
如果在回调函数中有console.log(response)
,则console中的结果将是:
你的汽车是紫色的。这是一辆沃尔沃300型
- 使用HTML表单
<form action="get_data.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
然而,第二种方法将用户重定向到
get_data.php
,我个人并不喜欢它,因为它的效率更喜欢AJAX。php是一种后端语言,用于呈现HTML并在页面加载时将其发送给客户端。Javascript是一种客户端语言。如果要将变量从JS发送到PHP,基本上不需要重新加载页面就可以将信息从客户端发送到服务器,则需要使用AJAX:
AJAX代表“异步JavaScript和XML”。尽管名称中包含XML,但JSON由于格式更简单、冗余度更低而更常用。AJAX允许用户与外部资源通信,而无需重新加载网页。
Stackoverflow关于Javascript AJAX的文档
- 使用AJAX
function ajax(file, params, callback) {
var url = file + '?';
// loop through object and assemble the url
var notFirst = false;
for (var key in params) {
if (params.hasOwnProperty(key)) {
url += (notFirst ? '&' : '') + key + "=" + params[key];
}
notFirst = true;
}
// create a AJAX call with url as parameter
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
callback(xmlhttp.responseText);
}
};
xmlhttp.open('GET', url, true);
xmlhttp.send();
}
下面是我们如何使用它:
ajax('cars.php', {type:"Volvo", model:"300", color:"purple"}, function(response) {
// add here the code to be executed when data comes back to this page
// for example console.log(response) will show the AJAX response in console
});
下面显示了如何在cars.php
中检索url参数:
if(isset($_REQUEST['type'], $_REQUEST['model'], $_REQUEST['color'])) {
// they are set, we can use them !
$response = 'The color of your car is ' . $_REQUEST['color'] . '. ';
$response .= 'It is a ' . $_REQUEST['type'] . ' model ' . $_REQUEST['model'] . '!';
echo $response;
}
如果在回调函数中有console.log(response)
,则console中的结果将是:
你的汽车是紫色的。这是一辆沃尔沃300型
- 使用HTML表单
<form action="get_data.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
但是,第二种方法会将用户重定向到
get_data.php
,我个人不太喜欢它,更喜欢AJAX的效率。你想把它传递到php
页面吗?你能把它的代码也包括进去吗?有两种方法。Ajax,或formpost。选择更好的一个如果你想接受用户输入,点击提交,并将其发送到PHP,直接的解决方案是使用“是”,我正在尝试,但我不知道如何将其传递到PHP页面,基本上我没有任何PHP代码。如@Superdrac所说,有两种解决方案:使用POST时,您将不使用JS,页面将必须重新加载;使用AJAX时,不需要重新加载页面。我已经在下面的回答中包含了后者。您是否试图将其传递到php
页面?你能把它的代码也包括进去吗?有两种方法。Ajax,或formpost。选择更好的一个如果你想接受用户输入,点击提交,并将其发送到PHP,直接的解决方案是使用“是”,我正在尝试,但我不知道如何将其传递到PHP页面,基本上我没有任何PHP代码。如@Superdrac所说,有两种解决方案:使用POST时,您将不使用JS,页面将必须重新加载;使用AJAX时,不需要重新加载页面。我在下面的回答中包括了后者。