Forum Moderators: coopster

Message Too Old, No Replies

PHP Variable in JavaScript Variable

How to put a PHP Variable in JavaScript Variable

         

starefossen

9:02 pm on Oct 10, 2007 (gmt 0)

10+ Year Member



I have a mall problem with a coding I'm doing. I'm trying to intergrate a php variable in a javascript variable and this is the code:

var text = " with...<br><input type='text' name='user_relationwith' value='".$userdata['user_relationwith']."' maxlength='100' class='textbox' style='width:200px;'>"

If someone have a sulution to this problem I would appreciate it.

Kind Regards
Starefossen

Habtom

4:32 am on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



var text = " with...<br><input type='text' name='user_relationwith' value='".$userdata['user_relationwith']."' maxlength='100' class='textbox' style='width:200px;'>"

You are not printing the value, that is the problem you are facing I think

var text = " with...<br><input type='text' name='user_relationwith' value=".<?php echo $userdata['user_relationwith'];?>." maxlength='100' class='textbox' style='width:200px;'>"

starefossen

8:43 am on Oct 11, 2007 (gmt 0)

10+ Year Member



Hi Habtom,

It didn't work, I guess the problem is the double qutes inside the javarscript var isn't good. The error is gone but so are the whole rest of the javascript. But using single quotes like this didn't work either:

var text = " with...<br><input type='text' name='user_relationwith' value='.<?php echo $relationwith;?>.' maxlength='100' class='textbox' style='width:200px;'>"

Anyway, thanks for giving it a try...

PHP_Chimp

8:56 am on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you not use php to echo the complete variable?

<?php
$var1 = "var text = ' with...<br><input type='text' name='user_relationwith' value='$relationwith' maxlength='100' class='textbox' style='width:200px;'>";
echo $var1;
?>

You were also missing an ; from the end of your variable. Dont know if this was just for posting or copied from live.

starefossen

9:04 am on Oct 11, 2007 (gmt 0)

10+ Year Member



I just missed the ; when I copied it.

The whole javascript block is like this:

switch (topic) {
case "In a Relationship":
var heading = "In a Relationship"
var text = " with...<br><input type='text' name='user_relationwith' value=".<?php echo $relationwith;?>." maxlength='100' class='textbox' style='width:200px;'>";
break

It switch some thext inside a <span></span> so I can't just use PHP to show it.

PHP_Chimp

11:42 am on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you switching the <span> contents with ajax? Or do you have an array of contents loaded in the background then just use javascript to find and output the one you want?

As if you are just pre loading all of the contents in the background and switching with javascript then that wont work with php (except to populate your array of contents). As there is no additional request to the server between javascript switches. If you are using ajax then there is the request to the server, so you can use php to help you.

So when you say that the variable is not getting output, do you mean that the string in the variable is not getting output; or are you getting the full <?php blabla;?> output?

starefossen

12:26 pm on Oct 11, 2007 (gmt 0)

10+ Year Member



It's kind of a AJAX script:

This is my jscript.js

function getData(dataSource, spanID)
{
var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
}

if(XMLHttpRequestObject) {
var obj = document.getElementById(spanID);
XMLHttpRequestObject.open("GET", dataSource);

XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}

function display_help (text) {
document.getElementById("relationship_box").innerHTML = text
}

function show_relationship(topic) {
var today = new Date()
var currYear = today.getYear()

switch (topic) {
case "In a Relationship":
var heading = "In a Relationship"
var text = " with...<br><input type='text' name='user_relationwith' value="<?php echo $relationwith;?>" maxlength='100' class='textbox' style='width:200px;'>";
break

default:
var heading = "Default"
var text = ""
}

display_help(text)
}

It shows the whole php code with variables and <?php?>

PHP_Chimp

3:42 pm on Oct 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Im guessing that you dont have php set up to parse .js files. So nothing in that file will be touched by php.

One option would be to use .htaccess and add the following lines -


AddType application/x-httpd-php .js

At the top of all .js files you would then need to add -


<?php
header("Content-type: application/x-javascript");
?>

Otherwise the files will be sent with php's default doc type, unless you have changed that. So your javascript will be served as text/html or whatever type it is set to and not work.

<note>
I use this technique for all my .js and .css so I can compress the files, as quite often these are at least 3 times the size of the html page. The downside is the server takes the hit on performance...hey nothings perfect :) Just I can guarantee that my server has more power than 99.99% of users computers, so I would rather have it do the work.
</note>

The other option is to have the ajax get the variables from a php page i.e. use php $_GET to buid your response text based on what is sent to it by ajax. That I suspect that would be the better answer.

So you can send the ajax to find all the people for your relationship field then use php to search through your database or whatever and return a string to your XMLHttpRequestObject.responseText that you can work through with javascript.