I have tried below code
<script type="text/javascript">
var hasvalue = window.location.hash;
</script>
<?php
$ValHas = "<script>document.writeln(hasvalue);</script>";
echo $ValHas."<br>"; // Display #123-12555
echo str_replace("#","",$ValHas); // Display again #123-12555
?>
What I am missing there?
$ValHas is still a string in PHP, with a value of "<script>document.writeln(hasvalue);</script>". It has no #s. It doesn't get turned into '#123-12555 ' until the client's Javascript evaluates the function.
If you want to remove the hash client-side, do this:
var hasvalue = window.location.hash.replace('#', '');
Related
For example:
I declared url in javascript:
<script>
window.location.href = "signup.php#year=" + myyear;
</script>
And in php, I am trying to get #year:
<?php
if(isset($_GET['year'])){
$year = $_GET['year'];
}
?>
thank you in advance!
in simple terms we use ? or / to get the variables and not #
don't used # sign
<script>
var myyear;
window.location.href = "signup.php?year=" + myyear;
</script>
and you can get
<?php
if(isset($_GET['year'])){
$year = $_GET['year'];
}
?>
Actually you just missing one thing on your href.
window.location.href = "signup.php?year=" + myyear
this code results to url with *signup.php?year=2017
Php
You can get it using $_GET['year'] now.
Convert the url string to a PHP url object with the function parse_url and dereference its "fragment" key like this:
$url=parse_url("www.example.com/example/?shareURL&adherentID=ascd#123");
echo $url["fragment"];
The above code returns 123. working example here - http://codepad.org/r8icljcW
If you want to make use of $_GET then you should use a ? instead of #. The ? symbol is the indicator for a GET, not the #.
So simply change your URL structure to:
<script>
window.location.href = "signup.php?year=" + myyear;
</script>
and then, as you already did, grab the value with the GET.
<?php
if(isset($_GET['year'])){
$year = $_GET['year'];
}
?>
If you want to use more parameter, use the &symbol to separate them. You can add as many as you want, you just have to follow the `&key1=value1&key2=value2? structure and you can expand it as long as you want. Example:
<script>
window.location.href = "signup.php?year=" + myyear&month=5;
</script>
Now you could do:
<?php
if(isset($_GET['year'])){
$year = $_GET['year'];
$month = $_GET['month']; // Would assign 5 to month.
}
?>
With scandinavian letters and when encoding them, I have a problem. With code below, javascript add some extra encoding to variable
<script>
function doit(params) {
var url = "/linkto/code.php" + params;
window.open(url,"Doit","width=750, height=600");
}
</script>
<?php
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
echo 'Do it!'; // link to page
?>
When changing code above to php, changed does not happened and problem go away.
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
// link to page
echo '<a href="/linkto/code.php"'.$param.'>Do it!</a>';
Hi all again,
I cannot make it work, no difference between utf-8 or iso-8859-1.
Result is something else, when using javascript-function or direct link.
You can try it here:
http://www.ajl.fi/tmp/test.php
Here is codes:
test.php:
<script type="text/javascript">
function doIt(params) {
var url = "doit.php" + params;
window.open(url,"doit");
}
</script>
<?php
$var1 = 'pähkinä';
$var1 = urlencode($var1);
echo sprintf("Do it - call",$var1)."<br>";
echo sprintf("Do it - link",$var1);
?>
and here is doit.php:
<?php
var_dump($_GET);
?>
In ist code, you have two issues in this code
1) Short tag will not work inside the <?php ?> here:
echo 'Do it!'; // link to page
2) You forgot to add quotes here:
window.open(url,"Doit",width=750, height=600"); //missing quote here
Modified Code:
<?php
$var1 = 'p%E4hkin%E4';
$values = urlencode($var1); // encoding skandinavian letters
$param = '?test='.$values; // add them to variable
?>
Do it!
<script type="text/javascript">
function doit(params) {
var url = "/linkto/code.php" + params;
console.log(url);
window.open(url,"Doit","width=750, height=600");
}
</script>
I answer to myself - Solved.
IE, Edge and Chrome, all working ok on both cases. Firefox has a problem. When using
Do it!
result is not correct, but when using
Do it!
seems to work on all browsers
Framework: CodeIgniter 2.0
I am trying access session data setup in model from view.
This is the code I am testing in view:
<script type='text/javascript'>
function notEmpty(elem, helperMsg){
var num_records = '$this->session->userdata("number_Of_Records_session")';
alert(num_records);
if(elem.value.length > 0){
alert(helperMsg);
elem.focus();
return false;
}else{
window.location="/path/";
}
return true;
}
</script>
When I use PHP code I could retrieve the value from session and display it.
<?php
$numberOfRecords_session =
$this->session->userdata('number_Of_Records_session');
echo "Num records:".$numberOfRecords_session;
?>
But in javascript this line
var num_records = '$this->session->userdata("number_Of_Records_session")';
print this message in alert box:
$this->session->userdata("number_Of_Records_session")
Any advice to retrieve value in javascript and show it alert box is appreciate.
You cannot access your session (stored on server) from JavaScript (executed on client)
So you have to declare a JavaScript variable and define it with PHP
Like this you're defining a string:
...
<script>
var sessionValue = "<?php echo $this->session->userdata('number_Of_Records_session');?>";
</script>
...
If you're sure that there is always a number you could remove the " above, which are defining the variable as string.
If you're not sure about the datatype of the sessionValue you should use parseInt(string,radix):
...
<script>
var sessionValue = parseInt("<?php echo $this->session->userdata('number_Of_Records_session');?>");
if(sessionValue == "NaN") sessionValue = 0;
</script>
...
If I pass an hard coded numeric value from php to javascript, all works perfectly. But if i pass the numeric value from a variable, i get an error:
javascript file (gallery.js)
function goto_anchor(id)
{
var anchor = $("#anchor_" + id);
$('html,body').animate({
scrollTop: anchor.offset().top - 20
}, 1200);
}
php file
....
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script src="js/gallery.js" type="text/javascript"></script><?php
$get_cat = 4;
if (isset($get_cat)) { ?>
<script>
$(document).ready(function() {
goto_anchor(4); // this will work PERFECTLY!!!
goto_anchor(<?php echo $get_cat; ?>); // this will NOT work !!!
});
</script><?php
} ?>
I need to pass the $get_cat variable in my php, not the harcoded numeric value. How ??
Thanks
I have such kind of problems before, can not fill
javascriptfunction(<?php echo $phpvirable ?>)
inside javascript function that causes error; Instead , according to your code, can echo it to javascript virable first before using it;
echo '<script> var get_cat = '.$get_cat.'</script>';
into your php
<?php $get_cat = 4; ?>
surely, Your php $get_cat can be captured from such as $_REQUEST['cat'] dynamic value from form submit event towards this page. then u convert it to javascript virable to use in function.
<?php
if(isset($getcat)):
echo '<script> var get_cat = '.$getcat.'</script>';
endif;
?>
// javascript function read predefined javascript virable that confirm work.
// u also avoid using mixed php and javascript statements which looks messy
<script>
$(document).ready(function() {
goto_anchor(get_cat); // this will work then.
});
</script>
I have function like this:
function SetPageShow (obj)
{
window.location.href="?CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text;
}
and it works fine until I have a page with another GET-values like
http://protectneu/main.php?site=mitarb&liz=260
. then when I call the function SetPageShow, the URL will be
http://protectneu/main.php?CMD=PAGEROWS&PARA=25
and the other values(mitarb and liz) are getting lost. Is there a way to keep them saved and just add the new paramethers. The result that I need is:
http://protectneu/main.php?site=mitarb&liz=260&CMD=PAGEROWS&PARA=25
if (window.location.search)
return window.location.href + "&CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text;
else
return window.location.href + "?CMD=PAGEROWS&PARA="+obj.options[obj.selectedIndex].text;
On the PHP side you need to strip out the parameters that you would send via JavaScript, then build a string with the other parameters, like so:
<?php
$params = $_GET;
unset($params['CMD']);
unset($params['PARA']);
?>
<script>
function SetPageShow(obj)
{
var params = '<?php echo http_build_query($params); ?>';
window.location.href = '?' + (params ? params + '&' : '') + "CMD=PAGEROWS&PARA=" + encodeURIComponent(obj.options[obj.selectedIndex].text);
}
Btw, I've also added encodeURIComponent() in JavaScript to perform proper escaping of the selected value.
Consider using PHP's sessions if you want to retain information like this.
<?php
session_start();
$_SESSION['foo'] = $bar;
?>
Then you can refer to this information on other pages by calling session_start() at the beginning of the page.
<?php
session_start();
$bar = $_SESSION['foo'];
?>