no esta enviado los partcipantes a enviarMensajeWs
ejemplo de como se ve el html renderizado
<div class="borde bloqueChatColab" id="chatcolab-232082" data-post-id="232082" data-participantes=""[\"1\",\"44\"]"" data-conversacion-id="13">
html
function chatColab($var)
It looks like you're encountering an issue where the data-participantes attribute in your rendered HTML is being double-encoded, leading to parsing problems in your JavaScript code. Specifically, the attribute value ends up with extra quotes and backslashes, making JSON.parse fail or produce incorrect results.
Here's the problem breakdown:
Double JSON Encoding: Your $participantes variable in PHP might already be a JSON-encoded string. By calling json_encode($participantes) again, you're effectively encoding it twice.
HTML Attribute Escaping: When you place JSON strings directly into HTML attributes, special characters like quotes can cause issues. Browsers may further escape these characters, leading to additional complications.
Incorrect JavaScript Parsing: Due to the above two points, when you attempt to parse data-participantes in JavaScript using JSON.parse, it doesn't produce the expected array or object.
To fix this issue, you need to ensure that:
You only JSON-encode the data once.
You properly escape the JSON string when embedding it into the HTML attribute to prevent browsers from altering it.
Here's how you can adjust your PHP and JavaScript code:
Updated PHP Code
Modify your chatColab function to correctly handle the $participantes variable:
php
function chatColab($var)
{
$post_id = intval($var['post_id']);
$conversacion_id = intval($var['conversacion_id']);
$participantes = $var['participantes'];
// Determine if $participantes is an array or JSON string
if (is_array($participantes)) {
$participantes_json = json_encode($participantes);
} else {
// Assume it's already a JSON string
$participantes_json = $participantes;
}
// Properly escape the JSON string for HTML attribute
$participantes_escaped = htmlspecialchars($participantes_json, ENT_QUOTES, 'UTF-8');
ob_start();
?>
<div class="borde bloqueChatColab"
id="chatcolab-<?php echo esc_attr($post_id); ?>"
data-post-id="<?php echo esc_attr($post_id); ?>"
data-participantes="<?php echo $participantes_escaped; ?>"
data-conversacion-id="<?php echo esc_attr($conversacion_id); ?>">
<!-- Rest of your HTML -->
</div>
<?php
return ob_get_clean();
}
Explanation:
Use htmlspecialchars with ENT_QUOTES to escape special characters and prevent issues in the HTML attribute.
Check if $participantes is already a JSON string to avoid double encoding.
Updated JavaScript Code
In your JavaScript, when you retrieve data-participantes, you can parse it directly:
javascript
function enviarMensajeColab(button) {
// ... previous code ...
const bloqueChat = button.closest('.bloqueChatColab');
const participantesData = bloqueChat.getAttribute('data-participantes');
// Parse the JSON string directly
const participantes = JSON.parse(participantesData);
// ... rest of your code ...
}
Note: Ensure that the data-participantes attribute does not contain extra quotes or escape characters. After making the PHP changes, the attribute should look something like:
Avoid Double Encoding: Ensure that you're not encoding an already JSON-encoded string.
Proper Escaping: Use htmlspecialchars to escape the JSON string when embedding it into HTML attributes.
Direct Parsing in JavaScript: Retrieve and parse the attribute value directly without worrying about extra quotes or escape characters.
Bonus: Handling Single Values
Since you mentioned that sometimes participantes could be a single value (like "44") or an array (like ["1", "44", "54"]), ensure your JavaScript handles both cases:
javascript
function enviarMensajeColab(button) {
// ... previous code ...
const bloqueChat = button.closest('.bloqueChatColab');
const participantesData = bloqueChat.getAttribute('data-participantes');
let participantes = JSON.parse(participantesData);
// Ensure participantes is always an array
if (!Array.isArray(participantes)) {
participantes = [participantes];
}
// ... rest of your code ...
}
This ensures that participantes is always an array, simplifying further processing.
By making these adjustments, your data-participantes attribute should contain valid JSON that can be parsed correctly in your JavaScript code, resolving the issues you're facing with message sending via WebSocket.