2017-07-25 79 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來提高效率。