开发者

problem with enable/disable button using javascript

Im am trying to add a check box that will enable/disable the edit button. Im retrieving a price list and displaying it inside a table. When i add the javascript into the php code it doesn't work. Below is my code

<table border="1">
  <tr>

    <td width="100">Fee % </td>
    <td width="100">Price</td>
    <td width="100">Total</td>
    <td width="102">&nbsp;</td>

  </tr>
  <tr>
    <?php
  $sql1="select * from pricelist";
  $result1=mysql_qu开发者_如何学Goery($sql1) or die(mysql_error());

  while ($row=mysql_fetch_array($result1)) {

$id=$row['id'];
$price=$row['h_price'];
$a=0;

print "<form id='form1' name='$a+' method='post' action=''>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>"; 
print "</tr>";
print "</form>";

    }
   ?>
</table>

Can someone please tell me what am i doing wrong?

Thanks


take out the following code out of loop... it creates multiple form tag...

print "<form id='form1' name='$a+' method='post' action=''>";

Place "<tr></tr>" inside the loop...

final code looks like:

<form id='form1' name='fm1' method='post' action=''>  <tr>
    <?php
  $sql1="select * from pricelist";
  $result1=mysql_query($sql1) or die(mysql_error());

  while ($row=mysql_fetch_array($result1)) {

$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<tr>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>"; 
print "</tr>";

    }
print "</form>";
   ?>

Next if u want to control the button only then

<input type='checkbox' onclick='document.$a.submit.disabled = !this.checked;' />

and make sure of the following things: 1.) form name should be $a i.e <form name='$a' ...>

2.) submit buttons name should be submit i.e <input type='submit' name='submit'...>

3.) increase the $a variable only at the end of loop i.e

$a++;
}


i think $a+ will cause syntax error. you need to increment $a at the end of loop like $a++. Also see page source and see what is coming from server.


I would recommend using following format for html + php output

<form id='form1' name='<?=$a++?>' method='post' action=''>
<tr>
<td><input name='fees' value ='<?=$fees?>' type='text' size='4' /></td>
<td><input name='price' value ='<?=$price?>' type='text' size='15' /></td>
<td><input type='checkbox' onclick='this.disabled = !this.checked;'><td>
<td><input type='submit' name='<?=$a++?>' value='Submit' disabled='disabled' /></td>
</tr>
</form>
<?

you also need to have < tr > to be in the while loop.


In your html part where you set up the formular you have to use " " to escape the php code. example:

print "<form id='form1' name='".$a+."' method='post' action=''>";

But as Adeel said already, what is $a+ ?

And you have to edit all prints and echoes in the way, that php expressions are excaped from the HTML source.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜