Welcome to WebmasterWorld Guest from 54.157.222.62

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

index.php?user=132 HOW?

   
2:20 am on Dec 8, 2004 (gmt 0)

10+ Year Member



I need your help again.

Can someone tell me how to retrieve the value of $user in index.php when using the following js script?

<script language="javascript" src="index.php?user=66175"></script>

Many thanks!

4:43 am on Dec 8, 2004 (gmt 0)

10+ Year Member



In your index.php scirpt, put:

$user = $_GET['user'];

$user will then hold the value '66175'.

That will get the job done, but when someone goes to your index.php page with no query string, PHP will produce a warning message. The way to solve that is to add a little more code to the line, like:

!isset($_GET['user'])? $user = "" : $user = $_GET['user'];

What this says is that if there is no user to get from the query string, $user will be set, but it will contain nothing, but if user is in the query string, $user will be set to the value that accompanies it. And by setting $user to nothing when appropriate, that may also eliminate some other error messages farther down in your code.

I hope that helps.

5:44 am on Dec 8, 2004 (gmt 0)

10+ Year Member



i usually use this ( just a bit different from previous answer )

$user = isset($_GET['user'])? $_GET['user'] : "";

11:03 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Thank you guys. I apreciate your quick rsponse.

That's indeed the instruction I was using. But it's still not working. When I execute 'index.php?user=66175' directly from the browser, it works, I get a value for $user.

But when I execute it through test.htm, nothing! I know for a fact that the script is taking me to 'index.php', but apparently not the variable. Here is the script of test.htm. Perhpas you can spot a mistake?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script language="javascript" src="http://.../index.php?user=66175"></script>
</body>
</html>

1:10 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Hello cameraguy,

I also faced the same problem a long time ago, probably you should set the mimetype correct for a JavaScript. Before sending anything from the index.php file, send the following header:

header('Content-type: application/x-javascript');

Regards,

Arjan

1:15 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks Arjan. I changes test.htm to...

<html>
<head>
<meta http-equiv="Content-Type" content="application/x-javascript; charset=iso-8859-1">
</head>
<body>
<script language="javascript" src="http://me.example.fr/NeoCOUNTER/neocounter.php?user_counter=66175"></script>
</body>
</html>

...to no avail unfortunately.

[edited by: jatar_k at 5:24 pm (utc) on Dec. 8, 2004]
[edit reason] generalized url [/edit]

1:19 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



cameraguy, I'm sure that's not what Arjan meant. The HTML was fine as it was.

In the php script, make sure you output the Javascript MIME type and JavaScript statements, like this:

<?php
header("Content-type: application/x-javascript");
echo 'alert(', $_GET['user'], ')';

5:55 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Ahhhh... I understand!

OK, it does seem to do the trick, but now it's my php script which does not work.

This works:
<?php
header("Content-type: application/x-javascript");
echo 'alert(', $_GET['user_counter'], ')';
?>

This does NOT work:
<?php
header("Content-type: application/x-javascript");
echo 'alert(', $_GET['user_counter'], ')';
echo "hello";
?>

This is what I am trying to do:
<?php
header("Content-type: application/x-javascript");
session_start();
$_SESSION['user']=$_GET['user'];
echo "$user";
?>

Does 'header("Content-type: application/x-javascript");' have anything to do with the problem?

8:30 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



cameraguy,

RonPK is right, that was indeed not what I meant.
What you should do, is first write out the javascript you want to send by hand.
Then code this is your PHP file, replacing the variables where needed and check whether the output from the script is what you want by directly entering the URL of the php script in your browser, not from the HTML file.

In your last example it will probably only write the value of '$user', which is not valid javascript.

regards,

Arjan

9:35 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



I am affraid I don't understand.
What I see is php and javascript mixed together...
I hate to ask, but could someone help me correct the my script?

<?php
header("Content-type: application/x-javascript");
//session_start();
$_SESSION['user']=$_GET['user'];
include('main.php');
?>

11:05 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Keep in mind that the PHP script can only send one type of output to the browser: javascript, html, css, xml, whatever, but only one at a time.

It should be possible in the same script to perform all sorts of server side actions: read/write files, do stuff in a database, store info in a session. So if you uncomment session_start(), I think things should work. Otherwise this may be the moment to reveal to the world what it is that you're trying to achieve.

11:45 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Try something like:
<body>
<script TYPE="text/javascript">
// declare JavaScript variable
var user_info = "Unknown";
</script>

<script language="javascript" src="http://.../index.php?user=66175"></script>

<script TYPE="text/javascript">
document.write("User Info = "+user_info+"\n");
</script>

</body>

Not sure about PHP syntax, but send a string back containing
the variable name and the value:
<?php
header("Content-type: application/x-javascript");
echo 'user_info = '$_GET['user_counter'];' ';
?>

GeorgeGG

12:27 am on Dec 9, 2004 (gmt 0)

10+ Year Member



RonPK

I put together a counter as my first php exercise and would like to propose it to other people.

I give them the following html code:
<script language="javascript" src="index.php?user=66175"></script>. They put on their website and a counter shows at that location.

I need to get that user id and make a session variable out of it in order to use it throughout my program.

It seems to be a much used method, but if you have another suggestion...

I apreciate your help.

8:56 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php 
header("Content-type: application/x-javascript");
session_start();
$_SESSION['counter']++;
echo 'document.write("You have requested ', $_SESSION['counter'],
' pages during this session.");';
?>

This may not be exactly what you're after, but maybe it gets you going.

9:06 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Thank you. I will try this.