简体   繁体   中英

Error in passing argument through a function.

I am having this error in my code..

echo "<input type='button' value='send mails' onclick=\"sendmails(".$sendQuestion.")\">";

i kind of feel stupid posting this question. But I am unable to figure out what's wrong in this code

This code is written in php. it works fine if I don't pass any arguments in the sendmails function, but when I pass argument it gives error as:

missing ) after argument list
  sendmails(Type your Question here testin to post from chrome)

Add a quote around it:

echo "<input type='button' value='send mails' onclick=\"sendmails('".$sendQuestion."')\">";

Without the quotes, javascript misreads your string.

If you take a look at the generated HTML, it'll look something like this :

<input type='button' value='send mails' onclick="sendmails(Type your Question here testin to post from chrome)">

There are some quotes missing arround the string you're passing as parameter to the JS function sendmails

So, I'd say add some quotes arround it ; a bit like this :

echo "<input type='button' value='send mails' onclick=\"sendmails('".$sendQuestion."')\">";

EDIT : added more stuff...

But, if $sendQuestion contains quotes, it'll get you another error... So it might be usefull to

  • bad idea : either replace those ' with \\' with something like str_replace
  • or "transform" the string to a valid-JS one, with, for instance, json_encode

The second solution will get you a PHP code like this one (note that json_encode adds double-quotes arround the string... so it becomes harder to embed directly in the function call... so let's use a variable) :

$sendQuestion = "Type your Question' here testin to post from chrome";
$json = json_encode($sendQuestion);

echo '<script type="text/javascript">' . "\n";
echo 'var myString = ' . $json . ';' . "\n";
echo '</script>' . "\n";
echo "<input type='button' value='send mails' onclick=\"sendmails(myString)\">";

And the generated HTML will be :

<script type="text/javascript">
var myString = "Type your Question' here testin to post from chrome";
</script>
<input type='button' value='send mails' onclick="sendmails(myString)">

Which is much more nice :-)
Maybe not perfect yet... But, by now, I think you get the point ;-)

As a sidenote : json_encode only exists since PHP 5.2... so you might want to check the version of PHP you are using...

The first thing you are doing wrong is posting some PHP code when the error is being reported by your JavaScript engine.

To debug it, the JavaScript is important. We can guess what the JS might look like, but it is much easier if we can see the JS to start with.

In this case, the most probable explanation is that you want to write some JS that has a string literal in it — but you aren't surrounding the JS argument with quote marks.

Think of PHP as a templating engine. Try this:

?>
<input
  type="button"
  value="Send mails"
  onClick="sendmails('<?php echo $sendQuestion; ?>');">
<?php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM