2

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.

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

3 Answers 3

1

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;
}
header('Content-type: application/json; charset=UTF-8');
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.
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 ?>

2 Comments

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.
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
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.