1

I am using a php file ( not HTML ). I want to create a button and call a function when I click it. I try it but onclick is not working.

My code:

echo "<input type= 'button' value='save' onclick='save()'>";
echo " <script>
    function save(){ alert('save func');}
</script>";

edit 1: in the web console: save is not defined. The error occures from:

onclick='save()';

i try:

onclick='javascript:save()';

but, the same error.

2
  • To be noted that the OP edited the question after two answers came up fixing the code and making it WORK. There should be more attention when asking. If it's just a minimal example, at least make sure it does not diverge from important parts of the original one. Commented Mar 12, 2016 at 22:07
  • Sorry but, I were hurry. Commented Mar 13, 2016 at 2:10

2 Answers 2

3

You have a couple things wrong in your code. Change it to:

echo "<input type='button' value='save'
    onclick='save()'>";

echo "<script>
    function save(){ alert('save func');}
 </script>";

Here is what I changed:

  • removed white space () between < and input;
  • replaced uppercase letters with lowercase;
  • changed from \ to / in the </script>.

Since you said that it wasn't working, I assume that the missing space was a typo in the question only, so the main problems in your code were: the uppercase F in function and using \ where it should be /.



References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types

JavaScript is case-sensitive [...]


https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Introduction

Tags are enclosed by angle brackets, and the closing tag begins with a forward slash.

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

4 Comments

Sorry for that but, i type the code not copy and paste it. I am sure that my syntax is correct. But, onclick not working!
@Alaa You could check the console and inspect the page's code to see if everything is OK too...
i edit my post. there is no syntax error. i open the console and see that save is not defined!
the error occures here: onclick='save()'. i try: onclick='javascript:save()' but, the same error!
1

First of all: you're using HTML

  • the <\script> tag is not correct, use </script> instead
  • remove the space before < input
  • JavaScript function must be lowercase function

Your code should be something like that

echo "<input type='button' value='save' onclick='save()'>";

echo "<script>
        function save(){ alert('save func');}
    </script>";

EDIT I copied the code on jsbin, and it works fine! https://output.jsbin.com/ratewijaro

6 Comments

Sorry for that but, i type the code not copy and paste it. I am sure that my syntax is correct. But, onclick not working!
i open the web console and it say: save is not defined.
the error occures here: onclick='save()'. i try: onclick='javascript:save()' but, the same error!
the only thing that I can say to you is to inspect your page and post your html code into jsbin and see what's happen
@DiegoMariani isn't it hard when the op seems to hold information or doesn't show the original code? First they clearly lack attention when posting the question. Then we are stuck with a code that seems to work for everyone else but the OP. I'm about to flag the question as off-topic cause the problem can't be reproduced...
|

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.