Welcome to WebmasterWorld Guest from 54.146.201.80

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

index.php?user=132 HOW?

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

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Full Member

10+ Year Member

joined:Oct 9, 2003
posts:314
votes: 0


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)

New User

10+ Year Member

joined:Oct 6, 2004
posts:5
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Full Member

10+ Year Member

joined:Feb 4, 2004
posts:305
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1749
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Full Member

10+ Year Member

joined:Feb 4, 2004
posts:305
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1749
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 6, 2002
posts:191
votes: 0


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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


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)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 22, 2002
posts:1749
votes: 0


<?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)

Junior Member

10+ Year Member

joined:Dec 3, 2004
posts:78
votes: 0


Thank you. I will try this.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members