Forum Moderators: open
What's supposed to happen is a First and Last name is entered into the form, the user clicks the search button, php searches a mysql table for the info (which it successfully does) and then Javascript updates the field values to reflect the new data (which it does not do).
I feel like the problem is simply in the pkacement of the javascript.
Here is the code abridged:
<form method="post" name="DataEntry" Action="ThisPage.php">
<table>
<tr bgcolor="yellow">
<td colspan=0>CP First Name: <input type="text" name="FName1" Value=""> <br> CP Last Name: <input type="text" name="LName1" Value=""></td>
<td colspan=0 align=right><input type="submit" value="Search CP by Name" name="SUBMIT"></td>
</tr>
<tr>
<td colspan="2">CP City: <input type="text" name="city1" value="" size="15" >
CP State: <input type="text" name="state1" value="" size="2" >
CP Zip: <input type="text" name="zip1" value="" size="10" ></td>
</tr>
<?php
//this bit of code essentially searches a database table for relevant information. This is how Javascript gets its values. The PHP is working fine, so this chunk can be ignored.
$FindCustQuery="SELECT * FROM Customer WHERE ($comparison)";
if(mysql_query($FindCustQuery)){
$ReturnedData=mysql_query($FindCustQuery);
echo "
<SCRIPT LANGUAGE=\"JavaScript\"><!--
document.DataEntry.CPinfobutton.value=\"Customer Data Found. Edit if necessary.\";
//-->
</SCRIPT>
";
$row=mysql_fetch_row($ReurnedCustQuery);
echo "
<SCRIPT LANGUAGE=\"JavaScript\"><!--
document.DataEntry.city1.value=\"".$row[7]."\";
document.DataEntry.state1.value=\"".$row[8]."\";
document.DataEntry.zip1.value=\"".$row[9]."\";
//-->
</SCRIPT>
";
}
</table>
</form>
The PHP is working just fine - proper values show up in the "view source" window (again abridged) Also, the info button works:
...
<SCRIPT LANGUAGE="JavaScript"><!--
document.DataEntry.CPinfobutton.value="Customer Data Found. Edit if necessary.";
//-->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript"><!--
document.DataEntry.CID1.value="1044";
document.DataEntry.city1.value="Troy";
document.DataEntry.state1.value="NY";
document.DataEntry.zip1.value="12180";
//-->
</SCRIPT>
</table>
</form>
Thanks for any help you can offer.
-Alex
<?php
if(mysql_query($FindCustQuery)){ (fetch values with php) }
form
In the form,
<input type="text" name="city1" id="city1" value="$row[7]" size="15">
The first time the form is called, the "if" fails, so there are no row values, so $row[7] (and others) are null, it would still produce value="".
@rocknbill
I hadn't thought of that! What if I needed i in $row[i] to change based on which table I grabbed from? Say in DatabaseA the needed city is field 4 and in DatabaseB it's field 17?
Guess I could use an inefficient if statement.
(This may be more talking aloud then anything else)
Anyways, thanks!
.. i in $row[i] to change based on which table I grabbed from?...
$index = (table eq 'database_a')?7:12;
<input type="text" value="$row[$index]">
(eq is the perl logical equality command, perl and php are very close anyway. :-) )
how can I do the same thing for drop-down menus..
DD's should be generated dynamically anyway. I return to perl again, where an array is @, and concatenating to a variable value is done with .=
@list= &some_function_that_gets_list_items;
$list = '<select name="list" id="list"><option value="">select</option>';
foreach $v (@list) { $list .= '<option value="' . $v; }
if ($v eq $row[$index]) { $list .= ' selected'; }
$list .= '">' . $v . '</option>';
}
$list .= '</select>';
So now the entire dd is stored in $list with any previously selected values . . . selected.
For anyone who comes around looking for the same sort of thing (albeit in the wrong forum)...
Instead of saying "if (this database){use thisIndex}" I have a function for either database where I use a sensical-named variable (e.g. $state=$row[4]).
Within the form I use the 'sensical-name' variable like this:
<input type="text" name="state" value="<?php echo $state;?>" size="2">
For my problem, I just made the 'sensical-name' variables global, but another solution might be to return an array of the new variables and then extract the array.