'Calling a JavaScript function with argument from html form

I am trying to pass an argument to a JavaScript function getConfirmation() and my code is as follows. The call to function is not executed at all. So I guess I am going wrong with calling it. Please help.

<html>
<head>
<script type="text/javascript">
<!--
function getConfirmation(var value1)
{
   
   var retVal = confirm("Pop Code:" + value1 + "\n" + "Amount:" + "" + "\n" + "Updated Balance:" + "" + "\nPlease confirm!");
   if( retVal == true )
   {
      alert("User wants to continue!");
      return true;
   }else{
      alert("User does not want to continue!");
      return false;
   }
}
//-->
</script>
<body>
<div align="center" style="width:100%; margin: 0px auto;">

<div align="center" style="width:100%; margin: 0px auto; background-color:f0f0f0">
<div id="child" style="  align="center" style="width:100%; margin:0px auto;" >
<div style=" right:5%; top:5%; z-index: 100; position:absolute;">

</div>
<img src = "images/header.png" width ="100%"  />

</div>
</div>
<div align="center">
<?php
require 'config.php';
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT * FROM cust_info;";
$result = mysql_query($query) or die(mysql_error());

echo "</br>";
echo "<table border='1'>
<tr>
<th>Pop Code</th>
<th>Open_Date</th>
<th>Maturity_Date</th>
<th>Amount</th>
<th>Balance</th>
<th>Col Amount</th>
</tr>";

while($row1 = mysql_fetch_array($result))
  {
  if ($row1['status'] == 'DONE')
  {
  continue;
  }
  echo "<div class=\"addform\"><form method='post' action=\"txn.php\">\n";
  echo "<tr>";
  echo "<td><input type='hidden' name='pop_code' value='".$row1['pop_code']."'>" . $row1['pop_code'] . "</td>";
  echo "<td><input type='hidden' name='open_date' value='".$row1['open_date']."'>" . $row1['open_date'] . "</td>";
  echo "<td><input type='hidden' name='mat_date' value='".$row1['mat_date']."'>" . $row1['mat_date'] . "</td>";
  echo "<td><input type='hidden' name='depoamt' value='".$row1['depoamt']."'>" . $row1['depoamt'] . "</td>";
  echo "<td><input type='hidden' name='balance' value='".$row1['balance']."'>" . $row1['balance'] . "</td>";
  echo "<td>" . "   <input type=\"text\" name=\"amount\"/>\n" . "</td>";
  //echo "<td>" . "<input type=\"button\" value=\"Make Entry\" onclick=\"getConfirmation();\"" . "</td>";
  echo "<td>" . "   <input type=\"image\" src=\"images/update.png\" onclick=\"getConfirmation(".$row1['pop_code'].");\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n" . "</td>";
  echo "</tr>";
  echo "</form></div>";
  }
echo "</table>";
?>
<div align="center" style="width:100%; margin: 0px auto;">
<img src="images/footer.png" style="width:100%;"/>
</div>
</div>
</body>
</html>


Solution 1:[1]

try changing

function getConfirmation(var value1)

to

function getConfirmation(value1)

Solution 2:[2]

You cannot use the var keyword as a function parameter.

try this

function getConfirmation(value1){

  //Your function

}

Solution 3:[3]

first call the function from your button code just like this

echo "<td><input type="image" src="images/update.png" onclick="getConfirmation('".$row1['pop_code']."');" alt="Update Row" class="update" title="Update Row"></td>";

when we define the function parameter the there is no need to declare variable as using var keyword so after that replace your javascript function declaration

<script type="text/javascript">
function getConfirmation(value1)
{

   var retVal = confirm("Pop Code:" + value1 + "\n" + "Amount:" + "" + "\n" + "Updated Balance:" + "" + "\nPlease confirm!");
   if( retVal == true )
   {
      alert("User wants to continue!");
      return true;
   }else{
      alert("User does not want to continue!");
      return false;
   }
}

</script>

Solution 4:[4]

I think there is 2 issues with the code.

  1. Remove the var from the function
    function getConfirmation(value1)

  2. The code onclick=\"getConfirmation(".$row1['pop_code'].");\" will create proper code on integer values for $row1['pop_code') ie getConfirmation(10), but when the code is text it will be incorrect getConfirmation(test). So you have to add quote the values.
    ie onclick=\"getConfirmation('".$row1['pop_code']."');\"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 WatsMyName
Solution 2 Mark Walters
Solution 3 G10DRA
Solution 4 Nandakumar V