1

I got an Unexpected token }, but I don't know why.

enter image description here

It is a table with some buttons on it. When the buttons is clicked, it will call the Javascript function.

Here is my code:

<tbody>
<?php $i = 0; foreach ($query as $row){?>
<tr>
 <td><?php echo ++$i; ?></td>
 <td><?php echo $row['address'] . ", " . $row['city'] . ", " . $row['state'] . ", " . $row['country']; ?></td>
 <td>
 <button id ="delete" class = "delete" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick="deleteAddress(this.value)">delete</button>
 </td>
 <td>
 //Print this <button id = "edit" class = "edit" type="button" value="7" onClick = "editAddress( this.value, "23", "323", "323", "iij")" >Edit</button>
 <button id = "edit" class = "edit" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick = <?php echo "\"editAddress( this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")\""; ?> >Edit</button></td>
 </tr>
</tbody>
 function editAddress(id, address, city, state, country)
 {
 document.getElementById("address_field").value = address;
 document.getElementById("city_field").value = city;
 document.getElementById("state_field").value = state;
 document.getElementById("country_field").value = country;
 document.getElementById("add_new").innerHTML = 'Update Address';
 document.getElementById("add_new").value = id;
 document.getElementById("add_new").onclick = function() { 
 var id = document.getElementById("add_new").value;
 var address = document.getElementById("address_field").value; 
 var city = document.getElementById("city_field").value;
 var state = document.getElementById("state_field").value; 
 var country = document.getElementById("country_field").value; 
 jQuery.ajax({
 type: "POST",
 url: "../wp-content/plugins/add_address_list/insert_address.php",
 data: {functionName : "updateAddress", id : id, address : address, city : city, state : state, country : country},
 success: function(msg){
 window.location.reload();
 }
 }); 
 };
 } 

The IDE don't have any syntax error. Don't know why. Thanks.

asked Sep 21, 2016 at 2:25
2
  • 1
    Is the error coming from PHP or JavaScript, it's not really clear? You do have several syntax error, for instance onClick = "editAddress( this.value, "23", "323", "323", "iij")" won't work as you have doublequotes around the arguments at the same time as you're using doublequotes for the attribute. Commented Sep 21, 2016 at 2:28
  • Thx. Changed the doublequotes to singlequotes, then it is working now. thanks. Commented Sep 21, 2016 at 2:40

2 Answers 2

1

There error is in:

onClick = "editAddress( this.value, "23", "323", "323", "iij")"

Or rather:

onClick = <?php echo "\"editAddress( this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")'"; ?> >Edit</button>

That should be:

<button id = "edit" class = "edit" type="button" value=<?php echo "\"" . $row['id'] . "\""; ?> onClick ='<?php echo "editAddress( this.value" . ", \"" . trim($row['address']) . "\", \"" . $row['city'] . "\", \"" . $row['state'] . "\", \"" . $row['country'] . "\")"; ?> >Edit</button></td>

Just enclosing the JavaScript with ' so double quotes are allowed so it would look like:

onClick = 'editAddress( this.value, "23", "323", "323", "iij")'
answered Sep 21, 2016 at 2:38
Sign up to request clarification or add additional context in comments.

1 Comment

Changed the doublequotes to singlequotes, And it is working. Thanks!
0

You Just forgot to Close your FOREACH Loop with "}" ;-)

just put

<?php } ?>

after the closing TR ... that should work.

answered Sep 21, 2016 at 2:35

1 Comment

I forgot to copy to here. LOL

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.