This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to call a PHP function in JavaScript?
i have a form to print to a POS printer. Problem 1 is that i cant print directly, Mybe in google chrome in kioskmode.. so. i would like to call a function after the windows be closed from printing. code is like this:
<script type="text/javascript">
$(function(){
$('#printOut').click(function(e){
e.preventDefault();
var w = window.open();
var printOne = $('.contentToPrint').html();
var printTwo = $('.termsToPrint').html();
w.document.write('<html>' + printOne + '<hr />' + printTwo ) + '</body></html>';
w.window.print();
w.document.close();
I INSERTED HERE>>> insert(); <<<<<<<<<<< AND WORKS BUT NOT BY CLOSING THE priter WINDOWS. I dont want to close the BROWSER!
return false;
});
});
</script>
where can i call the php function insert();
You can't call a PHP function since it's on the sever and your JavaScript code is on the client. You can navigate to a PHP containing that function or make an AJAX call to that page.
Navigating via Javascript would be:
document.location = "yourpage.php"
You cannot call PHP functions in the classic sense with Javascript. PHP code is run on the server, while Javascript code is executed in the user's browser window. What you can do however is use a technology called AJAX to send a request back to your server. Using HTTP methods you could initiate a PHP script that returns information back to your Javascript code also over HTTP.
Related
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I'm having problem with my code below:
jquery
var data = $('thisForm').serialize();
var msg = <?php echo computeAverage(data)?>;
alert(msg);
php
function computeAverage($data){
return $data;
}
I'm getting " data"(string) as the output and not the actual value of data. By the way, I only use one php file that contains the jquery and php function above.
Your help will be appreciated. I really need to figure this out. I'm new to jquery.
thank you for your replies.
given that i need to place my php function to a separate file
jquery
var url = "myPhpFunction.php";
var data = $('thisForm').serialize();
$post(url,data, function(response)); // how can i get the reponse from my url?
php
function computeAverage($data){ // how to convert $data to an array?
return average; // how can i return the average back to my jquery?
}
can anyone help me? thanks
PHP code is executed on the server before the client starts executing Javascript. All the code within <?php ?> are executed first.
After the PHP code has been executed, it will send output to the client which will look like:-
var data = $('thisForm').serialize();
var msg = data; // echo executed
alert(msg);
Now javascript will start executing.
The PHP will not consider javascript variable data as it is a part of client-side scripting.
You cann't pass the value from the javascript to PHP function as PHP execute on SERVER side and Javascript execute Clint side.
You should use the Ajax for doing such thing.
use $.post for sending data to your php script!
<script type="text/javascript">
$(document).ready(function () {
$("#autocomplete").keyup(function(e){
var form = $("#autocomplete");
var data = form.serialize();
$.post("ajax_form.php", data, function(response) {
$( "#autocomplete" ).autocomplete({
source: response
})
})
})
})
</script>
html part:
<input class="formfeld" id="autocomplete" name="suchfeld" title="type "a"">
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to return AJAX response Text?
I'm trying to put together a JQuery timeout function for a form submit action. I have something similar in place via PHP for a page reload, and have the time of the initial page load stored in $_SESSION['pageLoad']. What I'm now trying to do is grab that value and use it in some JQuery math.
After searching around SO, I have arrived at the following:
PHP
<?php
//filename: getsession.php
session_start();
echo json_encode($_SESSION['pageLoad']);
JQuery
$("#sms-creator").submit(function() {
var session;
$.ajaxSetup({cache: false})
$.get('sms_includes/getsession.php', function(data) {
session = data;
});
//at this point I just want to see what I'm getting, hence the next 2 lines...
alert(session);
return false;
});
The path to the PHP file is relative to the form page, not the .js file.
My alert only returns 'undefined'. I'm inexperienced with Javascript and its libraries, so the fault is not apparent to me. Any help appreciated.
ajax call is done asynchronously. so when $.get is executed, request is sent to server for response, however, the javascript program will not wait for the response because it is done asynchronously. when you alert(session), variable session hasn't been set to data returned by server. Try do ajax synchronously, or put alert session in the ajax callback.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating jQuery AJAX requests to a PHP function
I am trying to run a really simple formula,
Call a web page, and every few seconds or so Ajax calls a php function to echo "Hello World"
<?php
function test(){
echo "Hello World";
}
?>
<script type="text/javascript">
function testingTimer()
{
//CALL PHP FUNCTION CALLED TEST
}
setInterval('testingTimer()',5000);
</script>
All I need is the code that calls the already declared php function.
You cannot call that directly. Write code of AJAX and call URL like myfile.php?action=test and in file myfile.php write if action GET variable is equal to test then call that function and don't forget to exit code to prevent any other output.
Google on how to do ajax calls. It's basic knowledge explained in too many tutorials, and varies depending on if you are using a framework or not, and which.
You can post something for example :
function testingTimer()
{
$.post(URL,
{
foo : 'foo'
}
);
}
and in php check post:
if (isset($_POST['foo']))
test();
AJAX does not have the ability to call an arbitrary php function on its own. AJAX allows you to connect to a given page and request data. That being said, you can use AJAX to load a given page and all that page does is call your test function:
Based on your example code I assume you're trying to do this all within a single file. The issue you have is that PHP is run server side before/as the page loads. Once loaded it cannot be re-executed without a refresh.
You need to have your PHP in a separate file so that it can be called over HTTP by AJAX and return a response.
So something like this (if using jQuery):
$.ajax('helloworld.php', function(data){
alert(data);
}, 'html');
Should popup with hello world if implemented correctly.
You should use jQuery to use the ajax function.
You should not use a setInterval, but a setTimeout after the response of server because if the server take more than 5000 seconds to respond, then you'll have two requests at the same time.
If you want to put a timeout, you can always see the abort function on the jqXHR Object.
For example, you could do:
function refresh() {
$.ajax({
type: 'GET', // can be POST or GET
url: 'page.php' // php script to call
// when the server responds
}).done(function(response) {
console.log(response);
// call your function automatically
setTimeout(refresh, 5000);
});
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating jQuery AJAX requests to a PHP function
How can i write PHP in Jquery
is there is a way to do this
$('button').click(function(){
<?php session_destroy();?>
}) ;
You can't execute PHP client-side.
But
You can call a php script on click to do what you want:
$('button').click(function(){
$.get("destroySession.php");
});
You can also get or post some values:
var values = {
destroy: true
};
//get request:
$.get("destroySession.php", values);
//post request:
$.post("destroySession.php", values);
You can't execute PHP client-side.
You cannot. PHP is interpreted on the server side and jQuery is interpreted on the client side.
You should either use an anchor <a href="session_destroy.php"> to go to another PHP page and destroy the session or use AJAX to call the destroy function without leaving the page.
jQuery('button').click( function () { jQuery.get("session_destroy.php"); } );
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
Possible Duplicate:
How to pass a variable / data from javascript to php and vice versa?
I have a file of php and javascript code. I want to set a php variable to be the result of a javascript function which takes a php variable as a parameter. For example:
$parameter = "this is a php variable";
$phpVar = echo "foo(" . parameter . ");";
I know you cannot do a "= echo", is there another way I can do this?
Thanks
You can't directly do that, since JavaScript runs on the client-side and PHP gets executed on the server-side.
You would need to execute the JavaScript first and then send the result to the server via a FORM or AJAX call.
Here's what that might look like:
PHP
$parameter = "this is a php variable";
echo "var myval = foo(" . parameter . ");";
JavaScript
var myval = foo("this is a php variable"); // generated by PHP
$.ajax({
type: 'POST',
url: 'yourphpfile.php',
data: {'variable': myval},
});
Receiving PHP (yourphpfile.php)
$myval = $_POST['variable'];
// do something
PHP code is run on the server before the response is sent; JavaScript is run after, in the browser. You can't set a PHP variable from JavaScript because there are no PHP variables in the browser, where JavaScript if running. You'll have to use an AJAX request from JavaScript to a PHP script that stores whatever information it is you want to get.
Try this:
var a = "Result: "+ <?php echo json_encode($parameter); ?>;
You have to make a GET or POST request to the script from JavaScript.
You cannot pass a js variable to php code.
PHP happens to run on the server, a thousand miles away from client, where js is run.
So, you can only call a php script, using js or regular hyperlink.