Mario Ene
Mario Ene

Reputation: 853

javascript ajax with many parameters sent to php file

In my HTML file I have the following code at the end:

<script type="text/javascript">
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown)
  {
    $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&voteUp="+voteUp+"&voteDown="+voteDown, function(response){
       // alert("Data: " + data + "\nStatus: " + status);
       alert(response);
    });
  } 
</script>

But I have error when I load the HTML page:

XML Parsing Error: not well-formed Location: http://localhost/ajaxChat/ Line Number 626, Column 55: $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&voteUp="+voteUp+"&voteDown="+voteDown, function(response){ -------------------------------------------------------------^

If I use only one parameter, the HTML page is loading properly:

<script type="text/javascript">
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown)
  {
    $.get("_vote/vote_ajax.php?userID="+userID, function(response){
       // alert("Data: " + data + "\nStatus: " + status);
       alert(response);
    });
  } 
</script>

Upvotes: 1

Views: 42

Answers (2)

Juan Bonnett
Juan Bonnett

Reputation: 1853

Ok, I've had this problem when using links with multiple parameters in Blogger templates, for example, where they go trough an XML parser.

What you have to do is replace the "&" for "&amp;".

That's it!

 $.get("_vote/vote_ajax.php?userID="+userID+"&amp;userName="+userName+"&amp;channelID="+channelID+"&amp;messageID="+messageID+"&amp;voteUp="+voteUp+"&amp;voteDown="+voteDown, function(response){ ...

Upvotes: 0

epascarello
epascarello

Reputation: 207511

Your page is running through an XML parser so looks like you need to add a CDATA block

<script type="text/javascript">
/* <![CDATA[ */
  function voteUp(userID,userName,channelID,messageID,voteUp,voteDown) {
    $.get("_vote/vote_ajax.php?userID="+userID+"&userName="+userName+"&channelID="+channelID+"&messageID="+messageID+"&voteUp="+voteUp+"&voteDown="+voteDown, function(response){
       // alert("Data: " + data + "\nStatus: " + status);
       alert(response);
    });
  } 
/* ]]> */
</script>

Upvotes: 2

Related Questions