0

Is it possible to assign a php return to a js variable? ex:

<script type='text/javascript'>
var h = <?php include'dbconnect.php';
    $charl = (some number from another sql query)
    $sql=mysql_query("selct type from locations where id='$charl'");
    echo $sql; ?> ;
if(h == "hostile") {
    (run some other js function)
}
</script>

what I need to do is get a single text value (type) from the charl (character location) and assign it to a java script variable and run an if statement on it. any hints?

Here is an update on my code. it doesnt return any errors but its not outputting the way i want it to. it should return the [type] only which should be equal to hostile, city, farm, and stuff like that.it wont run unless the entire string is in the same line. I believe its returning the entire string and not just the echo (like i need it to)

function check_hostile() { var h = '<?php session_start(); include"dbconnect.php"; $charid=$_SESSION[\'char_id\']; $charloc=mysql_fetch_array(mysql_query("select location from characters where id=\'$charid\'")); $charl=$charloc[\'location\']; $newloc=mysql_fetch_array(mysql_query("select type from locations where id=\'$charl\'")); echo $newl[\'type\']; ?>'; 
if(h == "hostile") { 
 if(Math.random()*11 > 8) { 
  find_creature(); 
 } 
}
$("#console").scrollTop($("#console")[0].scrollHeight);
}

Here is the output of an alert function when theis is run.

<?php session_start(); include"dbconnect.php"; $charid=$_SESSION['char_id']; $charloc=mysql_fetch_array(mysql_query("select location from characters where id='$charid'")); $charl=$charloc['location']; $newloc=mysql_fetch_array(mysql_query("select type from locations where id='$charl'")); print $newloc['type']; ?>
8
  • 1
    isn't it better to get value for h via AJAX? Commented May 22, 2013 at 8:17
  • 1
    @RobertPodwika — No. That would involve another HTTP request and being asynchronous. It would, however, probably make more sense to just use PHP to determine if the script should be added to the document or not instead of generating a variable and then testing it client side. Commented May 22, 2013 at 8:18
  • You are using an obsolete database API and should use a modern replacement. Commented May 22, 2013 at 8:18
  • That isn't how you use mysql_query. You need to use something like fetchrow to extract the data from the result object you get back. Commented May 22, 2013 at 8:19
  • I don't understand the downvote, what was wrong with this question, seems like a perfectly valid question. Commented May 22, 2013 at 8:24

2 Answers 2

2

Change it to this

var h = <?php include "dbconnect.php";
$charl = (some number from another sql query)
$sql=mysql_query("selct type from locations where id=$charl");
$row = mysql_fetch_row($sql);
echo json_encode($row["type"]); ?>;

json_encode() will convert a PHP value into a valid Javascript representation that you can inject into your script.

Sign up to request clarification or add additional context in comments.

2 Comments

var h = <?= json_encode($row['type']) ?>;, actually. Otherwise, it will break on quotes (for example with the sentence i'm a lama).
@MaëlNison apt and changed
1

Yes, it is possible and is quite a common practice.

But your code has a small issue, it returns a string, so you must enclose it in quotes in javascript.

I have updated your code to fix that small issue and improve the code readability:

<?php 

include'dbconnect.php';
$charl = (some number from another sql query)
$sql=mysql_query("select type from locations where id='$charl'");

if (mysql_num_rows($sql) > 0) {
    $row = mysql_fetch_array($sql);
    $h = $row['type'];
} else {
    $h = null;
}

?>

<script type='text/javascript'>

var h = '<?php echo $h; ?>';
if(h == "hostile") {
    (run some other js function)
}

</script>

4 Comments

Note: I also suspect a PHP issue in your code which is not related to your question: the php variable $sql should be processed using something like 'mysql_fetch_array($sql)' and only then will you be able to access the 'type'.
a bit more detail would help, what are you stuck at? as i said, my answer only has the relevant info for your original question (possibility of mixing php and javascript). there are clearly php/sql issues in your code as well. I can address those too if you want.
I just updated some info as to my actual code now, i dont get errors, but its registering the entire string as the variable h, I just need h to be what is echoed/printed in the php code
I just updated the code so that it assigns only the type to h

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.