Jump to content

Dynamic Form with php - how to set values of a list


jwpryor

Recommended Posts

I have a form with a list/menu in it that is populated with records from a database. The items in list#2 are based upon what is selected in list#1.

 

I have been able to use a combination of php and javascript to get it to work, but the javascript reloads the page to get list #2 to refresh - and that clears out any other fields that have been filled in. Instead of javascript being called for the 'onchange' event for list #1, I'd rather it call php when list#1 is changed and so reset the values in list#2.

 

OPTIONS:

1. I thought I could have a php function that is called (i.e.  onchange="<?php reload();?>" but I can't figure out how to set the values of the list from within the php function. How to reference form objects with php?

 

or

 

2. is there some better way to do this?

 

 

Thank you

Julia

Link to comment
Share on other sites

JavaScript is run client-side, PHP is run server-side. You cannot call/run PHP in the browser. You could use javascript to call a PHP page and return back the results to the page to repopulate the select list. This is called AJAX. However, if your lists are relatively small I would suggest doing it all client-side in the JavaScript.

 

There's no reason that using JavaScript to repopulate a select list will reset all the fields on the form, unless you are doing something wrong. Perhaps it would help if you provided a link or a sample of the code.

Link to comment
Share on other sites

SITUATION: when 1 drop down list is selected, drop down list #2 is repopulated with new values (via javascript - see #2 below)

 

PROBLEM: When the drop down list changes, any other fields that have already been filled in, are reset - instead of keeping the value the user just entered.

 

-----------------------------------

I'm sure that I am probably doing something wrong - or least at this point don't know how to do it any differently. One thing there must be a different way of doing, is when the javascript is called, it then reloads the current page to reset list#2  (See #2 below). This is when the other fields that have been set are cleared out/reset.

 

I have placed the primary code below (instead of placing all the code). I can include all the code if this is not enough to help me with my problem.

Thank you,

Julia

 

-------------------------------

 

 

 

 

1. at the beginning of the file, I have PHP code to connect to the database and query results for list#1

 

   <?php

    //get the list of tier1 items

    mysql_select_db($database_connWIN, $connWIN);

    ///////// Getting the data from Mysql table for first list box//////////

    $quer2=mysql_query("SELECT tier1Name,tier1ID FROM tier1 order by tier1Name");

 

2. Within the HTML <head> tag, I have the javascript functions that are called for the 'onChange' event for the list #1.

 

<script language="JavaScript" type="text/JavaScript">

<!--

//function 'reload' called when list#1 has a change     onchange="reload(this.form)"

function reload(form)    

{

   var val=form.tier1.options[form.tier1.options.selectedIndex].value;

    self.location='enrollmentform.php?cat=' + val ;

}

//-->

</script>

 

3. Within the HTML form, I have php code that sets the values of the two lists. This is the code for list #2

 

<select name='area' onchange=""><option value=''>Choose the assessment site</option>

<?PHP

 while($noticia = mysql_fetch_array($quer)) {

 if($noticia['tier2ID']==@$cat3){echo "<option selected value='$noticia[tier2ID]'>$noticia[tier2Name]  </option>";}

 else{echo  "<option value='$noticia[tier2ID]'>$noticia[tier2Name]</option>";}

 }

?>

</select>

 

4. I have a text field  that gets reset each time the page is reloaded (via the javascript above).

 

input name="firstName" type="text" id="firstName" value="<?php if (isset($_POST['firstName'])) echo $_POST['firstName'];?>" maxlength="20">

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.