So what my output does is that when I select a tools name it automatically picks the specification on it and shows the control number, the only problem is when I create multiple data to specify my tools name it only shows the first specified tools name in that data and in my console.log it shows an array.
Here is the picture of my form
Here is my console.log(response) output
JavaScript
x
2
1
PSA-10000@BLACK & DECKER@T-1001@PSA-10000@BLACK & GOLD@T-1001@PSA-10000@BLACK & WHITE@T-1001@
2
Here is my console.log(shouldSplit) output
JavaScript
1
13
13
1
(10) ["PSA-10000", "BLACK & DECKER", "T-1001", "PSA-10000", "BLACK & GOLD", "T-1001", "PSA-10000", "BLACK & WHITE", "T-1001", ""]
2
0: "PSA-10000"
3
1: "BLACK & DECKER"
4
2: "T-1001"
5
3: "PSA-10000"
6
4: "BLACK & GOLD"
7
5: "T-1001"
8
6: "PSA-10000"
9
7: "BLACK & WHITE"
10
8: "T-1001"
11
9: ""
12
length: 10
13
Here is the result of what I am getting from ajax to form
JavaScript
1
4
1
PSA-10000
2
BLACK & DECKER
3
T-1001(Just my unique ID nevermind this)
4
Here is my javascript code
JavaScript
1
32
32
1
<script>
2
//Ajax for control number
3
function ToolsChange(element) {
4
let tools_id = $(element).val();
5
6
if (tools_id) {
7
8
$.ajax({
9
type: "post",
10
url: "form_JSON_approach.php",
11
data: {
12
"tools_id": tools_id
13
},
14
success: function(response) {
15
16
var dataSplit = response;
17
console.log(response);
18
var shouldSplit = dataSplit.split("@");
19
var shouldNotSplit = dataSplit.split();
20
console.log(shouldSplit);
21
console.log(shouldSplit[0]);
22
console.log(shouldSplit[1]);
23
console.log(shouldSplit[2]);
24
$("#sel_control_num").val(shouldSplit[0]);
25
$("#sel_tools_spec").val(shouldSplit[1]);
26
$("#sel_tools_id").val(shouldSplit[2]);
27
28
}
29
});
30
}
31
}
32
And here is my ajax code
JavaScript
1
40
40
1
<?php
2
3
include("../include/connect.php");
4
5
6
if(isset($_POST['tools_id'])){
7
8
$ID = $_POST['tools_id'];
9
$query = "SELECT tools_masterlist.control_no, tools_masterlist.tools_id,
10
tools_masterlist.tools_name,
11
tools_spec.model_num,tools_spec.model_num_val, tools_spec.status
12
FROM tools_masterlist LEFT JOIN tools_spec ON tools_masterlist.tools_id =
13
tools_spec.tools_id
14
LEFT JOIN tools_registration ON tools_masterlist.control_no =
15
tools_registration.reg_input WHERE
16
status = 1 AND tools_name = '$ID'";
17
18
19
// $result=mysqli_query($con, "CALL GetAjaxForToolsRegistration('$ID')");
20
$con->next_result();
21
$result=mysqli_query($con, $query);
22
if(mysqli_num_rows($result)>0)
23
{
24
while($row = mysqli_fetch_assoc($result))
25
{
26
// $explodeData = $row['control_no'] . " " . $row['model_num'] .
27
" " . $row['tools_id'];
28
// $pieces = explode(" ", $explodeData);
29
// echo $explodeData[0];
30
echo $row['control_no'] . "@" . $row['model_num'] . "@" .
31
$row['tools_id'] ."@";
32
}
33
}
34
else
35
{
36
// $maxQuery = "SELECT MAX(tools_id) FROM tools_registration";
37
}
38
39
}
40
?>
Advertisement
Answer
You can just add a loop in you success
function to concat/join
all array values.
So, the function would look something like below.
JavaScript
1
21
21
1
success: function(response) {
2
var dataSplit = response;
3
console.log(response);
4
var shouldSplit = dataSplit.split("@");
5
var shouldNotSplit = dataSplit.split();
6
console.log(shouldSplit);
7
console.log(shouldSplit[0]);
8
console.log(shouldSplit[1]);
9
console.log(shouldSplit[2]);
10
$("#sel_control_num").val(shouldSplit[0]);
11
12
var specs = [];
13
for (i=1; i<shouldSplit.length; i+=3){
14
specs.push(shouldSplit[i])
15
}
16
17
$("#sel_tools_spec").val(specs.join(', '));
18
19
$("#sel_tools_id").val(shouldSplit[2]);
20
}
21