I hope someone can help me, I've been struggling, I have a from and within the form is table which has columns. It retrieves the data from a Mysql table. In column 2 it displays the current information and in column 3 it displays the form o开发者_JS百科ptions to update the values, eg:
First name: John [Form field to update]
Date of birth: 1970-01-01 [form field to update]
I'm specifically struggling to get the java script functioning for the popup calendar. If I run it from html it works fine but when I do it within php I can't get it to run because it displays the form within a while loop:
<head>
<script src="datetimepicker_css.js"></script>
</head>
<body>
<?php
.....
$sql2="SELECT * FROM em_detail WHERE id = '$data1'";
$result2=mysql_query($sql2);
echo "<form action='upd_emp.php' method=post>";
while($row2 = mysql_fetch_assoc($result2)) {
$id_2 = $row2['id'] ;
.....
echo "<tr>";
echo "<td height=35px><strong>Date of Birth: </strong></td>";
echo "<td>$dob_2</td>";
echo "<td><input size=30 type=Text name=dob id=demo1/></td>";
echo "<td align=left><img src=images/cal.gif onclick=javascript:NewCssCal('demo1') style=cursor:pointer/></td>";
echo " </tr>";
It's not working because in every row you print out in the loop, the corresponding inputs have the same id — demo1
.
IDs in HTML should be unique across all elements. This means that every calendar that is opens looks for an input with ID of demo1
, and it always finds the same one.
You'll want to make sure the input in each row has a unique id. You can use the variable you have, $id_2
, or you can increment a counter for each iteration of the loop and use that. You will then use that unique number in both the id
of the <input>
and the onclick
for the calendar trigger.
<?php
//...
$counter = 0;
while($row2 = mysql_fetch_assoc($result2)) {
$id_2 = $row2['id'] ;
?>
<tr>
<td height="35"><strong>Date of Birth: </strong></td>
<td><?php echo $dob_2; ?></td>
<td><input size="30" type="text" name="dob_<?php echo $id_2; ?>" id="demo_<?php echo $counter; ?>"/></td>
<td align="left"><img src="images/cal.gif" onclick="NewCssCal('demo_<?php echo $counter; ?>');" style="cursor:pointer"/></td>
</tr>
<?php
$counter++;
}
Also I've update the code with the following changes:
name
of an input should also be unique.- You should use quotes around attribute values in HTML
- No need to echo your HTML code if it doesn't have variables - just use
?>
and<?php
when you want to get back into PHP code. javascript:
is not needed inonclick
attributes — they are always Javascript, and thejavascript:
just becomes a label that isn't used.
I believe the problem is the code you're trying to execute is using the ID
in the input
. ID's are only supposed to be used once on the page or JavaScript doesn't know what field to work with.
You have 2 potential options, the first is to use a class to call the date picker, this is what I do personally and it works a charm. You may need to modify your code a bit for this to work however.
The second is to dynamically generate a different ID
for each input
field you're using.
I hope this helps you.
精彩评论