2017-07-25 78 views
1

我会使用getElementById()来取值,并传递给PHP上的变量。将JS变量发送到PHP

<script> 
function addMachine() { 
    var ip = document.getElementById('ipTextBox').value; 
    document.getElementById('ipTextBox').submit; 
} 
<\script> 

的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">&times;</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> 

我没有关于如何将这些传递给一个PHP变量任何想法。

+0

你想把它传递给'php'页面吗?你能包括它的代码吗? – KaoriYui

+0

有两种方法。 Ajax或表单POST。选择更好的一个 – Superdrac

+0

如果你想接受用户输入,点击提交,并发送到PHP,直接的解决方案是使用[形式](http://php.net/manual/en/tutorial.forms。 php) – Luke

回答

7

PHP是一种后端语言,用于呈现HTML并在页面加载时将其发送给客户端。而Javascript是客户端语言。如果你想从JS发送变量PHP,基本上是从客户端发送信息到服务器,而无需重新加载页面,你需要使用AJAX:

AJAX的全称是“异步JavaScript和XML”。尽管名称包含XML,但由于格式更简单,冗余更低,因此更常用JSON。 AJAX允许用户在不重新加载网页的情况下与外部资源进行通信。 #1的JavaScript的AJAX

  • 文档随着AJAX

我所做的文档页面后,在这儿呢。我希望它能帮助你理解它是如何工作的。

此功能使用GET让我们送参数(对象)到文件(串),并推出了回调(功能)当请求已经结束运行的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)控制台应该是:

你的车的颜色是紫色的。这是一辆沃尔沃300型!

  • 随着HTML表单

在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(目标文件)你必须写:

<?php 
echo $_GET["name"]; 
echo $_GET["email"]; 

然而,第二种方法会将用户重定向到get_data.php,我个人认为它不太喜欢它,并且更喜欢使用AJAX来提高效率。