homepage Welcome to WebmasterWorld Guest from 107.22.37.143
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
index.php?user=132 HOW?
cameraguy




msg:1279129
 2:20 am on Dec 8, 2004 (gmt 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!

 

Salsa




msg:1279130
 4:43 am on Dec 8, 2004 (gmt 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.

daku




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

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

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

cameraguy




msg:1279132
 11:03 am on Dec 8, 2004 (gmt 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>

adb64




msg:1279133
 1:10 pm on Dec 8, 2004 (gmt 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

cameraguy




msg:1279134
 1:15 pm on Dec 8, 2004 (gmt 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]

RonPK




msg:1279135
 1:19 pm on Dec 8, 2004 (gmt 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'], ')';

cameraguy




msg:1279136
 5:55 pm on Dec 8, 2004 (gmt 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?

adb64




msg:1279137
 8:30 pm on Dec 8, 2004 (gmt 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

cameraguy




msg:1279138
 9:35 pm on Dec 8, 2004 (gmt 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');
?>

RonPK




msg:1279139
 11:05 pm on Dec 8, 2004 (gmt 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.

GeorgeGG




msg:1279140
 11:45 pm on Dec 8, 2004 (gmt 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

cameraguy




msg:1279141
 12:27 am on Dec 9, 2004 (gmt 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.

RonPK




msg:1279142
 8:56 am on Dec 9, 2004 (gmt 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.

cameraguy




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

Thank you. I will try this.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved