PHP Auto Fill

They have: 38 posts

Joined: Dec 2002

I have a form that I would like to have an auto fill with. There are 7 particular values. Depending on the value chosen I would like it to autofill in the cost and another additional field. Are there any scripts/tutorials on how to do this?

Thanks!

druagord's picture

He has: 335 posts

Joined: May 2003

if you mean without reloading the page or go to another page then php can set the values in a javascript wich will be called in the onchange param of your select. Is that what you want you are not really specific

IF , ELSE , WHILE isn't that what life is all about

They have: 38 posts

Joined: Dec 2002

Yes that is what I would like. Are there any scripts/tutorials for this? Thanks

druagord's picture

He has: 335 posts

Joined: May 2003

put this in head

<script language="javascript">
function autofill()
{
        switch (document.myform.myselect.selectedIndex)
        {
              case 0:
                   alert('select something');
                   break;
              case 1:
                    document.myform.myinput1.value='value1';  // hard coded way
                    document.myform.myinput2.value='<?=$myvalue2;?>';  // php way
                    document.myform.myinput3.value='value3';  // hard coded way
                    document.myform.myinput4.value='value4';  // hard coded way
                    document.myform.myinput5.value='value5';  // hard coded way
                    break;
              case 2:
                    document.myform.myinput1.value='value10';  // hard coded way
                    document.myform.myinput2.value='<?=$myvalue11;?>';  // php way
                    document.myform.myinput3.value='value12';  // hard coded way
                    document.myform.myinput4.value='value13';  // hard coded way
                    document.myform.myinput5.value='value14';  // hard coded way
                    break;
       }
}
&lt;/script&gt;
'

put this in your page

<form name="myform">
<select name="myselect" onchange="autofill()">
<option> select something </option>
<option> choice 1</option>
<option> choice 2</option>
</select>
<input type="text" name="myinput1">
<input type="text" name="myinput2">
<input type="text" name="myinput3">
<input type="text" name="myinput4">
<input type="text" name="myinput5">
</form>
'

now you should see the logic in this

IF , ELSE , WHILE isn't that what life is all about

They have: 38 posts

Joined: Dec 2002

Thank you! I see the logic but I do have a couple of questions. How can I get the myselect to input just one text field based on the selection?

druagord's picture

He has: 335 posts

Joined: May 2003

you just have to put only one line in the case section corresponding.

They have: 38 posts

Joined: Dec 2002

If I have the selections as radiobuttons, which I prefer, then autofill 2 fields depending on the choice. How would that change it?

druagord's picture

He has: 335 posts

Joined: May 2003

&lt;script language="javascript"&gt;
function autofill()
{
        for(i=0;i<document.myform.myradio.length;i++)
        {
              if(document.myform.myradio[i].checked)
              {
                      radiovalue=i;
              }
         }
        switch (radiovalue)
        {
              case 0:
                    document.myform.myinput1.value='value1';  // hard coded way
                    break;
              case 1:
                    document.myform.myinput1.value='<?=$myvalue11;?>';  // php way
                    break;
       }
}
&lt;/script&gt;
'
<form name="myform">
choice 1<input type="radio" name="myradio" onclick="autofill()">
choice 2<input type="radio" name="myradio" onclick="autofill()">

<input type="text" name="myinput1">

</form>
'

IF , ELSE , WHILE isn't that what life is all about

They have: 38 posts

Joined: Dec 2002

That works awesome! Thank you! You are very helpful and I am learning a lot!
I want to add a second field so if one of the radio buttons is checked an additional value goes into another text field. This field will change depending on the radio button selected.

druagord's picture

He has: 335 posts

Joined: May 2003

You have all the info you need on this thread to do just that. I could tell you how but you wouldn't learn Smiling

They have: 38 posts

Joined: Dec 2002

I got it to work! Thank you for all of your help! Smiling

druagord's picture

He has: 335 posts

Joined: May 2003

glad to hear that

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.