admin管理员组

文章数量:1323714

I know you can't directly use PHP variables inside javascript code, but is there a way around it? I need to use these parameter's in javascript:

username: '<?php echo $user_id;?>.example.co.uk',
password: 'example',

Instead of showing the $user_id variable, it outputs: "Resource id #4.example.co.uk"

I'm quite new to javascript so I'm finding it pretty tricky.

Thanks for any help

Edit: I am taking the $user_id variable from the URL like so:

$user_id = mysql_real_escape_string($_GET["user_id"]);

so shouldn't it just have the set value from the URL?

I know you can't directly use PHP variables inside javascript code, but is there a way around it? I need to use these parameter's in javascript:

username: '<?php echo $user_id;?>.example.co.uk',
password: 'example',

Instead of showing the $user_id variable, it outputs: "Resource id #4.example.co.uk"

I'm quite new to javascript so I'm finding it pretty tricky.

Thanks for any help

Edit: I am taking the $user_id variable from the URL like so:

$user_id = mysql_real_escape_string($_GET["user_id"]);

so shouldn't it just have the set value from the URL?

Share Improve this question edited May 9, 2011 at 10:37 Daniel H asked May 9, 2011 at 10:26 Daniel HDaniel H 2,9258 gold badges35 silver badges32 bronze badges 4
  • Where $user_id; es from? looks like result resource – Shakti Singh Commented May 9, 2011 at 10:29
  • What you have looks okay - from the JS end. From the PHP end, you're trying to echo something; you need to be echoing a string (else you'll get this). – Piskvor left the building Commented May 9, 2011 at 10:31
  • possible duplicate of What's the best way to pass a PHP variable to Javascript? – Álvaro González Commented May 9, 2011 at 10:32
  • You can use PHP variables "inside" Javascript just fine, when your Javascript is generated by PHP. – Lightness Races in Orbit Commented May 9, 2011 at 10:32
Add a ment  | 

5 Answers 5

Reset to default 2

I'm a little rusty on PHP, but this may work:

<? //start HTML/Javascript code... 
    echo'var variable = ' . $user_id . '.example.co.uk';
    //finish HTML/Javascript here..
?>

You can assign that php value in any html object then you use the object value in js page

for example

PHP:

$phpvalue = $_GET["sid"];

HTML:

<input type="text" value="<?php echo $phpvalue ;?>" id="txtsamp" />

JS:

var phpval = document.getElementById('txtsamp').value;

Resource id #4 means u have just not plete the mysql query

mysql_query returns Resource id

u have to fetch the data using while ($row=mysql_fetch_array()) and then use

written here

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

UPDATE

don't use mysql_real_escape string for GET data..instead use (int)$_GET['id']

see here

If no connection is found or established, an E_WARNING level error is generated

If you have selected only one column (and expect 1 row), you can use:

$user_id = mysql_result($user_id, 0);

which would fetch the 1st column of the 1st row of the result set $user_id.

But for clarity's sake you should rename the variable $user_id, because it's not an ID but a resource (so call it $userQuery or something =)).

You can't use the result of a mysql_query() directly. Instead, you have to use mysql_result() or something similar (depending on your SQL query).

本文标签: How to use PHP variable in JavascriptStack Overflow