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']; ?>
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
  • 1
    isn't it better to get value for h via AJAX? – Robert May 22 '13 at 08: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. – Quentin May 22 '13 at 08:18
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin May 22 '13 at 08: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. – Quentin May 22 '13 at 08:19
  • I don't understand the downvote, what was wrong with this question, seems like a perfectly valid question. – Software Guy May 22 '13 at 08:24
  • `$sql` will be a mysql resource – asprin May 22 '13 at 08:24
  • @SoftwareGuy probably because there is a lot of other questions about `convert php to javascript`, such as http://stackoverflow.com/questions/12744040/convert-php-to-javascript – Maël Nison May 22 '13 at 09:02
  • Think the problem was using hard returns on the string so it wa registering it as incomplete. I put the entire string on a single line, it works now. Also i probably could have used the + (concatenation operator) to combine the sections. – user2408545 May 22 '13 at 09:58

2 Answers2

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.

Maël Nison
  • 7,055
  • 7
  • 46
  • 77
chandresh_cool
  • 11,753
  • 3
  • 30
  • 45
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>
Software Guy
  • 3,190
  • 4
  • 21
  • 21
  • 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'. – Software Guy May 22 '13 at 08:23
  • 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. – Software Guy May 22 '13 at 09:40
  • 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 – user2408545 May 22 '13 at 18:13
  • I just updated the code so that it assigns only the type to h – Software Guy May 22 '13 at 20:11