Reputation: 27
I have a simple help request of passing a string with a special character to a javascript function. The problem is that the string gets cut out at the special character.
Passing string: 123-456 on a onlclick function in a php file ends up as -456 without the first part of the string.
first_php_query_file.php {
if ($sql) {
// output data of each row
while ($row = $sql->fetchAll(PDO::FETCH_ASSOC)) {
$orders['data'] = $row;
}
$orders['success'] = true;
} else {
$orders['success'] = false;
}
return $orders;
}
//$orders is data ->
function populateOrders(data) {
//data[i].order_id console.log prints: "123-456" that is correct but when onclick="openOrder(' + data[i].order_id + ')" is rendered to the page and when clicked, the order_id gets passed as: "-456" to openOrder().
// here is the card with onclick:
var row = '<div class="card mb-3"><a class="card link-unstyled" onclick="openOrder(' + data[i].order_id + ')" data-toggle="modal" data-target="#orderModal">
<div class="card-header">Order ' + data[i].order_id + ' ' + status +'</div>
<div class="card-body"><h5 class="card-title">' + data[i].name + ' (' + data[i].number + ')</h5><p class="card-text">' + data[i].place_name + '</p>
<p class="card-text">' + data[i].email +
'</p></div><div class="card-footer text-muted">' + data[i].updateStatus + '</div></a></div>';
}
function openOrder(order_id) {
console.log("Order number: "+order_id); // Prints "Order number: -456" instead of 123-456
}
Thank you for your time in advance :)
Upvotes: 0
Views: 944
Reputation: 714
Pass variable as a string
like this
var row = '<div class="card mb-3"><a class="card link-unstyled" onclick="openOrder("' + data[i].order_id + '")" data-toggle="modal" data-target="#orderModal">
Upvotes: 2