Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How to access the php variable from my external javascript file.

this is my getting.js file

$(function(){

$(".search").keyup(function() 

{ 

var searchid = $(this).val();

var dataString = 'search='+ searchid;

if(searchid!='')

{

    $.ajax({
    type: "POST",
    url: "search.php",
    data: dataString,
    cache: false,
    success: function(html)
    {
    $("#result").html(html).show();
    }
    });
}return false;    
});

and this my search.php file.

<?php

include('db.php');

if($_POST)
{

$q=$_POST['search'];

$sql_res=mysql_query("select id,name,email from detail where name like '%$q%' or email like '%$q%' order by id LIMIT 5");

while($row=mysql_fetch_array($sql_res))

{
$username=$row['name'];

$email=$row['email'];

$b_username='<strong>'.$q.'</strong>';

$b_email='<strong>'.$q.'</strong>';

$final_username = str_ireplace($q, $b_username, $username);

$final_email = str_ireplace($q, $b_email, $email);

?>
<div class="show" align="left">

<span class="name"><?php echo $username; ?></span>&nbsp;<br/><?php echo $email; ?><br/>

</div>

<?php

}

}

?>

I want to use $username in my getting.js file. how do I access this. Please let me know.

share|improve this question
    
If this script is not rendered by php - you can't –  vp_arth Dec 19 '14 at 19:18
    
Hi Bono, there are thousands of records. so i want to show these using for loop . –  Travelers Dec 19 '14 at 19:19
    
how do i render it? –  Travelers Dec 19 '14 at 19:20
    
I think you miss difference between server and client code, and how they interact via http. –  vp_arth Dec 19 '14 at 19:20
    
My preferred way is change your search.php to return json instead of html, and handle all received data in javascript. –  vp_arth Dec 19 '14 at 19:23

3 Answers 3

up vote 0 down vote accepted

I love php, which does not knew about html at all.

Try to change your search.php to return json instead of html:

...
$result = array('users' => array(), 'method'=>'search');
while($row=mysql_fetch_array($sql_res))
{
  $result['users'][] = $row;
}
echo json_encode($result);

and handle all received data in javascript:

success: function(json) {
 var data = JSON.parse(json);
 console.log(data);
 // change DOM with new data etc.
}
share|improve this answer

The easiest way I have found to export variables from PHP to JavaScript is to use a snippet:

<script type="text/javascript">var username = '<?=$username?>';</script>

This will set a new JavaScript variable named 'username' to the value of the PHP variable '$username' at the time of page generation. Place this snippet in the page header above where you include 'getting.js' and use the variable 'username' in 'getting.js'.

Note that

<?=$var?>

is a shortcut for

<?php echo $var ?>
share|improve this answer
    
Matt but i am using external js file i don't want to get it into index.php and there are thousands of records so how do i handle these. –  Travelers Dec 19 '14 at 19:18
    
In that case, either: - Return a JSON object with a username and email property and construct the html in your success handler, or - After inserting the html, use JQuery to get the contents of span.name –  Matt W Dec 19 '14 at 19:20
    
can you please share some demo or code url . so that i could get some idea ... –  Travelers Dec 19 '14 at 19:21

There are couple of thing you could do for fetching PHP variables,

Create a hidden element for storing the php variable so it could be fetched from javascript

<input type="hidden" id="user-name" value="<?php echo $username; ?>"> 

And you could fetch it in Javascript by simply using

var userName = $("#user-name").val();

Or , you could define a javascript variable in your search.php file as ,

<script type="text/javascript">
   var username = '<?php echo $username; ?>';
</script>

And As you have mentioned above, you can simply use ,

<span class="name"><?php echo $username; ?></span>

And in JavaScript ,

$(".name").val();

for fetching userName.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.