我想通過使用這個JS代碼的textarea得到一些關鍵字。很明顯,我也需要PHP代碼,但是我遇到了字符串 - var ehy = "php echo $dunno"
的問題。爲什麼這個?誰能幫我?
<?php
if (isset($_POST['line'])) {
$line = $_POST['line'];
$dunno = (explode(" ", $line));
}
?>
<script>
function countLines(){
var stringLength = document.getElementById("myText").value.length;
var count = Math.ceil(stringLength/document.getElementById("myText").cols);
// just devide the absolute string length by the amount of horizontal place and ceil it
return count;
}
function what(){
var n = countLines()
var tarea = document.getElementById('myText')
var lines = tarea.value.split("\n")
//for(var x = 0; x < lines.length; x++) {
$.ajax({
type: "POST",
url: "",
data: "line="+lines,
success: function(){
var ehy = "<?php echo $dunno; ?>"
$('#what').text(ehy)
},
});
//}
}
</script>
</head>
<body>
<h1>SearchFunction()</h1>
<textarea rows="10" cols="70" id="myText"><?php echo "what the hell?";?></textarea>
<input type="button" onclick="what()"/>
<p id="try"></p>
<p id="what"></p>
</body>
</html>
'$ dunno'好像是一個數組,因此使用'「<?php echo $ dunno;?>」'不正確。作爲一般的經驗法則:始終使用'json_encode'將PHP值導出到JavaScript。 – Halcyon
@FritsvanCampen:您應該將其作爲答案發布;你已經擊中了頭部。 (即使它只是一個純字符串,他應該使用'json_encode()',以確保它正確轉義)。 – Spudley