1

I want to retrieve a row value from database. Since we need serverside to access database we have to use PHP to retrive the value but I am not sure how to use this php in a .js file.

This is what I am trying but it does not work. The reason is simple.. we can not write PHP inside a javascript file.

file.js:

<?php 
$con = mysqli_connect($server, $db_user, $db_pwd, $db_name);
$username = $_SESSION['username'];
$sql = "SELECT * FROM Holders WHERE username='$username'";
$data = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($data);
$address = $row['address'];
?>

$( document ).ready(function() {
    var addressFrmPHP = "<?= $address; ?>";
    var url = "https://api.examlesite.io/api?module=account&action=bal&add=000000000&address="+addressFrmPHP+"&tag=latest&apikey=APIKEY";
});

How can I assign the php $address variable to javascript addressFrmPHP variable?

2
  • you cannot use php inside js file Commented Jan 4, 2018 at 7:10
  • So what's the solution? I mentioned in my question what you've mentioned @vel is there any way to get a database table row value and use it in jS? Commented Jan 4, 2018 at 7:13

3 Answers 3

3

You have to create two files as shown below

getdata.php

<?php 
$con = mysqli_connect($server, $db_user, $db_pwd, $db_name);
$username = $_SESSION['username'];
$sql = "SELECT * FROM Holders WHERE username='$username'";
$data = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($data);
$address = $row['address'];
echo $address;
?>

file.js

$( document ).ready(function() {

    /* AJAX for get data from php page */

    $.post('getdata.php', function(address) {

        var addressFrmPHP = address;
    });

    var url = "https://api.examlesite.io/api?module=account&action=bal&add=000000000&address="+addressFrmPHP+"&tag=latest&apikey=APIKEY";
});
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - Ajax ($.post) is probably what OP was looking for. OP: I would recommend adding some errorhandling (what happens if $_SESSION['username'] is not set? could that potentially happen?)
1

you have to do like this

test.php

<?php 
$con = mysqli_connect($server, $db_user, $db_pwd, $db_name);
$username = $_SESSION['username'];
$sql = "SELECT * FROM Holders WHERE username='$username'";
$data = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($data);
$address = $row['address'];

echo '<script> var address= '.$address.';</scrpit>';
?>
<script type='text/javascript' src='js/file.js'></script>

file.js

$( document ).ready(function() {
    var addressFrmPHP = address;
    var url = "https://api.examlesite.io/api?module=account&action=bal&add=000000000&address="+addressFrmPHP+"&tag=latest&apikey=APIKEY";
});

1 Comment

so how to include test.php in file.js or file.js in test.php ? then only we can use this right?
1

Create One file Getdata.php and write the below code:

<?php 
    $con = mysqli_connect($server, $db_user, $db_pwd, $db_name);
    $username = 'nirav4491';
    $sql = "SELECT * FROM Holders WHERE username='$username'";
    $data = mysqli_query($con, $sql);
    $row = mysqli_fetch_assoc($data);
    $address = $row['address'];

?>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="application/javascript">
        $(document).ready(function() {
            var addressFrmPHP = '<?php echo $address; ?>';
            var url = "https://api.examlesite.io/api?module=account&action=bal&add=000000000&address="+addressFrmPHP+"&tag=latest&apikey=APIKEY";
            alert(url);
        });

    </script>
</head>
</html>

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.