0

Below image show my code.

$usernam = sudo grep -w "navneet" "/var/www/mcloud/modules/demo_cmp/.cmpinfo.txt" | cut -d ":" -f7;

function check()

{
    var username = "<?php echo $usernam; ?>";
    alert(usernam);
    return false;
}

I am retrieving value of $usernam variable by linux command and I want to use this variable value in javascript. But unfortunately javascript stop working after adding value of php variable. Please help me to short out my problem.

enter image description here

6 Answers6

1

I got the solution.

I have add trim in my linux command which used to retrieve my record.

$usernam = trim(sudo grep -w "navneet" "/var/www/mcloud/modules/demo_cmp/.cmpinfo.txt" | cut -d ":" -f7);

0
<?php
    $sim = 'hello world';

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

</script>

Other than that, if you really want to "interact" between PHP and JavaScript you should use Ajax.

Nikos Paraskevopoulos
  • 36,975
  • 10
  • 83
  • 85
Ashish Kumar Saxena
  • 3,570
  • 8
  • 24
  • 39
0
<script>
function check(){
var username = "<?php echo $usernam; ?>";
alert(username);
}
</script>

What you are doing wrong is placing quotes with JS variable username which should not be there.

Deepak jha
  • 278
  • 5
  • 18
0

Open Google Chrome or other browser with developer/debugging capabilities. Navigate to the page with your code. View source code or open Developer tools and check the lines referent to var username in Element or Source panel. Also check Console panel.

Also, you need validate/clean/transform PHP variable $usernam in PHP to ensure it a valid string that suit your needs (an error or warning message is not an username).

Samir
  • 314
  • 2
  • 8
0

I give you a sample code. Think that you have to execute the php with a system or exec function. If you only write the code inside a variable, the variable will only have the text nor the result.

<?php
  $usernam = system('sudo grep -w "navneet" "/var/www/mcloud/modules/demo_cmp/.cmpinfo.txt" | cut -d ":" -f7');
?>

<script>
  function check(){
    var username = "<?php echo $usernam; ?>";
    alert(username);
  }
</script>

Check if that is useful for you. I wish it

Kyto
  • 173
  • 3
  • 12
-1

Remove single quote from alert i.e.

  alert(username);

okay sorry for this answer. I only refer your screen shot.

Try this one

{
var username = "<?php echo $usernam; ?>";

alert(usernam); replace with alert(username);

 return false;
}
Kavita
  • 77
  • 7