954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Call a function by onclick event

Hi,
I am trying to call a function by "onclick" event of the button.
When I do that it shows error message.
Can anybody help me out on this so that when I click on the button it should call the function and execute it.

My code looks like:
--------------------------

<?php
function hello()
{
echo "Hello";
}
echo "<input type='button' name='Release' onclick= hello(); value='Click to Release'>";
?>
mana_panigrahi
Newbie Poster
23 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

you can use javascript function when you want to apply any check but in case of php function you have to submit your page

BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 
<?php

print<<<END
<script type="application/javascript">

function hello(clicked)
{
  alert(clicked);
  return false;
}
</script>

<input type='button'
           name='Release'
           onclick='hello("You clicked!");'
           value='Click to Release'>
END;
?>


This is probably closer to what you wanted. 'Onclick' functions must be javascript functions. 'Echo' doesn't exactly work well in HTML; you build HTML/Javascript with PHP. HTML defines the page; Javascript (technically, it's ECMAScript) can dynamically change the HTML, thus changing the viewed page. It can be a chore learning and remembering where things execute and which I/O they have available, but it does stick after a while.

Fest3er
Posting Whiz in Training
242 posts since Aug 2007
Reputation Points: 51
Solved Threads: 35
 

Hi Fest3er,
Thanks for reply...

I tried with the code that you have sent but it shows error message when I click on the button.Please find details of the error attached i.e. "error.jpg"

On more issue is that my requirement is when I click on the button it calls the function.That function should update the database.
I am not sure whether javascript updates the database.

Kindly suggest.

Regards,
-Manoranjan

Attachments error.jpg 14.98KB
mana_panigrahi
Newbie Poster
23 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

use this please

<input type='button' name='Release' onclick='hello();' value='Click to Release'>

<script type="application/javascript">

function hello()
{
  alert("Hello It works");
}
</script>
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 

Hi BzzBee,
Still this code also shows same error....
Could you please share me steps to access the same to make this code working.

Regards,
-Manoranjan

mana_panigrahi
Newbie Poster
23 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

i am sending you a file containing this code. put that file on you WWW directory and run

Attachments test.php (0.21KB)
BzzBee
Posting Whiz
327 posts since Apr 2009
Reputation Points: 16
Solved Threads: 48
 
<script type="application/javascript">
<script type="text/javascript">
function hello() { alert("Hello It works"); }
</script>]
<input type='button' name='Release' onclick='hello();' value='Click to Release'>
almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Hi BzzBee,
Thanks for your reply and sharing the code.
But if I put <?php ?> tag in the code you shared it gives an error
I need <?php ?> to be included as this shared code will be the part of a whole php file.

One more thing can we update database using java script.

Please find my below code which is actually needs to be implemented and it shows error:

function updateRelease()
{
	$JobName1=$arr['JobName'];
	$table_name1 = 'currentexecutions';
	$updateresult="r";
	$sql = "UPDATE ". $table_name1 . " SET tcresult= \"". $updateresult . "\" WHERE JobName=\"" . $JobName1 ."\"";	
	$db->exec_query($sql); 
}
		print "<INPUT TYPE=\"button\" name=\"Click To Release\" onclick=\"updateRelease();\" VALUE=\"Submit\">";


Regards,
-Manoranjan

mana_panigrahi
Newbie Poster
23 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

you do not need <?php ?>
this is a html block
in your php file you can

<?php
//bla bal bla php code
?>
html code block
bla bla
<?php //continue php
//or
echo "html block";
//or
print<<<END
html block
END;
?>


all of which will put the javascript into the php file without error
You need to read more of the php manual

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

You're trying to call a function defined in PHP with an JS event i.e. click of a mouse...

It can't work that way because PHP is being executed on a server side and not on a client side like JS does.
In order to PHP function to work you have to send request to a server where PHP is being executed and then it will go through your code. OnClick event doesn't send anything to the server (well, most of JS functions don't) and you're stuck in there.
Try creating a submit button and in form action define a PHP page to handle the request and then you'll be able to use the PHP function you created.

And the error you're getting is a JS one because JS cannot use PHP variables in that way.

Refer: http://www.php.net/manual/en/language.variables.external.php

Regards,
Rashmi

Rashmi R
Newbie Poster
2 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

here is echo "code"; within text

john_lane
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You