get value of a php variable set by url in javascript - javascript

Hey i defined a php variable like this inside the body tag:
<?php $v=null;?>
Now i am using this code to check if the variable is set through the url
if ("<?php echo $_GET["v"];?>;" == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
I am using an url like this: www.example.com/index.php?v=12345
When i run this code by opening the index with this url it says v is undefined..
When i run this code by opening the index with "www.example.com/index.php" it is undefined as well.. but not 'null'
What is the problem here? Thank you

Try like
var my_var_arr = location.search.replace('?', '').split('=');
if (my_var_arr[1] == null) {
// Do something because the variable is set through the url
}
else {
// Do something else because the variable is not set through the url
}
Try this FIDDLE

You can use javascript to get the query string parameter instead of php. There is lot of tutorials around here.
Ref: http://javascriptproductivity.blogspot.in/2013/02/get-url-variables-with-javascript.html
Possible solution: jquery get querystring from URL

Related

How to get parameter from URL using javascript

I passed a parameter through an URL using javascript. Here's the code:
<script>
window.onload = function() {
// Creating a cookie after the document is ready
var cookies = document.cookie.split(";")
var cookiePair = cookies[0].split("=");
var cookie_user=cookiePair[1]; // remove ending parenthesis here
window.location.replace("http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username="+cookie_user);
};
</script>
The page that received the parameter is called ChangeInfo
This is what I see in the URL when I get to the ChangeInfo page:
http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username=nitzan
When I'm trying to get the parameter username from the URL, I get this error:
Notice: Undefined index: username in C:\xampp\htdocs\test\ChangeInfo.php on line 5
The way I'm trying to get this parameter is to use $_GET like that: $username = $_GET['username'];
Does anyone know why this makes me a problem?
Thanks in advance
I just solve the problem
I deleted the Page parameter from the URL I created in javascript part.
this is the updated Javascript part:
<script>
window.onload = function() {
// Creating a cookie after the document is ready
var cookies = document.cookie.split(";")
var cookiePair = cookies[0].split("=");
var cookie_user=cookiePair[1]; // remove ending parenthesis here
window.location.replace("http://192.168.206.1/test/ChangeInfo.php?username="+cookie_user);
};
</script>
thank you :)
Ignoring the javascript part, needing to focus on PHP.
You are on this page:
http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username=nitzan
And when you use $_GET['username'] you get the error, that it is not assigned.
It seems that your $_GET is not working at all, probably Apache settings.
Also, it is safer to get GET parameters with isset first.
if(isset($_GET['username']) && $_GET['username']] {
$username = $_GET['username'];
}
else {
$username = '';
}
Then you can compare, if username is set or not in your php code:
if($username) {
//Do something
}
Final thought. Is your first parameter page=http://192.168.206.1/test/ChangeInfo.php working? Can you get it through $_GET?
The problem seems to be just in the way you set and get the url parameter though $_GET. If you use some framework, it might be disabled to use $_GET directly and for example in Symfony you need to use:
$request->get('username');

How to extract the value passed in through onclick function event?

I have this onclick="ShowSingleNew(299)" event for a link and it opens up a new popup, what I'd like to do is extract the 299 (which is an id of the given new item) and pass it in my php script so that that would be the item the user can read.
The php bit looks like so:
if (empty($_GET['news_id'])) { sql select .. }
else { sql select where news_id == $_GET['news_id']; }
So how can I pass this ShowSingleNew id to the next page and give it to the $_GET variable?
You have to pass the newsId using query string like url?newsId=104 format.
The basic format should be: ?strID=XXXX&strName=yyyy&strDate=zzzzz
The sample code looks like:
function ShowSingleNew(newsId) {
var url = "Your url goes here";
window.open(url + "?newsId=" + newsId);
// other code
}
On php side, you could use $_GET['newsId'] to fetch the news id.
Note:
Check how to pass mutiple values in query string here: multivalue query string passing
Reference:
QueryString Structure
Try to send like
function ShowSingleNew(id) {
url = desired_url + '?news_id=' + id;
window.open(url , "_blank");
}

Jquery .get() result evaluation not working

I'm relatively new to javascript, but I've read lots of posts talking about Jquery .get and the need to use a callback function today... but I guess I'm still not doing it right. I have a function that 'gets' the result from a PHP page which accesses a MySQL table.
I've included alerts in my code so I can see that the page is called correctly and is returning the result I expect. If the file already exists in the database then the 'get' returns "exists" if the file is in the database but flagged as deleted 'get' returns "deleted" and if the file isn't in the database 'get' returns nothing.
However, when I place alerts inside the if(file_exists=="exists") statement they never get triggered. I even tried moving the entire evaluation section to another function and calling that function from inside the first. still no joy. docReplace never gets set to true and I never see the dialog asking if I want to replace it.
Please, some helpful suggestions?
<script type = "text/javascript" language = "javascript">
function docCheck(){
var attachfilepath = document.getElementById("AttachFile").value;
var docguid = document.getElementById("DocGuid").value;
//cut the file path down so just get the filename
attachfile = attachfilepath.substr(attachfilepath.lastIndexOf("\\")+1);
// Check to see if file exists.
$.get("Doc_Check.php", {guid:docguid, filename:attachfile}, function(file_exists){
alert(file_exists);
if(file_exists=="exists"){
if(confirm("The file \"" + attachfile +"\" already exists. Would you like to replace it?")){
document.getElementById('docReplace').value = "true";
}else{
return false;
}
}else if(file_exists == "deleted"){
document.getElementById('docReplace').value = "true";
}
});
var rreplace = document.getElementById('docReplace').value;
alert (attachfile);
alert (docguid);
alert (rreplace);
}
</script>

How to check if user is on specific page

Hello I want to check if user is on specific page for example: if he is on localhost/chat show his nick. How can I check it?
If you want to do it at the server side (with php), have a look at http://www.php.net/manual/en/reserved.variables.server.php. You can do something like this:
if($_SERVER['PATH_INFO'] === "/chat") {
// show the name
}
If you want to do it at the client side (with javascript), you can do it with location.href. Here you can do it like this:
if(location.href.split(location.host)[1] === "/chat") {
// show the name
}
use this code...
myUrl = window.location.href; // get the url
lastWordInUrl = myUrl.substring(myUrl.lastIndexOf('/')+1,myUrl.length); // get last word of url
if(lastWordInUrl == 'chat'){...do your stuff...}

Getting portlet session in JS

Hi I am trying to fetch portlet session in my JS variable. How could I go about it
request.getPortletSession().getAttribute("countryList"); //This is my .java class code
I need to fetch the same in JS?
you can't access to java session via javascript.
One solution is to serialize you java object into json string and save it into session(if object is null, just save a string that equals to "null")
getPortletSession().setAttribute("countryListJson", JSONUtil.toJSON(yourObject));
then you can use
<script type="text/javascript">
var obj = <%=request.getPortletSession().getAttribute("countryListJson")%>;
</script>
Another solution is to provide a json padding controller method for ajax call. and return a string like this :
return "var countryList = " + JsonUtil.toJson(request.getPortletSession().getAttribute("countryList")) +";"
then you should mapping this method to a url such as "/contryList.do"
and in your page:
<script type="text/javascript" src="<%=request.getContextPath()%>/contryList.do"></script>
<script type="text/javascript">
alert(contryList);
</script>
As i understand, you need to place in some JS var the countryList (from controller). You don't need to get portlet session in JS, you should be able to do like follows:
I managed to get some strings from controller to js using scriptlets and usebean. Try something like this:
Let's say i need to alert an error from backend and i have put it in a java string named "error"(in controller).
I'm able to do this(doView):
String error = request.getAttribute("error"); // or it can be simply String error = "sometext"
if ((error != null) || (!error.equals(""))){
request.setAttribute("error", error);
}
else request.setAttribute("error","false");
Then, in your jsp, at the top i'll place an usebean tag like:
<jsp:useBean id="error" class="java.lang.String" scope="request"></jsp:useBean>
Then, between script tags:
var error = "<%=error%>";
if (error != "false") alert(error);
As i noticed, you need to set the var in controller (with setAttribute) using an if / else statement.

Categories