1

I have a link that needs to pass the name of of the div 'bnkcontent' to the AJAX function editcustomer(). I am getting errors from the line of code below.

echo'<a href="#" name="edit" id="edit" onclick="javascript:editcustomer('bnkcontent');">'.'edit details'.'</a>';

below is the function its calling:

function editcustomer(SpanName) 
{
    var address = document.getElementById('address');
    var town = document.getElementById('town');
    var postcode = document.getElementById('postcode');
    var telephone = document.getElementById('telephone');
    var email = document.getElementById('email');
    var opassword = document.getElementById('opassword');
    //alert(opassword.value);
    var password = document.getElementById('password');
    var cpassword = document.getElementById('cpassword');
    var memword = document.getElementById('memword');
    var cmemword = document.getElementById('cmemword');
    var curDateTime = new Date(); //For IE 
    var poststr = "address=" + address.value + "&town=" +town.value + "&postcode=" +postcode.value + "&telephone=" +telephone.value + "&email=" +email.value + "&opassword=" +opassword.value + "&password=" +password.value + "&cpassword=" +cpassword.value + "&memword=" +memword.value + "&cmemword=" +cmemword.value;
    alert(poststr);
    var SpanName = SpanName; 
    makePOSTRequest('test.php', poststr, SpanName); 
}
2
  • What error are you getting? If the PHP echo is exactly what is in your code, then the final ** should be removed. Commented Apr 15, 2011 at 16:13
  • Why don't you use the code block button while posting? Your question was once edited to correct code formating, yet you re-edited it and made the code a mess. Edit your question, select the code blocks and click {}. Commented Apr 15, 2011 at 16:17

3 Answers 3

2

This is the PHP, not the Javascript. Change it to this:

echo'**<a href="#" name="edit" id="edit" onclick="javascript:editcustomer(\'bnkcontent\');">';**

Notice that I put a \ before and after the quotes in editcostomer('bnkcontent'). It would break the quotes that echo it.

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

Comments

1

As you can see in your highlighted code, you forgot to escape the ' in the string:

echo'<a href="#" name="edit" id="edit" onclick="javascript:editcustomer(\'bnkcontent\');">';

Instead of '' you have to make ' to \' because an ' alone would tell the parser that the string ends here.

Comments

0

You have embedded quotes which need to be escaped. Either use alternate quotes and escapes:

echo "**<a href=\"#\" name=\"edit\" id=\"edit\" onclick=\"javascript:editcustomer('bnkcontent');\">";**
     ^--switch to double quotes and do lots of escaping

or

echo '**<a href="#" name="edit" id="edit" onclick="javascript:editcustomer(\'bnkcontent\');">';**
                                                                           ^^--escape them

Since you've got both types of quotes in the string you'r eoutputting, use the quote style that requires the fewest escaping, which'd be single quotes. Otherwise your code gets highly ugly read through because of "leaning toothpick syndrome"

1 Comment

Your first example is wrong. The other attributes would need escaping.

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.