0

I hope this is the right place for this question. I have a function which takes two arguments that are coming from a mysql database and stored in a php variable. the first argument is an int and the second argument is a string. These variables are passed to my javascript function, but when the function is called the first argument is correct but the second argument is undefined and my browser breaks into debug mode.

This is part of my php code that calls the javascript function:

"<td><a href='javascript: confirmDelete(".$row['users_info_id'].", ".$row['firstname'].")' id='delete'>Delete</a></td>"

This is my javascript function:

function confirmDelete(id, dealer){
   alert(id + "<br />" + dealer);
   //var answer = confirm("Are you sure you want to delete " + dealer + "?");
   //if(answer == true){
      //window.location = "process_dealers.php?delete=" + id;
      //alert("Dealer has been deleted from the database!");                
   //}
   //else{
    //alert("Dealer has not been deleted from the database!");
   //}              
}

I have commented most of the code out so I can see what is being returned in the alert function. The first argument returns the correct value but the second argument returns the name of the dealer, but as undefined. I have tried everything and spent last night and this moring trying to figure this out. I would greatly appreciate any suggestions.

4
  • 1
    Can you post the code that calls the confirmDelete function too? It sounds like you're not passing the dealer value when calling it. Commented Jun 29, 2011 at 15:41
  • Can you show us where this function is called? Commented Jun 29, 2011 at 15:42
  • @Francois - '<td><a href='javascript: confirmDelete(".$row['users_info_id'].", ".$row['firstname'].")' id='delete'>Delete</a></td>' Commented Jun 29, 2011 at 15:44
  • When pasting code into a question (or an answer), especially HTML, you need to highlight it and select the { } button on the toolbar (or press Ctrl+k). Commented Jun 29, 2011 at 15:46

1 Answer 1

3

Your problem is that when you are calling the function, the string argument needs to be quoted. The int argument works fine, because ints don't need to be quoted.

"<td><a href='javascript: confirmDelete(".$row['users_info_id'].", \"".$row['firstname']."\")' id='delete'>Delete</a></td>"

Notice the quotes around $row['firstname']. It was undefined, because without the quotes, JavaScript treated the value as a variable name, not a string.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.