0

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_query($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

4 Answers 4

1

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++;
}
Sign up to request clarification or add additional context in comments.

2 Comments

But im still having the javascript problem. Once i tick the checkbox the button doesn't get enabled..
it should work fine if u have given attention to my points 1-3... here is a static eg: <form name="f1"> <input type='checkbox' onclick='document.f1.submit.disabled = !this.checked;' /> <input type="button" name="submit" value="kool" disabled="disabled" /> </form>
1

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.

Comments

1

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.

1 Comment

short tags will be deprecatd in php6
0

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.

Comments

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.