Php - run another page and go - php

i've a question, in a php script I've to do some things, and I need to be really fust, but in the script I've to do some database controlls, too, so, I would know if it's possible run an external php page, that do something, but without wait for its results.
Thanks
(P.S.: sorry for my english)

You'll want to look at using Javascript and Ajax. This allows you to run a php script from within a page asynchronously.
If you use a javascript library, such as jQuery then you can use something similar to this:
$.get('my_script.php', function(response) {
// This code is ran when the page has been loaded
// `response` is the content you get back from script
});
For more information have a look at the jQuery documentation on the $.get function.

You can use Ajax to do that, it let another script run in background.

Yes, you can use Ajax for this. Using Ajax you can run php code in the background, however if user navigates to another page, the execution of that php code will be terminated. The simplest way to get started with Ajax is to use a library like jQuery. See http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/
Hope this helps
--EDIT--
This how you can achieve calling exec(php script.php) using Ajax and PHP
home.php (here you are using Ajax to run exec.php)
<!--include the jQuery library -->
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.js"></script>
<script type="text/javascript">
function runInBackground(){
$.get('exec.php', function(data) {
});
}
</script>
exec.php
<?php
exec(php script.php);
?>

Related

Calling a PHP function through an HTML Link (no form)

I have a PHP Function that I would like to integrate into my (existing) web page. Further, I would like it to execute when the user clicks a link on the page. The function needs to accept the text of the link as an input argument.
Everything I've researched for sending data to a PHP script seems to involve using forms to obtain user input. The page needs to accept no user input, just send the link-text to the function and execute that function.
So I guess the question is two-part. First, how to execute a PHP script on link click. And second, how to pass page information to this function without the use of forms. I am open to the use of other technologies such as AJAX or JavaScript if necessary.
EDIT:: Specifically what I am trying to do. I have an HTML output representing documentation of some source code. On this output is a series of links (referring to code constructs in the source code) that, upon being clicked, will call some python function installed on the web server (which leads me to think it needs called via PHP). The python function, however, needs the name present on the link as an input argument.
Is there some sort of interaction I could achieve by having JavaScript gather the input and call the PHP function?
Sorry for the vagueness, I am INCREDIBLY new to web development. If anything is unclear let me know.
You'll need to have a JS function which is triggered by an onclick event which then sends an AJAX request and returns false (so it won't be redirected to a new page in the browser). You can do the following in jQuery:
jQuery:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("myfile.php");
return false;
}
</script>
And in your page body:
Click Me!
In myfile.php:
You can add whatever function you want to execute when the visitor clicks the link. Example:
<?php
echo "Hey, this is some text!";
?>
That's a basic example. I hope this helps.
You will need to use AJAX to accomplish this without leaving the page. Here is an example using jQuery and AJAX (this assumes you have already included the jQuery library):
First File:
<script language="javascript">
$(function(){
$('#mylink').click(function(){
$.get('/ajax/someurl', {linkText: $(this).text()}, function(resp){
// handle response here
}, 'json');
});
});
</script>
This text will be passed along
PHP File:
$text = $_REQUEST['linkText'];
// do something with $text here
If you are familiar with jQuery, you could do the following, if you don't want the site to redirect but execute your function:
in your html head:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
the link:
Execute function
in ajax.php you put in your function to be executed.
Maybe something like this:
....
<script>
function sendText(e)
{
$.ajax({
url: '/your/url/',
data: {text: $(e).html()},
type: 'POST'
});
}
</script>
You can use query strings for this. For example if you link to this page:
example.php?text=hello
(Instead of putting a direct link, you can also send a ajax GET request to that URL)
Inside example.php, you can get the value 'hello' like this:
<?php
$text = $_GET['hello'];
Then call your function:
myfunction($text);
Please make sure you sanitize and validate the value before passing it to the function. Depending on what you're doing inside that function, the outcome could be fatal!
This links might help:
http://net.tutsplus.com/tutorials/php/sanitize-and-validate-data-with-php-filters/
http://phpmaster.com/input-validation-using-filter-functions/
Here's an overly simplistic example of what you're trying to do..
Your link:
Some Action
Your PHP file:
<?php
if (isset($_GET['action']))
{
// make sure to validate your input here!
some_function($_GET['action']);
}
PHP is a server side language i.e. it doesn't run in the web browser.
If you want a function in the browser to operate on clicking a link you are probably talking about doing some Javascript.
You can use the Javascript to find the text value contained in the link node and send that to the server, then have your PHP script process it.

Script-Tags generated by Javascript do not work?

I'm creating a webpage using php, mysql, html5&css3 and javascript (ajax).
To improve the performance, i decided to use AJAX to get only the content div (<div id="content">) that actually changes, i dont want to reload the whole code all the time. That works as follows: after having received the new content from a php-file, javascript does the following:
document.getElementById("content").innerHTML = new_content;
After that, the new_content is well displayed...
Now the Problem:
In the new_content, i have some javascript, too, e.g.:
<script type="text/javascript" lang="JAVASCRIPT">
alert('Hello!');
...
</script>
Unfortunately, when inserting it in the div, it isn't executed at all...when calling the php-file "naturally", the javascript code does what i want it to do, so there's no error in it.
Thanks in advance
EDIT: Problem is solved, i included jquery:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
and used $("#content").html(new_content); in javascript as mentioned below.
Use jQuery's $.html. It will automatically execute scripts for you.
$("#content").html(new_content);
Indeed, by default script execution is disabled when just replacing HTML - makes sense if you look at it. You should really consider using a library like jQuery or Mootools to abstract issues like this away.
If you look at Mootools' Request.HTML implementation you'll see it has its evalScripts option defaulting to true, which later on during the call results in this bit of code being executed:
response.html = text.stripScripts(function(script){
response.javascript = script;
});
...
if (options.evalScripts) Browser.exec(response.javascript);
However this probably won't work on its own without Mootools' other bits of browser-abstracting code in place, so yes I'd definitely recommend including a library on your project.

PHP - Execute a specific php code when clicking a link

I'm currently working on a website and I would like to be able to do the following:
when clicking one of the links from the sideMenu the only thing I would like to change would be the content of my contentMain div and nothing else(page layout/design/etc)
Could anybody give me some general pointers on how I could achieve this in php?
Thank You in advance :D
This is a client-side change that cannot be accomplished using PHP. PHP is evaluated on the server-side, so once the page is loaded for the user, it has no control over what the user sees (unless you use client-side code to call PHP).
To accomplish this, you will need to use Javascript and/or jQuery.
Reference: https://developer.mozilla.org/en/JavaScript/
jQuery: http://jquery.org/
iFrame, frameset or AJAX all work for your case depending on what you are actually trying to achieve.
For AJAX calls (the most modern way out of the three that relies on Javascript) you can use a library such as jQuery.
http://api.jquery.com/category/ajax/
You can use ajax for this one. Using jQuery to detect the click on the link or use normal JavaScript onClick function. Then do the things like you want.
<a href="" id="my_link">My link<a>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#my_link').click(function(){
jQuery.ajax({
url: 'ajax page need to be called',
success: function(data) {
//do your operations on success
}
});
});
});
</script>
You can get more details on :
jQuery
jQuery Ajax
Hope this helps you

about combine ajax ,php and javascript

I have a main html file and I am using ajax to call another php file in same directory.
Inside that php file, I call some external Javascript functions. But my javascripts functions are not working. Is it not possible?
I see generated source in my web browser and it is normal. If i call those functions with using php file (without using ajax), then my functions are working and the generated source same as the previous case. Please help me.
In my html file I am using ajax as follows:
xmlhttp.open("GET","end_location_drop_down.php?q="+str,true);
xmlhttp.send();
in my php file functions as follow,
<?php
echo '<script type="text/javascript" src="../js/pointing.js"></script>'; //external script
$btn8="'btn8'";
$q=$_GET["q"];
echo '<script type="text/javascript">
nextpoint('.$at_id.'); //$at_id mean a variable,nextpoint() is my java script function
</script>';
?>
in my JavaScript function have some image swapping functions.they can call by nextpoint(). But it didn't work.
Javascript functions are executed only when you load the page directly in a browser, as your browser is the one who interprets and executes the javascript code. However when the javascript code is in a different page and that page is being called through AJAX, there is no browser to execute the javascript code before the ajax response is sent back to you. SO it won't work.
Javascript must be executed in browser, so you must insert that script in DOM to current document. Try to insert result of ajax call to DOM.

How do I call a php function from javascript onclick() event?

For example, I have a php function:
function DeleteItem($item_id)
{
db_query("DELETE FROM {items} WHERE id=$item_id");
}
Then, I have something like the following:
<html>
<head>
<script type="text/javascript">
function DeleteItem($item_id)
{
alert("You have deleted item #"+$item_id);
}
</script>
</head>
<body>
<form>
Item 1
<input type="button" value="Delete" onclick="DeleteItem(1)" />
</form>
</body>
</html>
I want to be able to call the PHP function DeleteItem() from the javascript function DeleteItem() so that I can use Drupal's db_query() function, so I don't have to try to establish a connection to the database from javascript.
Does anyone have any suggestions on how this might be done? P.S. I understand that PHP processes on the server-side and javascript processes on the client-side, so please no responses saying that. There has got to be some kind of trick one can do in order to have this work out. Or maybe there is a better way of doing what I am trying to accomplish.
Since you are aware that PHP processes on the server-side and javascript processes on the client-side, you must also realize you can't call a PHP "function" from javascript. Your client side code can redirect to a PHP page, or invoke a PHP program using AJAX. That page or program must be on the server and it should do a lot more than just the one line you have in your function. It should also check for authentication, authorization, etc. You don't want just any client side script anywhere to call your PHP.
You need to write a PHP script which will execute the function. To call it, either:
use XMLHttpRequest (aka Ajax) to send the request
change the page's location.href and return HTTP status code 204 No Content from PHP
You will want to use ajax for that.
Also, database connection from within javascript is something you should not even consider as an option - terribly insecure.
A very simple example:
//in javascript
function DeleteItem($item_id) {
$.post("delete.php", { id: $item_id}, function(data) {
alert("You have deleted item #"+$item_id);
});
}
//in php file
db_query("DELETE FROM {items} WHERE id=" . $_REQUEST["id"]);
you can't actually call PHP functions within JavaScript per se. As #Christoph writes you need to call a PHP script via a normal HTTP request from within JavaScript using the magic that is known as AJAX (silly acronym, basically means JS can load external HTTP requests on the fly).
Take a look at jQuery's AJAX functionality on how to reliably make a HTTP request via JS, see http://docs.jquery.com/Ajax
All the normal security rules apply, i.e. make sure you filter incoming data and ensure it's what you're expecting (the $item_id in your example). Bear in mind there's nothing to stop someone manually accessing the URL requested by your JS.
First, use jQuery.
Then, your code will have to be something like:
<input ... id="item_id_1" />
<script>
$(document).ready(function() {
$('input').click(function(){
var item_id = $(this).attr('id');
item_id = item_id.split('_id_');
item_id = item_id[1];
$.get('/your_delete_url/' + item_id);
});
});
</script>

Categories