1

I am trying to populate a form with data from a table based on buttonclick. I am passing the data into a javascript function.

<?php 
$sql=mysqli_query($con, "SELECT FacultyEducationalId, FacultyDegreeName, FacultyCollegeName, FacultyPassingYear FROM facultyeducationaltable WHERE FacultyId=".$_SESSION['userid']);
while ($row=mysqli_fetch_array($sql))
{
?>
<tr class="info">
<td><?php echo $row['FacultyDegreeName'] ?></td>
<td><?php echo $row['FacultyCollegeName'] ?> </td>
<td><?php echo $row['FacultyPassingYear'] ?> </td>
<td>
<button onclick="modifyEdu(<?php echo $row['FacultyEducationalId'].','.$row['FacultyDegreeName'].','.$row['FacultyCollegeName'].','.$row['FacultyPassingYear']; ?>)" id="modifyEdu" class="btn btn-primary">Modify</button>                                                                 
</td>
</tr>
<?php
}
?>

The function is as follows:

<script type="text/javascript">
  function modifyEdu(id, deg, uniname, passyear) {
var facultyeduid=parseInt(id);
var degname=deg;
var uniname=uniname;
var passyear=passyear;
console.log("1");
document.getElementById("degreename").value="2";
document.getElementById("universityname").value=uniname;
document.getElementById("passyear").value=passyear;
}
</script>

I am getting : Uncaught ReferenceError: x is not defined at HTMLButtonElement.onclick. How do I fix this? (x is the value that is in 'FacultyDegreeName')

1
  • 1
    well you do not wrap it in quotes it sounds like. View the source and you will see why. Commented Mar 27, 2018 at 17:25

1 Answer 1

2

You are missing to escape the string using single quote ' and \':

<button onclick="modifyEdu('<?php echo $row['FacultyEducationalId'].'\',\''.$row['FacultyDegreeName'].'\',\''.$row['FacultyCollegeName'].'\',\''.$row['FacultyPassingYear']; ?>')" id="modifyEdu" class="btn btn-primary">Modify</button>

Because you will get modifyEdu(foo,bar,2018) instead of modifyEdu('foo','bar','2018').

In the first case foo will be considered as an undefined variable and cause the "ReferenceError" error.

Sign up to request clarification or add additional context in comments.

1 Comment

This works. Thanks a lot. I had no idea this was the problem.

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.