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'>";
?>

Recommended Answers

All 11 Replies

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

<?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.

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

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>

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

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

<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'>

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

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

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

here is echo "code"; within text

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.