I have done this several times before but am wondering if perhaps there is a conflict with my bitly class.
I use php to generate a bitly url from long ones. This is stored in a variable called
$url
I can echo the $url variable and know it works fine. However, when I try and place the into the following javascript function (which is called onclick event), the entire action fails.
function fbs_click() {
var uf="<?php echo $url; ?>";
var tf=document.title;
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(uf)+'&t='+encodeURIComponent(tf),'sharer','toolbar=0,status=0,width=626,height=436');
return false;
}
if I replace with an actual URL, i have no problems. Even if I replace with the word "blah", it works. Something about the php echo is throwing it for a loop.
The php echo renders this is source:
var uf=""http://bit.ly/rfEcJl\n"";
My guess is that doing this instead will solve your issue:
var uf = <?php echo json_encode($url); ?>;
Is the url is of some file in the file system and wrongly it is giving you '\' instead of '/'? in which case JS might crash... I guess.
It may be that url is not in proper form. So just try to console/alert thr url in "uf" variable, and url in winodw.open(...) statement and then check.
Related
live at http://vivavidadesign.com/includes/content.php
I have the PHP code:
<?php
if (isset($_GET["#photo"])){
print ("HELLOOOOOOO");
} else {
print ("Nothing Set");
}
?>
I am using this file in to return files in conjunction with AJAX.
Previously going to http://vivavidadesign.com/includes/content.php#photo would trigger (isset($_GET["#photo"])). I can trigger it with ?photo, but I want to know how to trigger using a hash i.e. #photo
The browser is never sending hash to the server, when requesting data.
You have to send it manually, ie. using jquery:
var hash = window.location.hash;
$.ajax({ url: 'content.php?photo=' + hash});
PHP does not grab a hash / anchor.
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
You would have to parse it using Javascript and possibly send via ajax to your php script.
#xxx at the end of URL is not argument to the page but anchor in the page. So PHP doesn't put it in GET variable.
Either use
[code]
http://vivavidadesign.com/includes/content.php?%23photo
[/code]
if you really need the name of variable to be #photo or you can pass variable in the classic wat like this
[code]
http://vivavidadesign.com/includes/content.php?photo=something
[/code]
<?php
if(isset($_GET["photo"])) {
echo $_GET["photo"];
} else {
echo "Nothing Set";
}
?>
And call it:
index.php?photo=file.jpg
Btw. do not use print if it is not needed, use echo instead.
How to get the value after the hash in "somepage.php#name"?
A get is included in the URL after a questionmark, eg.
http://www.example.com/?photo=1
An anchor is included in the URL after a hashtag, eg.
http://www.example.com/#photo
An anchor is never sent to the server from the browser, but you might be able to find it using javascript and through javascript pass it on to the server.
If you want your variable to be a get you need to change the $_GET['#photo'] into $_GET['photo'] in your code and #photo into ?photo=something in your URL.
You have to choose one php or javascript to get value :
PHP
$ar = explode("#",$_SERVER['REQUEST_URI']);
print_r($ar[1]);
Demo : https://eval.in/85871
2 JS:
var hash = window.location.hash;
document.location.href = '/content.php?photo='+hash;
This is similar to this other question.
You do this not with $_GET, but with parse_url().
The part you are looking for is the fragment
Array
(
[path] => includes/content.php
[fragment] => photo
)
Edit to clarify:
If this URL is coming from the client, you're misusing/misunderstanding how the hashtag functions in a URL: http://en.wikipedia.org/wiki/Fragment_identifier . You must parse it client side via javascript.
I have a php script which is editing records in mysql table. I have an issue in refreshing the page using javascript by passing the record number.
Pl check below are the few lines of my php script:
if ($mode == "edit")
{
$ids=$_POST[prs_rid];
$edt1=mysql_query("SELECT * FROM ECRDTL_edit WHERE id='$ids'");
$edt2=mysql_fetch_assoc($edt1);
echo "<script>var x=document.getElementById('prs_rid').value</script>";
echo "<script> alert (x);</script>";
echo "<script>document.location.href='ecr-detail-edit.php?prs_mode=edit&prs_id='+x;</script>";
}
I have used alert to see if variable "x" is getting the record value or not, this works fine. But when i use the same in the next line, it is not showing the same record in the edit mode of my php.
But if I put the same line in address bar of a browser like this, it works fine:
http://www.mydomain.com/ecr-detail-edit.php?prs_mode=edit&prs_id=27
Kindly check what could be the issue or is there any other way of refreshing the page passing the record number.
Just use the location.href object which (as specified in MDN window.location) belongs to the window object, not document.
So your last line of code should read like:
echo "<script>location.href='ecr-detail-edit.php?prs_mode=edit&prs_id='+x;</script>";
On another note, you will get better browser support using the script tags as <script type="text/javascript">
Relative URL's don't work when assigned to location.href, it should be absolute or fully qualified.
In your case, absolute will do:
location.href = location.pathname + '?prs_mode=edit&prs_id=' +
encodeURIComponent(x)
The location.pathname gives the path (starting with /) up to the query separator (question mark).
I've also added encodeURIComponent(x) to make sure the value of x is properly escaped if necessary.
Implementation
echo "<script>location.href = location.pathname + '?prs_mode=edit&prs_id=' + encodeURIComponent(x);</script>";
The problem is that window.location needs an absolute, http:// URL. Use the following code:
window.location=window.location=location.protocol+'//'+location.host+location.pathname+"?get_variables_here";
document.location.href='ecr-detail-edit.php?prs_mode=edit&prs_id='+x;
should be
location.href='ecr-detail-edit.php?prs_mode=edit&prs_id='+x;
and also you should use mysql_real_escape_string() to escape malicious data user may pass on to your script.
hence change
$ids = $_POST['prs_rid'];
to
$ids = mysql_real_escape_string($_POST[prs_rid]);
you are missing script type, when you want to use javascript you need to tell to browser that the code being declared is of javascript, you need to change
<script>
to
<script type="text/javascript">
you are missing single quotes in your POST data. add single quotes to the following.
$ids = $_POST['prs_rid'];
one last thing is i would never output javascript with PHP. it is better you keep javascript and PHP different. for example your above code can be changed to.
<?php
if ($mode == "edit"):
$ids = mysql_real_escape_string($_POST['prs_rid']);
$result = mysql_query("SELECT * FROM ECRDTL_edit WHERE id='$ids'") or die(mysql_error());
$row = mysql_fetch_assoc($result);
?>
<script type="text/javascript">
var x = document.getElementById('prs_rid').value
alert(x);
location.href = 'ecr-detail-edit.php?prs_mode=edit&prs_id='+x;
</script>
<?php endif; ?>
Try this..
echo "<script type='text/javascript'>window.location.href='ecr-detail-edit.php?prs_mode=edit&prs_id='+x;</script>";
For this line to execute successfully, var x=document.getElementById('prs_rid').value in PHP, the html dom should be loaded first else it will give an error, which might be the issue here.
I have this code right now :
$(document).ready(function() {
var ktitle = $('.hiddentwo').text();
$('div#tab2').load('morefour.php?title=ktitle');
});
but the php file doesn't seem to be getting this variable...am I doing something wrong?
I echo something in the php file and I see the text. Also when I echo the $ktitle in the php file I get 'kitle'...that should not be happening :p. This is a stupid question, but I just want to know how to store that variable in the url.
$('div#tab2').load( 'morefour.php?title=' + encodeURIComponent(ktitle) );
try using
$('div#tab2').load("morefour.php", {
title:ktitle
});
or
$('div#tab2').load('morefour.php?title='+ktitle);
UPDATE
In the first case, the data are passed to the script via POST, in the second via GET.
Because you're hardcoding the ?title to "ktitle". If you want to replace this with a variable, you need to concatenate the variable with 'morefour.php?title=' + ktitle.
I'm using jQuery to load the result of a PHP script into a variable. The script is passed something that the user typed with a GET request. I want to take just what the script spit out into its <body> tag. Here's what I've tried:
JS:
function loader() {
var typed = $('#i').val(); //get what user typed in
$.get("script.php", {i: typed}, function(loaded) {dataloaded = loaded;});
alert($(dataloaded).find('body'))
}
But it just displays [Objec object]. How can I get a useful value that is just the contents of the body of a loaded page?
I know the PHP works, I just need the JS.
The script echos something like 1!!2 (two numbers separated by two exclamation points).
Thanks!
You are trying to access the dataloaded which might not be assigned due to the asynchronous nature of AJAX calls. The only safe place to access it is inside the success callback. Also you could use the .html() function to get the contents of the body tag:
function loader() {
var typed = $('#i').val(); //get what user typed in
$.get('script.php', { i: typed }, function(loaded) {
alert($(loaded).find('body').html());
});
}
Also note that if the script.php only echoes 1!!2 without a <body> tag it won't work.
Without knowing what console.log prints it is hard to say, but try these
alert($(dataloaded).find('body').html());
Or
alert($(dataloaded).find('body').text());
I changed the page that I'm trying to fetch to XML. I'm using $.find to get each element of interest individually from the XML page, which suits this particular app well.
This problem has disappeared, as there is no longer a head section to ignore, and I'm just grabbing individual XML elements anyway.
Thanks for all your time and help!
Use JSON type. I am not sure about whether your Jquery script correct or not but using JSON with a correct usage would solve problem. ie.:
function loader() {
var typed = $('#i').val(); //get what user typed in
$.get("script.php", {i: typed}, function(loaded) {dataloaded = loaded;},"json");
alert($(dataloaded).find('body'))
}
And POST variable from script.php after encoding JSON. Use Php's json_encode() function. You need to create variable as an array. For example:
<?php
$title = 'Hello World';
$content = 'Get well soon Japan!';
$arr=array('title'=>$title,'content'=>$content);
echo json_encode($arr);
?>
And Jquery would be something like:
function loader() {
var typed = $('#i').val(); //get what user typed in
$.get("script.php", {i: typed}, function(loaded) {var dataloaded = loaded.title+" "+loaded.content;},"json");
$("body").html(dataloaded);
}
You may need to use Jquery's parseJson() functions on some situations. Don't think you will need here.
I was playing around with AJAX.
If I do
echo "helllo"
in the PHP file it works fine.
However, if I do something like
echo "<script language=Javascript> alert('hi');</script>";
in the PHP file, the alert() does not come up.
Anyone know if I'm doing anything wrong?
example:
in my html file i've got this
<div id='something'> </div>
and i want the response text from the php file be placed above:
if (req.status==200) {
document.getElementById('something').innerHTML=req.responseText;
}
if i changed that to:
if (req.status==200) {
document.getElementById('something').innerHTML="<?php echo 'hi';?>";
}
it works fine, the response text will be ---> hi
but if i do echo "\"<?php echo 'hi';?>\""; in my php file,
the response text will be ""
i hope i was clear in explaining
use $.load() , and the script will be evaluated.
$("#something").load("request.php");
Maybe jQuery there also uses eval() , so it is'nt more safe, but as long as load() only works on the same Domain u should have Control over the things that will be evaluated.
However, it is easier to use, because you did'nt have to parse the Fragment for script's on your own :)
another approach: using eval
var result = ajaxResponseText;// "alert('hi')"; in your case
eval(result);
You must create script tag with returned data;
var script = document.createElement('script');
stript.innerHTML = req.responseText;
document.getElementsByTagName('head')[0].appendChild(script);