-1

I want to send two values via AJAX.

My jQuery function (working fine with single value on the whole page):

function randomfunction(qq, xxx)
  {
      jQuery.ajax({
       type: "POST",
       url: "https://someurl.php",
       data: "qq="+qq+"&qqq="+xxx,
       cache: false,
       success: function(data)
       {
         jQuery("#stuff"+id).html(data);
       }
     });
 }

My input to trigger it:

echo"<input class='inputbutton' style='width: 122px!important;' type='button' id='button_id' value='Anfordern' onClick='randomfunction($qq, $xxx);'>";

I use this type of setup on my page with single values, which works perfectly. Now I started using 2 and it always gives me the error Uncaught SyntaxError: missing ) after argument list

I already checked and tried these solutions:

I want to pass two parameters in onclick, one is a function & another is a div id

How to send multiple data fields via Ajax?

data: {qq: qq, xxx: xxx},

SyntaxError: missing ) after argument list

JavaScript: SyntaxError: missing ) after argument list

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_parenthesis_after_argument_list

and tried a debug with JSLint and JSHint. I also checked my PHP backend and it works perfectly if I access it manually with something like https://randomlink.php?qq=stuff&xxx=stuff2.

I even clear my cache everytime I change something to prevent loading old code. I just don't get what is wrong. Can anyone help me? Am I too blind to see the obvious?

pr0cz
  • 509
  • 7
  • 22
  • Are you able to console.log the value coming into the function ? like `console.log(qq, xxx)` – Always Helping Aug 05 '20 at 10:46
  • 1
    Thanks for the hint! I couldn't do this. It wouldn't log something. This brought me to the thought, that there is a mistake in my transfer from PHP to JS. I posted an answer which solved it. – pr0cz Aug 05 '20 at 11:11

1 Answers1

0

Okay, so apparently I was searching on the wrong side of the code. The error was not in the function, it was in passing the values from PHP to JS. If one of the values is a string, like an email address, it needs to be quoted. I solved it with this addition to my button:

echo"<input onClick='randomfunction(\"$qq\", $xxx);'>";

Reference: How do I pass a PHP string into a Javascript function call?

pr0cz
  • 509
  • 7
  • 22