I have a html/javascript/php
code as shown below in which at the 3rd echo statement
, I have added a button which on click calls on deleteAttribute()
function.
On the 3rd echo statement (type = "button")
, I am getting an error:
"Uncaught SyntaxError: missing ) after argument list"
html/javascript/php:
<html>
<head>
</head>
<body>
<?php
while ($row = $result->fetch_assoc()) {
echo ' <dt><a href="abcde.php?id='.rawurlencode($row['attribute']).'&action=' . rawurlencode($row['action']).'&name=' . rawurlencode($row['map_value_name']).'">'."n";
echo ' '.$row['attribute'].'</a> ('.$row['owner'].')'."n";
echo ' <input type="button" value ="Delete" class="btn btn-map btn-danger" onclick="deleteAttribute('.$row['attribute'].')"/>';
echo ' </dt>'."n";
echo ' <dd>'."n";
echo ' '.$row['description']."n";
echo ' </dd>'."n";
}
?>
</body>
<script>
function deleteAttribute(attribute) {
if(confirm('Are you sure you want to delete ' +attribute+ ' ?')){
jQuery.ajax({
url: 'delete.php',
type: 'post',
data: {
attribute: attribute
},
});
}}
</script>
</html>
Problem Statement:
I am wondering what changes I need to make in 3rd echo statement in the while loop so that I can avoid this error "Uncaught SyntaxError: missing ) after argument list"
.
2
Answers
There was an issue when passing the
$row['attribute']
the quote was mixing so an easy solution is to wrap it in a variable and pass that to the function as below:Here is the working screenshot:
The SyntaxError that you are encountering is a result of failing to encapsulate your filename.
Simply changing
onclick="deleteAttribute(test png000)"
toonclick="deleteAttribute('test png000')"
should resolve your syntaxual error.Without proper encapulation, your script thinks "test png000" is two seperate variables instead of a string that you are attemtping to pass into your funciton.
On closing I did want to bring your attention to utilizing alternative syntax for control structures that will make your codebase more readable and easily debugged without making a mess with redundant echos.
Additionally, instead of concatinating a long list of URL encoded values into a query, you could simply take advantage of PHP’s http_build_query to make your code more readable while keeping things tidy. Please take my code below in reference.
I hope this helps!