0

I'm sorry I am very new to programming especially java script. Please help me.

I'm using php to get and update the data from db. And I'm trying to get my data from database using JavaScript function onclick but I kept getting an the same error.

First is I have my data already fetched. And stored it inside my variable $data but I applied json_encode as per the video tutorial I watched but unfortunately my code didn't worked. I don't know why.

This is how I initiates my codes:

//This is foreach loop with a variable of $user where all the data has been stored in array(I'm not sure), before the line of this code below.
$data = json_encode($user, true);

//In my link/href where I'm getting an error that my $data is not defined. 
<a href='javascript:getUpdateUser($data);' id='edit'> EDIT </a>

//and my script
<script>
function getUpdateUser(user) {
    alert(user);
}
</script>
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • `` pass this in your function instead of this `$data` . – Swati Mar 09 '19 at 18:45
  • Welcome to StackOverflow Jasper. Your question lacks some of the information required to really assist you with what is going on. Please take a moment to produce a [mcve] that we can use. The code snippet you provided is small, and it does not give us a good idea as to how you came to your problem. – artomason Mar 09 '19 at 18:50
  • @Swati, Hello, thank you for responding me. I tried it but Im still getting an error which is Uncaught SyntaxError: Unexpected end of input – Jasper Santiago Mar 09 '19 at 18:51
  • @Swati, this how it looks the output after getUpdateUser({ , that's it. – Jasper Santiago Mar 09 '19 at 18:53
  • Check [this](https://stackoverflow.com/questions/3983088/javascript-error-uncaught-syntaxerror-unexpected-end-of-input) out ,this will help you . – Swati Mar 09 '19 at 18:59
  • @artomason, thank you for letting me know of these instructions and I'm sorry if my question lacks of some important information. I will just update it. – Jasper Santiago Mar 09 '19 at 19:00

3 Answers3

2

You need to go back into PHP mode to echo the variable.

<a href='javascript:getUpdateUser(<?php echo $data; ?>);' id='edit'> EDIT </a>

Also, the second argument to json_encode() is not a boolean, it's an integer containing flag. You're confusing it with json_decode() which uses the second argument to determine whether to return objects or associative arrays. When you set $data it should be:

$data = json_encode($user);
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • Thank you so much Sir, it finally worked! I will be asking soon, and I'll be more specific unlike this post. I just realized how small the problem I just had. Once again, thank you so much! – Jasper Santiago Mar 09 '19 at 19:08
  • wow - I just watched the "accepted" arrow dance from one answer to another to another and back... awesome – Professor Abronsius Mar 09 '19 at 19:12
0
  • first the file must be ".php" extition
  • second this is the fix:

    <a href='javascript:getUpdateUser("<?php echo $data; ?>");' id='edit'> EDIT</a>
    

you should acsess the variable {$data} inside php not in html or JS

you have 2 way to print the variable

1. <?php echo $data; ?>

or

2. <?= $data; ?>

update

anyway, you must separate developing Languages :

<?php 
//This is foreach loop with a variable of $user where all the data has been stored in array(I'm not sure), before the line of this code below.
$data = json_encode($user, true);
?>

//In my link/href where I'm getting an error that my $data is not defined. 
<a href='javascript:getUpdateUser("<?php echo $data; ?>");' id='edit'> EDIT </a>

//and my script
<script>
function getUpdateUser(user) {
    alert(user);
}
</script>
A. El-zahaby
  • 957
  • 8
  • 27
  • 1
    Thank you so much Sir, it finally worked! I will be asking soon, and I'll be more specific unlike this post. I just realized how small the problem I just had. Once again, thank you so much! – Jasper Santiago Mar 09 '19 at 19:09
0

Write $data inside php tag:

<a href='javascript:getUpdateUser(<?=$data?>);' id='edit'> EDIT </a>
  • 1
    I'm not entirely sure how you came to this conclusion based on what little information was provided by the OP. Please take a moment to expand upon your answer considering the OP's knowledge level of the subject. – artomason Mar 09 '19 at 18:54