I want to populate the extract records from mysql database. I am using ajax javascript to do the request.
The problem is not populating to input type text area array just bec. of this error.
I want to populate the extract records from mysql database. I am using ajax javascript to do the request.
The problem is not populating to input type text area array just bec. of this error.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 27, 2024, 2:08 AM EDT
The error message “Uncaught TypeError: Cannot set properties of null” typically means that you’re trying to access or modify a property on an object that doesn’t exist or is
null.In your case, it seems like the issue is with this line of code:
document.getElementById("section1_target_action" + [0]).value = section1_action;Here, you’re trying to set the value of an element with the id
"section1_target_action0". However, the error suggests that there’s no such element in your HTML.The problem might be that your textarea doesn’t have an id. You can try adding an id to your textarea like this:
This way, your JavaScript code should be able to find the textarea and set its value. Please make sure the id you’re using in your JavaScript matches the id in your HTML. Also, if you have multiple textareas, each one should have a unique id.
Thanks
Rodel Barbin151 reputationPosted Mar 26, 2024, 10:08 PM EDT
Hi Amit,
Please see code snippet below. The problem here it is not populating to section1_target_action array it return this error.
HTML:
Javascript:
var xmlhttp = new XMLHttpRequest();
//creating URL to php to retrieve data from backend.
url="getSaveDraftExemptValues.php?evaluator_id="+evaluator_id;
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200)
{
var myObj = JSON.parse(this.responseText);
let section1_action = myObj[0].section1_action;
document.getElementById("section1_target_action" + [0]).value = section1_action;
}
}
xmlhttp.send(null);
Back end: PHP
$output = array();
$database = new Database();
$db = $database->getConnection();
$userModel = new Transaction($db);
$evaluator_id = $_GET['evaluator_id'];
$save_draft=1;
$output = $userModel->getSaveDraftEmployee($evaluator_id,$save_draft);
//Send in JSON encoded form
$result= json_encode($output,true);
echo $result;
Thank you.
Amit MohantyRank #112 · 20K reputationPosted Mar 27, 2024, 1:49 AM EDT
You're trying to populate a textarea with the ID "section1_target_action" + [0]. However, this will result in the ID being "section1_target_action0", which might not be what you intended. Try the below code once:
var xmlhttp = new XMLHttpRequest(); //creating URL to php to retrieve data from backend. var url = "getSaveDraftExemptValues.php?evaluator_id=" + evaluator_id; xmlhttp.open("GET", url, true); xmlhttp.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { var myObj = JSON.parse(this.responseText); // Loop through the data and populate textareas for (var i = 0; i < myObj.length; i++) { var section1_action = myObj[i].section1_action; document.getElementsByName("section1_target_action[]")[i].value = section1_action; } } } xmlhttp.send();Amit MohantyRank #112 · 20K reputationPosted Mar 26, 2024, 6:15 AM EDT
Could you please share a minimal, reproducible code snippet to understand the issue? Without seeing your code, it's difficult to provide specific guidance.
Mrunali SawantRank #763 · 3K reputationPosted Jun 6, 2024, 1:28 AM EDT
The "Uncaught TypeError: Cannot set properties of null" error typically indicates that your JavaScript code is attempting to access or modify an element that does not exist in the DOM. This often happens when the script runs before the DOM is fully loaded or if the element's selector is incorrect.
Common Issues and Fixes:
document.getElementById()match the IDs in your HTML.