How can I mix javascript and php? With one button I want it to call a php function as well as a javascript function?
-
I am really struggling with this. If I use a submit button and then say: <?php if isset($_POST['buttonName']){ ?> call some javascript function. <?php } ?> The above code does not work properly. if do an onclick for the submit button, the function will work sometimes, but sometimes it won't... If I use a button type and onclick set to javascript function. This works great for javascript, but then php does not recognize the click. Please help!
-
Answer:
PHP and JavaScript do not run at the same time. They generally also do not run at same machine. JavaScript code is executed while user is on the page and JavaScript codes run at user's machine. That's why we call Javascript client side language. However PHP is server side language, which means PHP codes are executed at server. Execution of PHP code is over when user sees the page. Therefore PHP cannot know that user clicked a button. On the other hand, you can write a JavaScript function which is triggered by user input(clicking a button for example) and this function can connect to server and make PHP code executed again. AJAX is one of the ways to do this. There are pretty good sources which tells how to use AJAX. Also there are libraries handle that communication by using AJAX.
Burak Yücesoy at Quora Visit the source
Other answers
This question is better for Stack overflow. JavaScript is for modifying the HTML document model. Php is for serving content to a user. They don't mix unless you're trying to modify the document model using information from the server. In that case, search for keyword: AJAX
Willy Zhang
A2A. PHP is server-side scripting and JavaScript is client-side. They don't interact with each other directly. When you click a submit button, it usually redirects the form data to another script (url), which processes it and renders or redirects you to the output. Case 1 (Inside the Browser): You can handle submit requests on client side via JavaScript too by using an onSubmit() callback attached to the form, this is generally used for form validation. If the form isn't validated you can just return false; and it wont submit the form data to the script. Case 2 (Server SIde): However, if you submit the form data to the script, you can only execute JavaScript code when a new page is rendered in your browser and the code is inside the onLoad() callback for the <body> HTML tag. Example code for Case 2: <html> <head> <script type="text/javascript"> function foo() { alert("Here"); } </script> <head> <body <?= isset($_POST['buttonName']) ? "onload='foo()'" : "" ?>> <!-- Content for body here --> </body> </html>
Deepanshu Mehndiratta
I would like you to go through Browser â Server Communication good discussion on this. http://programmers.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming It may help you to understand the different behavior of client and server.
Ashish Kasama
A2A Your immediate solution must be this- <?php if isset($_POST['buttonName']){ ?> <script type="text/javascript">someFunction();</script> <?php } ?> But it is not an efficient way. I say this because if you call the JS function before the page load, and try to access something which has not yet loaded, it would just lead to some weird error that you might not be able to catch. A better way would be to do this. <body onload="someFunctionToCheckFlag()"> ... ... <?php if isset($_POST['buttonName']){ ?> <script type="text/javascript">var flag = true;</script> <?php } ?> ... ... <script type="text/javascript"> var someFunctionToCheckFlag = function() { if (flag) { SomeFunction(); } } </script> I would still not do this, because this looks like a bad way of doing things. If you usually submit a form, your PHP script should do some work and save some data.
Shaumik Daityari
May be i am too much late to write this answer but i want to give my suggestion. We can use an opportunity of simple ajax or jQuery ajax. Below is my suggestion using jQuery ajax(Assumed already included jquery lib ). At HTML page suppose we have a button with id btnId .Then in JavaScript block we can put like . <script type="text/javascript"> $("#btnId").click(function(){ $.ajax({url: "yourPhpPage.php?clicked=Y", success: function(){ yourJavaScriptFunction();// this will call after PHP method execution. }}); }); </script> Now at your PHP page(say yourPhpPage.php) <?php if(isset($_GET['clicked']) && $_GET['clicked']=='Y' ){ //do your php work } ?> Hope this will help to execute your PHP & javascript method method on button click
Manish Srivastava
I find the best way is to pass your data as a json object and do the checks in javascript. The simpliest way is to <script type="text/javascript"> var data = <?= json_encode(array('mytest' => true)) ?>; if (data.mytest) { // do something } </script> I would recommend namespacing but this is provided as a simple solution.
Damian Paul Dennis
Related Q & A:
- How can I check captcha with PHP?Best solution by Stack Overflow
- How can I transfer my address book from one Yahoo account to a new one?Best solution by Yahoo! Answers
- How can i transfer the address book from one yahoo account to another?Best solution by Yahoo! Answers
- How can I move my yahoo bookmarks from one yahoo account to another yahoo account?Best solution by Yahoo! Answers
- How can I import all my contacts from one hotmail address to another?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.