使用html表单中的参数调用javascript函数

时间:2012-09-11 10:27:38

标签: php javascript html javascript-events

您好我正在尝试将参数传递给javascript函数getConfirmation(),我的代码如下。根本不执行对函数的调用。所以我想我说错了。请帮忙。

<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>

5 个答案:

答案 0 :(得分:2)

应该是:

function getConfirmation(value1){

并形成:

echo "<div class=\"addform\"><form method='post' action=\"txn.php\" onsubmit=\"return getConfirmation('".$row1['pop_code']."');\">";

并且您可以单独保留提交图片,而无需任何JavaScript调用。

最终代码:

  echo "<div class=\"addform\"><form method='post' action=\"txn.php\" onsubmit=\"return getConfirmation('".$row1['pop_code']."',this,'".$row1['balance']."');\">\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=\"image\" src=\"images/update.png\" alt=\"Update Row\" class=\"update\" title=\"Update Row\">\n</td>";
  echo "</tr>";
  echo "</form></div>";

和javascript:

<script type="text/javascript">
<!--
function getConfirmation(value1,form,balance)
{

   var retVal = confirm("Pop Code:" + value1 + "\n" + "Amount:" + form.amount.value + "\n" + "Updated Balance:" + balance + "\nPlease confirm!");
   if( retVal == true )
   {
      alert("User wants to continue!");
      return true;
   }else{
      alert("User does not want to continue!");
      return false;
   }
}
//-->
</script>

答案 1 :(得分:0)

尝试更改

function getConfirmation(var value1)

function getConfirmation(value1)

答案 2 :(得分:0)

您不能将var关键字用作函数参数。

试试这个

function getConfirmation(value1){

  //Your function

}

答案 3 :(得分:0)

首先从您的按钮代码中调用该函数,就像这样

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

当我们定义函数参数时,不需要声明   变量为使用var关键字,所以在那之后替换你的javascript   功能声明

<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>

答案 4 :(得分:0)

我认为代码存在2个问题。

  1. 从功能中删除var function getConfirmation(value1)

  2. 代码onclick=\"getConfirmation(".$row1['pop_code'].");\"将为$ row1 ['pop_code')的整数值创建正确的代码,即getConfirmation(10),但是当代码是文本时,它将是不正确的getConfirmation(test)。所以你必须添加引用值 即onclick=\"getConfirmation('".$row1['pop_code']."');\"

相关问题