Problem in assigning a javascript variable value to a PHP variable

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2882 Views - Last Post: 29 November 2012 - 09:07 AM Rate Topic: -----

#1 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 04:56 AM

This is the first time I am programming in PHP. I seem to have a problem in assigning the value of the variable 'id' to my PHP variable 'samp'.

Here is my code for that:

<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function test(id)
{
	alert("I am inside javascript: "+id);//Alert box shows perfectly without errors
	
/*function test1()
{
	var id2=id;
	alert("I am inside php" +id2);
	return id;
}*/

//document.write("value of temp is ",temp);

<?php 

if(isset($_GET['id']))
 {
   $samp = $_GET['id'];
 }		
echo 'alert("I am inside php" .$samp);';//This shows 'undefined
?>
}
</script>		
</head>
<body>
	<input type="button" onclick="test(555);" value="Button">
</body>
</html>



Now, what is happening is that, first a value of 555 is getting passed to test and an alertbox is showing
I am inside javascript:555

The next alert box is showing 'Undefined'.


I have gone through various examples regarding this but am not able to find out the main reason behind this. Any help will be appreciated.

Thanks

Regards,
Raghav

Is This A Good Question/Topic? 0
  • +

Replies To: Problem in assigning a javascript variable value to a PHP variable

#2 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 04:59 AM

Hello,

$samp is only accessible during the isset scope and is not available straight after it. If you declare something in a scope, it stays in that scope. What you want to do is declare it outside the scope as null perhaps and modify it if your isset scope is true. Then before echoing your alert statement, check if it's null or not.
Was This Post Helpful? 1
  • +
  • -

#3 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 05:56 AM

Scope is one, but there's also second reason:
echo 'alert("I am inside php" .$samp);';//This shows 'undefined

What's wrong with this line? One thing to consider is probably just a typo (a dot instead of a comma[ugh, sorry] + operator that would concat strings in Javascript). Second thing is single-quoted string.

edit: Actually, I'd bet that it's the usage of dot is what gives you "Undefined", and not scope issue.

AFAIK in PHP when you define a variable like this, it will be accessible outside of that block like it was declared before it. That's maybe not what you'd expect from C/C++/Java, but PHP is PHP and it does things its own way. ;)
Blocks ( { ... } ) like these with if/for/while etc do not introduce additional scope like functions do, and so you can actually have these shenanigans where you define a variable in nested loop and it's available after it's all done.

Of course, one can argue that in this case defining the variable before would be better style, but if you don't do so - it's still legal. Maybe bit ugly and less readable, but legal.

if (true) { $hello = 1; }
if ($hello) { $world = 0; }
var_dump($hello . $world);

// 10

This post has been edited by Xupicor: 27 November 2012 - 06:22 AM

Was This Post Helpful? 1
  • +
  • -

#4 Kruithne  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 99
  • View blog
  • Posts: 442
  • Joined: 28-July 09

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 05:58 AM

View PostXupicor, on 27 November 2012 - 12:56 PM, said:

Scope is one, but there's also second reason:
echo 'alert("I am inside php" .$samp);';//This shows 'undefined

What's wrong with this line? One thing to consider is probably just a typo (a dot instead of a comma). Second thing is single-quoted string.


Nice catch, I passed over that too easily. However, I don't think the dot is a typo, I have a feeling OP has combined both PHP and Javascript in multiple ways, one of which being the PHP way to concat' strings together, hense the dot.
Was This Post Helpful? 1
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 06:22 AM

If so, then dot does not concat strings in Javascript, and it is passed verbatim (and still will be even if you use double quotes). So it was rather going to be + operator. ;)

edit: Or it should be dot indeed, and $samp evaluates to some String method. Who knows, I sure as hell won't try to understand what's going on in that math-head of his. ;P

This post has been edited by Xupicor: 27 November 2012 - 06:25 AM

Was This Post Helpful? 1
  • +
  • -

#6 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 07:05 AM

Mixing Javascript and PHP together like that can get messy very quick. They aren't even processed on the same machine.
Was This Post Helpful? 1
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 07:12 AM

Aye it can and does. And of course they are not on the same machine (unless they are, but even then that doesn't matter at all).
Raghav here understands that, I wager. (You do, don't you?) Hopefully he also understands a need for content/logic separation. ;)
Was This Post Helpful? 1
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 09:21 PM

View PostKruithne, on 27 November 2012 - 05:29 PM, said:

Hello,

$samp is only accessible during the isset scope and is not available straight after it. If you declare something in a scope, it stays in that scope. What you want to do is declare it outside the scope as null perhaps and modify it if your isset scope is true. Then before echoing your alert statement, check if it's null or not.


Thank you,Kruithne. I tried to add the echo inside the scope with an if statement and what happens is, that part of the code doesn't seem to get executed at all. It just seems to skip the part. I guess its because the value becomes NULL...

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#9 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 09:37 PM

View PostXupicor, on 27 November 2012 - 06:26 PM, said:

Scope is one, but there's also second reason:
echo 'alert("I am inside php" .$samp);';//This shows 'undefined

What's wrong with this line? One thing to consider is probably just a typo (a dot instead of a comma[ugh, sorry] + operator that would concat strings in Javascript). Second thing is single-quoted string.

edit: Actually, I'd bet that it's the usage of dot is what gives you "Undefined", and not scope issue.


Thanks, Xupicor but I tried using a + instead of '.' and that part of the code didn't execute at all. What I want to do is use an alertbox which displays the php variable var, so I tried using it like that.

regards,
Raghav

View PostKruithne, on 27 November 2012 - 06:28 PM, said:

View PostXupicor, on 27 November 2012 - 12:56 PM, said:

Scope is one, but there's also second reason:
echo 'alert("I am inside php" .$samp);';//This shows 'undefined

What's wrong with this line? One thing to consider is probably just a typo (a dot instead of a comma). Second thing is single-quoted string.


Nice catch, I passed over that too easily. However, I don't think the dot is a typo, I have a feeling OP has combined both PHP and Javascript in multiple ways, one of which being the PHP way to concat' strings together, hense the dot.


Right,Kruithne. You are indeed right that I want to use a javascript to make an alertbox which displays the php variable of $samp.

regards,
Raghav

View PostXupicor, on 27 November 2012 - 06:52 PM, said:

If so, then dot does not concat strings in Javascript, and it is passed verbatim (and still will be even if you use double quotes). So it was rather going to be + operator. ;)/>

edit: Or it should be dot indeed, and $samp evaluates to some String method. Who knows, I sure as hell won't try to understand what's going on in that math-head of his. ;P


Well Xupicor, the dot was for the php variable so that it gets read in the alertbox.Am I doing it wrong? IS there any other way of doing the same?

regards,
Raghav

View PostSho Ke, on 27 November 2012 - 07:35 PM, said:

Mixing Javascript and PHP together like that can get messy very quick. They aren't even processed on the same machine.


Well, I know that javascript is client side and php is server side. But what I want to do is transfer that value of the javascript variable to the php variable and my boss says its possible as well.

regards,
Raghav

View PostXupicor, on 27 November 2012 - 07:42 PM, said:

Aye it can and does. And of course they are not on the same machine (unless they are, but even then that doesn't matter at all).
Raghav here understands that, I wager. (You do, don't you?) Hopefully he also understands a need for content/logic separation. ;)/>


Totally, Xupicor. But what is the issue here is to make that value of javascript to get assigned to the php variable $samp.

Well, I guess I am missing something which is very simple. I do hope that I will find out what is it very soon :)

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#10 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 09:51 PM

If you absolutely must use values found within PHP in your javascript, change line 24 to this:
echo 'alert("I am inside php ' . $samp . '");';



Really, though, there should be no need for using PHP variables in a client-side language like Javascript. It'll only obfuscate your code more and more and waste precious time debugging when other things could be done instead.


EDIT:ALso, your conditional checking to see if $_GET['id'] is checked doesn't even include all of the php code. Unless $samp has some default value if $_GET['id'] is set(which it doesn't in the code you posted), line 24 should be inside the if block

This post has been edited by Sho Ke: 27 November 2012 - 09:54 PM

Was This Post Helpful? 1
  • +
  • -

#11 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 27 November 2012 - 10:35 PM

Thank you,Sho Ke. Now my code reads something like this.

function test(id)
{
	alert("I am inside javascript: "+id);//no problems here
	
/*function test1()//attempt 1 which went in vain
{
	var temp=id;
	alert("I am inside php" +id2);
	return temp;
}*/

//document.write("value of temp is ",temp);//attempt 2 with no desired result

<?php 

$samp=555;//just some default value

$samp = isset($_GET['id']);//is this helpful??
  
echo 'alert("I am inside php' . $samp .'");';//revised according to Sho Ke's suggestion
 
?>
 
}



Now what happens is that the second alertbox just shows

I am inside php


What I want it to do is to get the value of id like

I am inside php (value of id)


Any idea on how to go about it?

regards,
Raghav

This post has been edited by raghav.naganathan: 27 November 2012 - 10:36 PM

Was This Post Helpful? 0
  • +
  • -

#12 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 28 November 2012 - 04:02 AM

No, now that's just confusion squared. ;)/>/>

I concur that usually if you want to do this, you may want to rethink why.

But you can do it in principle:
<?php
$message = "Hello World";
echo "alert('$message')";


Notice that I used double quotes so $message inside of the string would be parsed as variable, and also notice that I wrapped it using single quotes so this line after PHP parses it reads:
alert('Hello World')
Instead of:
alert(Hello World)

See the difference there?

If you want to send data from client-side to server-side, then you either use HTML form, URL or AJAX.

Also, see php.net/isset.

This post has been edited by Xupicor: 28 November 2012 - 04:26 AM

Was This Post Helpful? 2
  • +
  • -

#13 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 424
  • View blog
  • Posts: 1,807
  • Joined: 30-April 10

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 28 November 2012 - 10:53 AM

When working with PHP variables in Javascript I use php's json_encode and then work with them via a Javascript variable
http://php.net/manua...json-encode.php

This post has been edited by laytonsdad: 28 November 2012 - 10:55 AM

Was This Post Helpful? 2
  • +
  • -

#14 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 28 November 2012 - 11:52 AM

Aye, that's a good idea if your variable is not just single-valued. Like arrays. ;)
Was This Post Helpful? 2
  • +
  • -

#15 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Problem in assigning a javascript variable value to a PHP variable

Posted 29 November 2012 - 01:43 AM

Hey guys, I solved the problem. What I did was I used a different file called file.php and used the window.location.href method to assign the value in the url.

Here is my code.

<html>
<head>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function test(id)
{
    value=id;
    alert("I am inside javascript: "+value);
    window.location.href = "file.php?value=" +value; 
}
</script>		
</head>
<body>

<input type="button" onclick="test(555);" value="Button">
	
</body>
</html>


Now, the below code is my file.php

<?php

$variable=$_GET['value'];

echo '<script type="text/javascript">alert("I am inside php :' . $variable .'");</script>';
?>



Thank you for your help. Cheers :)

regards,
Raghav

This post has been edited by raghav.naganathan: 29 November 2012 - 01:45 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2