2016-04-15 48 views
1

我的jQuery像this如何調用PHP值的innerHTML與AJAX

我想知道我怎麼可以重複一個PHP變量時的innerHTML輸入有一個值標籤動態生成的輸入字段。

var x2counter = 1; 
var x2data_i = 45; 

function addExam2(d2p_1){ 
    var e = document.getElementById("d2p_1"); 

    e.innerHTML += "<input name='xdata_" + x2data_i + "' placeholder='type in' type='text' value='<?php echo $value;?>'>"; 
    x2counter++; 
    x2data_i++; 
}; 

我知道AJAX可以做到這一點。突然想到說,我有一個像

value.php

$value = ('abc' === 'abc') ? 'right' : 'false' ; 

一個PHP文件,我怎麼能說value.php內的innerHTML所以,這將是這樣的:

... 
$(wrapper).append('<div><input type="text" name="something" value="<?php echo $value;?>"/>'); 
... 

我是jQuery的絕對初學者,所以如果有人能幫助我,我真的很感激。

非常感謝。

+0

你的腳本與你的jQuery存儲在哪裏?如果它存儲在.php文件中,則可以直接回顯該變量。 –

+0

嗨,目前它存儲在一個單獨的main.js文件中。 – John

+3

「我知道* [AJAX](http://api.jquery.com/jquery.ajax/)*可以做到這一點。」......是的,你有沒有試過? ;) – DelightedD0D

回答

2

使用jQuery它可以是這樣的。同時更新您的value.php以返回/回顯該值。如果您需要基於發送內容的具體數值,請使用data發送。

$(document).ready(function() { 
    var max_fields  = 10; //maximum input boxes allowed 
    var wrapper   = $(".input_fields_wrap"); 
    var add_button  = $(".add_field_button"); 
    var x = 1; 
    $(add_button).click(function(e){ 
     e.preventDefault(); 
     if(x < max_fields){ 
      x++; 
      $.ajax({ 
       method: "GET", //or POST whatever you need 
       url: "value.php" 
       //date: //pass what you want 
       ,success: function(value){//If you are successful do 
              //this with the value returned. 
        $(wrapper).append("<div><input type='text' name='something' value='"+ value +"' />"); 
       } 
       //can do fail too 
      }); 
     } 
    }); 
}); 

還有$.get$.post是更具體,但引回$.ajax通話。

0

使用AJAX從您的Web服務器加載變量。有一個文件value.php,只是將你的價值回顯到空白頁面。然後,使用XMLHttpRequest對象的函數getValue()(如下所示)將從網頁中獲取該值。然後在你準備就緒的函數中調用這個函數,並在HTML中追加變量的值。

<script> 
function getValue() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (xhttp.readyState == 4 && xhttp.status == 200) { 
      return xhttp.responseText; 
     } 
     }; 
    xhttp.open("GET", "http://example.com/path/to/file.php", true); 
    xhttp.send(); 
} 

$(document).ready(function() { 
    var max_fields  = 10; //maximum input boxes allowed 
    var wrapper   = $(".input_fields_wrap"); 
    var add_button  = $(".add_field_button"); 
    var x = 1; 
    $(add_button).click(function(e){ 
     e.preventDefault(); 
     if(x < max_fields){ 
      x++; 
      $(wrapper).append('<div><input type="text" name="something" value="'+getValue()+'"/>'); 
     } 
    }); 
}); 
</script> 
+0

感謝您的回答。 – John

0

你給你的PHP文件創建在計算它使不recive從前端任意輸入一個獨立的價值,如果這真的是你的使用情況(存疑,見下文),你可能只是這樣做:

value.php

$value = ('abc' === 'abc') ? 'right' : 'false' ; 
echo $value; 

在你的jQuery:

$.get("example.php", function(result) { 
    alert(result); // result = $value here 
}); 

然而,這樣的設置就不是很實用,它更可能的是,你需要一些數據發送到PHP頁面它處理在這種情況下$value,你幾乎要更多的東西一樣:

value.php

$sentValue = isset($_GET['input']) ? $_GET['input'] : null; 
$value = ('abc' === $sentValue) ? 'right' : 'false' ; 
echo $value; 

在你的jQuery:

$.get("example.php", {"input":"some value"}, function(result) { 
    alert(result); // result = $value here 
});