Forum Moderators: coopster
$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.
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>
<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]
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?
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
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.
<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
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.