ทำได้แล้วครับ
ผมทำผิดตั้งแต่นำค่าตัวแปลใน php เอามาใส่ในตัวแปล javaScript แล้วครับ
จริง ๆ แล้วเราต้องนำค่าที่เรากรอก ใน textbox ออกมาเลยจะทำให้ ค่าใน javaScript มี \n อยู่
เราก็ทำการ replace ให้ \n เป็นการเว้นวรรคแทนจากนั้นค่อยนำไป split จึงจะได้ค่าอาเรย์ที่แบ่งค่าตามเว้นวรรค ครับ
โค้ด
Code: Select all
<form class="" method="post">
<textarea required rows="15" cols="30" type="text" name="data_set"class="form-control text-left" id="check_number"></textarea>
<input class="btn btn-success" type="submit" name="submit" onclick="myFunction()"/>
<?php
$name = ($_POST['data_set']);
?>
</form>
<button onclick="myFunction()">Try it</button>
<?php echo $name; ?>
<p id="demo"></p>
<script>
function myFunction() {
var str = document.getElementById('check_number').value;
var no = str.replace(/\n/g, " ");
var res = no.split(" ");
document.getElementById("demo").innerHTML = res;
alert(res)

- Selection_999(1439).png (14.46 KiB) Viewed 1082 times