Help Using Javascript Code in a php file - php

I have the code below in a .php file on my site. I would like to be able to type the link to the .php file in the address bar, and for the php script to run on the current page. How could I do this?
PHP File:
<SCRIPT language=JavaScript>
javascript:document.body.contentEditable='true'; document.designMode='on';void(0)
</script>

The way you suggest to do it it's not possible, but you can have a "bookmarlet" which is javascript code that you drag and drop to your bookmarks bar, and every time you click on it, it runs a script.
Try it here: http://jsfiddle.net/XtHsP/

Related

Redirect HTML to PHP with Parameters

I am no web developer and this may be a simple(yet unusual) task, but I have found no solution so far.
On my Webserver (Apache2), I have two files:
index.html
test.php
The test.php needs some parameters to function properly, so I have to call the file this way for example:
www.mydomain.com/test.php?parameter=true
My goal is that I can call the test.php file, when I open the index.html file.
For example:
www.mydomain.com/index.html?parameter=true
This should redirect to/execute test.php AND pass the parameter. Is there any solution for this?
Please note:
The index.html file is a must, I may not replace it with index.php or anything else.
You're gonna have to use JavaScript for this, but it is doable. On the page load, call something like this:
location = "test.php?" + location.search;
Not sure whether it will be a good practice or no but here I have found something which you may try.
Since your execution will start from the html file, you can include a JavaScript code in it and write a code to redirect to necessary URL
e.g.
In your html, you can include a script as follows -
<script type="text/javascript">
window.location.href = "http://www.yourdomain.com/test.php/?parameter=true";
</script>
You may also add script to replace
the 'index.html' with 'test.php'
in your current URL via JavaScript which will redirect you to desired place.
Inside of the '<head></head>' tags of the index.html page, place the following code:
<script type="text/javascript">
window.location = "http://www.yourdomain.com/test.php" + window.location.search;
</script>
The 'window.location.search' part gets the query string from the URL. The query string is the part of the URL that follows and includes the question mark (?).
http://www.yourdomain.com/test.php?hello=world&eddard=stark
Now, everytime someone comes to your page they will immediately be redirected to test.php with the all the accompanying parameters.

Pass value to PHP file using JavaScript and receive new set of values from PHP to JavaScript and display in Browser

I have a page called test.html. This HTML page will contain 2 JavaScripts file. I need to pass a variable value to a PHP file using the first JavaScript file. The user can copy this javascript on as many pages as he wants for displaying output.
The PHP file that receives the variable from the JavaScript file retrieves some data from database depending upon the variable's value. This retrieved value can contain HTML content. This PHP file will always reside on my server.
All of the retrieved content (from the PHP file) needs to be passed to the second JavaScript file so that the data can be displayed in browser. This JS file will need to stay together with the first JS file in order for the data to be displayed.
So I have this:
JavaScript File
<script type= "text/javascript" src="http://www.myserver.com/custom_script.php?unique_id=12"></script>
PHP FILE
//custom_script.php
<?php
$unique_id= (int)$_GET['unique_id'];
$res = db_res(" SELECT col1, col2, col3, .... col10 FROM table WHERE unique_id = $unique_id
LIMIT 1 ");
$rows = mysql_fetch_array($res);
?>
<div id="1"><?php echo $rows['col1']; ?></div>
<div id="2"><?php echo $rows['col2']; ?></div>
<div id="3"><?php echo $rows['col3']; ?></div>
.
.
.
<div id="10"><?php echo $rows['col10']; ?></div>
I need to send all the HTML above from the PHP file to the second JavaScript file so that the output can be displayed. Please note that the CSS styling is also applied using Div ID, so I am expecting those styles would show up too. Please note that there may be more than 10 columns, so an efficient way of passing data is highly appreciated.
So what would be the easiest and the best way to send all the HTML data from the PHP file in one go to the 2nd javascript file residing in test.html page, so that the HTML data can be displayed in test.html file?
EDIT 1:
I apologize to everyone if my question has been confusing. I just thought of an example and hence wanted to add it my edit. I hope you are all aware of what Google Analytics (GA)(or any other Website Visits Stats Tracker) does. Right? You register for a Analytics account and Google gives you a piece of JS code that you copy and paste in your website. And after couple of days, you can login into your GA account to see the stats. Correct? What I am trying to do here is just the same.
Users come to MY WEBSITE and register for an account and I give them JS files that they can paste in their website. The only difference between GA and my website is that GA is personal to you and no one else, but you, the account holder can see it. Whereas in my case, your data can BE SEEN by others as well, as long as you include the JS file on your website. Because users can't just take my PHP file and run it on their server, I am trying to access MY PHP file by giving the full path to it in the JS file.
For example:
<script type= "text/javascript" src="http://www.myserver.com/custom_script.php?unique_id=12"></script>
This is not an actual JS file, rather it is just a medium for my custom_script.php script to receive the unique_id of the user, query MY database and send back the HTML data related to this requesting user. And I am stuck with this part. Hope this clairifies what I am trying to do.
The jQuery documentation actually gives an example almost identical to your problem:
http://api.jquery.com/jQuery.get/
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
This will get an html page and insert it under an html element with class 'result'.
You should be able to replace your php script url and specify where the output should be displayed.
If youre worried about conflicts with other jQuery instances, have a read of this:
http://api.jquery.com/jQuery.noConflict/
If you want to write your own AJAX handler check out this page :
https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest/Using_XMLHttpRequest
Essentially you will do something like this (taken from the link):
function reqListener () {
console.log(this.responseText);
};
var oReq = new XMLHttpRequest();
oReq.onload = reqListener;
oReq.open("get", "yourFile.txt", true);
oReq.send();
I think its worth doing if you've never had to do it before, but you will open yourself up to lots of potential problems that have been solved for you.
Ideally if I were doing this I would return json from the handler and allow the user to decide how to display it, but thats your call.

Embedding php in javascripts

I was trying to embed some PHP into a javscript function that gets called from an onChange in a drop down list. the javascript function is in a .js file, I can get the DDL to call the function, however i cannot get the function to work with the php... eventually i am going to use the value selected in the DDL to access a DB and populate other fields from there, right now i am trying to get it to work with the php:
function controllerType(){
alert('outside php');
<?php
$message = "inside php";
echo "alert('$message');";
?>
}
The function prints the first alert but not the alert called in the php.
Your webserver probably isn't looking for PHP code in .js files by default. You'll have to tell it to either look for PHP in those files or change the file extension to .php.
If you you want to try the former and you're running an Apache web server, try adding the following line to your .htaccess file:
AddHandler application/x-httpd-php .js
Try the following
function controllerType() {
alert('outside php');
alert('<?php echo $message; ?>');
}
You can't call an alert() in PHP.
PHP can just echo text the JavaScript interpreter can interpret as JavaScript.
As for the code you posted, that should all be ran by JavaScript fine. CodePad.
I think the problem might be that your javascript is not being evaluated by your PHP parser.
You might have to create a separate document for that function which ends in .php or using a .htaccess (assuming you're using apache) to tell your php handler to parse .js files
From your following sentence:
eventually i am going to use the value
selected in the DDL to access a DB and
populate other fields from there
It really sounds like you are trying to have the PHP code execute at runtime within the browser. PHP is a server side language and you cannot simply use it in a javascript file as you are trying to.
You will need to have the javascript function post the value from the drop down list to a PHP page through a HTML form or through AJAX.
Forgive me if I misunderstood you.
<?php
$message = "inside php";
?>
<script type="text/javascript">
function controllerType(){
alert('outside php');
alert(<?=$message;?>);
}
</script>

Calling a JavaScript function from PHP

I have a php script that is a bit of a mess and after a form entry, I need to get an address, and display it on a google map. The html and php is crammed into the same script so I essentially need to call the JavaScript as the PHP is happening. Is there a way to do this?
Thanks,
Alex
You can POST your from to a different frame (or iframe), so your page would not reload. The response of your PHP file which comes back to that frame can contain JavaScript code, which will be executed. Something like:
echo('<script type="text/javascript"> alert("Executed on client side"); </script>');
No, PHP executed by the server and returns the full response to the browser. JavaScript in the page is then executed by the client.
You can't call Javascript functions from PHP. You can set the Javascript to run when the page loads instead.
What you want is something like this:
<script type="text/javascript"></script>
var userAddress = "<?php echo $_POST['address']; ?>";
doSomethingWithAddress(userAddress);
</script>
If that code is on the page which you are POSTing the address to, it would take the address from the user, and write it into a javascript tag. The PHP will get executed first on the server, before building the HTML document. This new document has the variable available to the javascript.
I don't know how you would go about doing that, but this seems like a good place to start looking:
http://code.google.com/intl/en/

Variable from JavaScript to PHP

Here is the code:
$('#sousmenu a').click (function (){
startSlideshow(<?php echo json_encode(glob("photos-" .$_GET["folder"]. "/*.jpg"));?>);
return false;
});
The question is I like the HREF to change and get caught by PHP, now it doesn't do anything, but writing the ?folder=portraits works.
Here is the page.
**** Simpler *****
Maybe I am not clear, it happens sometimes!
I want the link href to be send to this PHP function,
<?php echo json_encode(glob("photos-" .(i what the href link). "/*.jpg"));?>
so clicking on the link animaux will send animaux to the glob() PHP function and will get all the .jpg files in the photos-animaux folder.
Clicking on the portraits will send the photo-portraits, etc.
If you want to modify the URL and have the added/changed variable picked by PHP interpreter you have to reload your page. Just altering the URL doesn't do anything because JS is executed after PHP processing.
If your site is on http://example.com and you wish a myparam with value test to be passed to PHP you should add something like this in your JS:
document.location = 'http://example.com?myparam=test';
This will reload your page adding a new param which can be accessed in PHP by simply using $_GET['myparam'] variable.
You may also want to consider using AJAX to dynamically changing the contents of your page without having to refresh the whole page, but that's a little bit more complicated.
Look at the source in your browser.
Php is server-side, and that means you need to use ajax or reload whole page to get a response.
There is a nice ajax part of tutorial on jquery website, after reading it you should be able to do what you want: http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery#Rate_me:_Using_Ajax

Categories