Forum Moderators: open

Message Too Old, No Replies

Trouble getting javascript to update field values

         

MemberID

11:04 pm on Jul 5, 2006 (gmt 0)

10+ Year Member



So I'm working on forms with javascript and php but javascript is giving me trouble.

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" >
&nbsp; &nbsp; CP State: <input type="text" name="state1" value="" size="2" >
&nbsp; &nbsp; 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

RonPK

8:22 pm on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shoot me if I'm asking a dumb question. You do realize that JavaScript works client-side (in the browser) and that it takes special techniques to have it communicate with a PHP script that runs on the server?

rocknbil

8:50 pm on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well my two cents are: if you're doing this page in PHP, why do you need JS to populate the values? Simply move the php tag to the top and generate the form with values intact, something like

<?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="".

MemberID

10:01 pm on Jul 6, 2006 (gmt 0)

10+ Year Member



@RonPK
I actually did another page very similar to this one using javascript & php that's working great. I'm having trouble figuring out the difference between the two pages.

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

MemberID

10:26 pm on Jul 6, 2006 (gmt 0)

10+ Year Member



I guess the other issue would be - how can I do the same thing for drop-down menus..

rocknbil

11:30 pm on Jul 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just answered yourself on this one
.. 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.

MemberID

5:49 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Works like a champ! Thanks!

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.