PHP errors trying to call AJAX function
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(开发者_JS百科'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);
}
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.
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.
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"
精彩评论