0

i have a button and when the button is click i wan it to call a function to do some processing . However it wouldn't call the function . what when wrong?

<input type="submit" name="submit" onclick="counterminus()" id="submit" value="Buy!" <? function counterminus() { $cmeter = $cmeter - 1; mysql_query("INSERT orders SET quantity='$value',fbId='$fbme',fbName='$fbName', email ='$fbEmail', dealName='$dealName'" ); mysql_query("UPDATE stardeal SET cmeter='$cmeter'WHERE dealId='$dealId'"); echo '<script type="text/javascript">' , 'reloadPage();' , '</script>'; } ?> 
0

4 Answers 4

1

What went wrong is that you are confusing PHP with a client-side language. All PHP processing is done serverside, before the page is sent to the user, thus the client can't call the PHP script without sending a request to the server.

Try hooking up a javascript event to the button, which posts to a PHP script instead.

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

Comments

1

I hope that this will solve your problem.

code:

<html> <body> <?php $a = "hello"; ?> <script> function echoHello() { alert("<?php hello(); ?>"); } </script> <?php function hello() { global $a; echo $a; } ?> <button onclick="echoHello()">Say Hello</button> </body> </html> 

1 Comment

I think you could provide details to the OP what your solution solves. Thanks.
0

PHP is a server-side scripting language, meaning that the script executes when the page is requested from the server, and stops when the client has downloaded the page. Therefore, it is not possible to call a PHP function directly from a web page, as the script is no longer running.

JavaScript, however, starts when the page is loaded, and running until the page is closed. Therefore, you should instead use it to handle button events. If you absolutely need to call PHP code on the server, you can take a look at AJAX.

Comments

0

Call the function in server side side like below and redirect the page after the query is executed.

<?php if(isset($_REQUEST['submit'])) { counterminus(); } function counterminus() { $cmeter = $cmeter - 1; mysql_query("INSERT orders SET quantity='$value',fbId='$fbme',fbName='$fbName', email ='$fbEmail', dealName='$dealName'" ); mysql_query("UPDATE stardeal SET cmeter='$cmeter'WHERE dealId='$dealId'"); Header("location:samePage.php"); } 

?>

 <input type="submit" name="submit" id="submit" value="Buy!" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.