이 js 코드를 사용하여 텍스트 영역에서 일부 키워드를 가져 오려고합니다. 분명히 PHP 코드도 필요하지만 문자열에 문제가 있습니다 - var ehy = "php echo $dunno"
. 왜? 누구든지 나를 도울 수 있습니까?왜 '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;?> ''는 올바르지 않습니다. 일반적으로 PHP 값을 JavaScript로 내보내려면 항상'json_encode'를 사용하십시오. – Halcyon
@FritsvanCampen : 답변으로 게시해야합니다. 너는 머리에 못을 박았다. 평범한 문자열 일지라도 제대로 이스케이프 처리되도록하려면'json_encode() '를 사용해야합니다. – Spudley