Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OnSelect style drop down 3

Status
Not open for further replies.

MinnisotaFreezing

Programmer
Jun 21, 2001
120
KR
I would like to make a drop down that does and action when the user selects a choice from that drop down or leaves the drop down. Can this be done with PHP? That is, if you select "Orange" from the list, a text box will be populated with "Fruit".

Also, I want focus to go to a specific text box when a page loads. Is this possible with php?

Thanks all

CJ
 
PHP is serverside so no interactive elements to the user can be made. You need JavaScript for this. Ofcourse you can build the JavaScript with data from a database which you read with php.
All events will take place in the JavaScript. Only when you submit the page you can use php to make it more dynamic ea interactive. mcvdmvs
"It never hurts to help" -- Eek the Cat
 
you can do in JS.

for example:
<script language=&quot;javascript&quot;>
function filltext(){
switch(myform.choose.selectedindex){
case 1 : myform.text.value=&quot;Fruit&quot;;break;
case 2 : myform.text.value=&quot;Tool&quot;;break;
}
}
</script>

<form name=myform ...>
<select name=choose onChange=&quot;filltext()&quot;>
<option value=0>Choose...</option>
<option value=1>Orange</option>
<option value=2>hammer</option>
</select>

<input type=text name=text value=&quot;&quot;>

</form>

To do the on load go to text field in the form up, just place this on the body tag:
<body ... onLoad=&quot;myform.text.focus()&quot;>
...
</body> Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Wow guys, that is exactly the info I was looking for. I'm going to try that right now.
 
I'm back for a little more. You threw me for a loop, its
selectedIndex rather than selectedindex.

Which leads me to my next question. Are there any resources that I can download that have all HTML functions and javascript functions? So if I want to look up selectedindex and see exactly what it does? Or if I wan't to look up <input> and see what paramaters it takes and what they do? I can go online and look on google, but I would like to DL something to my comp so I don't have to go online all the time(I'm dial up right now) I thinking kinda like MSDN is for microsoft stuff.(For some reason they don't include much info on Java in the MSDN :))

thanks

CJB
 
Sorry .. i always forget the case sensitive. But i'm glad it helped you out.

To fing, i usualy go to Google and search for javascript manual or javascript tutorial. Once i found one great in the netscape site. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I'm going to wear out my welcome here, but here goes. I want to now combine PHP and Javascript in the same function. Like this:

<?php
echo <script language=&quot;javascript&quot;>;
function filltext(){
alert(customers.selectedIndex);
alert(customers.options[customers.selectedIndex].text);
$query = &quot;select address from customers where name = customers.options[customers.selectedIndex].text&quot;;
$result = mysql_query($query) or
die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);

db.value = $row[address];

}
</script>

I get a pars error in line 2.

So I tried to separte the 2, like this

<script language=&quot;javascript&quot;>
function filltext(){
alert(customers.selectedIndex);
alert(customers.options[customers.selectedIndex].text);
$query = &quot;select address from customers where name = customers.options[customers.selectedIndex].text&quot;;
return $query;
}
</script>

<?php
function filltextphp(){
$query = filltext();
$result = mysql_query($query) or
die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
//db.value = $row[address];
}

That results in a debug error when I try and call filltestphp() in this line:

<select name=&quot;customers&quot; onChange=&quot;filltextphp()&quot;>

Any suggestions? Should this be a new topic?
 
where are the <? and ?> before and after the function call?
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Yes, the <?php is before the function call and the ?> is after the funtion call. Here is my entire page if it helps. I'm really enjoying PHP, but I really want to get this to work. There is a lot of extra stuff in there, but it is what I have.

Also, select_field is a custom function that blows up an arry into a drop down box.

<?php
echo &quot;<script language=\&quot;javascript\&quot;>&quot;;
echo &quot;function filltext(){
alert(customers.selectedIndex);
alert(customers.options[customers.selectedIndex].text);
\$query = \&quot;select address from customers where name =\&quot; + customers.options[customers.selectedIndex].text;
\$result = mysql_query(\$query);
\$row = mysql_fetch_array(\$result, MYSQL_ASSOC);

db.value = \$row[address];

}
</script>&quot;;


include (&quot;header.php&quot;);
mysql_select_db(&quot;customers&quot;)or
die(mysql_error());
$output = anchor_tag(&quot; &quot;Go here!&quot;);
echo &quot;$output&quot;;
echo &quot;<br>&quot;;
$myarray = array(&quot;name&quot; => &quot;MyImage&quot;, &quot;alt&quot; => &quot;new image&quot;, &quot;height&quot; => &quot;32&quot;, &quot;width&quot;=>&quot;259&quot;, &quot;border&quot; => &quot;4&quot;);
echo image_tag(&quot;/apache_pb.gif&quot;);
echo &quot;<br>&quot;;
echo $output = image_tag(&quot;/apache_pb.gif&quot;, $myarray);
echo $output = ul_list($myarray);
print table_row(table_cell(&quot;hellow world&quot;, array(&quot;align&quot;=>&quot;right&quot;)));
print $output = select_field(&quot;customers&quot;, db_values_array(customers, name), &quot;&quot;, &quot;onChange=\&quot;filltext()\&quot;&quot;);

echo &quot;<br>&quot;;
echo &quot;<br>&quot;;
echo text_field(db, &quot;&quot;, 50);
//echo &quot;<input type = &quot;text&quot; name = db size = &quot;50&quot; value = &quot;guestbook&quot;><br>&quot;;
echo checkbox_field(&quot;Check yes or no&quot;, &quot;Yes&quot;, &quot;no&quot;, &quot;no&quot;);




?>
 
I'm going to give you an advise, for you and for other begginers.

In order to make your code more readable and easier to understand, write HTML and JS outside PHP. If you need to put some PHP inside the HTML just open there the tag. Another thing, don't forget PHP is server side and JS is client side. You can't mix their variables.

TO do what you want, you must read ALL the addresses to a JS array and then on change pick the address you want.

My suggestion is onchange submit form to himself and fill the address using the costumer you picked up.


Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Did you get this working? Realize this is one month later, but if you need this, I can give you the code.
 
cdthompso1,
I would like very much to see that code if you would not mind posting it..

Thank you.
AnonyMouse
 
I never did get this going. On advice from a couple of people, I just submited the page to itself, and re-loaded it. This was not exactly what I wanted to do, and if you could post code that shows how to do it, that would be great. Or if you could link to a page that does it, thats cool too. Thanks for any help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top