Populate dynamic php dropdown from child window to parent form

dcjsdts
dcjsdts used Ask the Experts™
on
I have a form with dropdown boxes that are populated from a MySQL database.  I am trying to add an option next to the dropdown so that it will invoke a popup window so the user can quickly add a new name to the database lookup table.  (The one that populates the drop down right next to it).  The popup form consists of a text box and a submit button. Once the popup form has been submitted the value is being added database, and the parent window can be refreshed automatically to show the new value in the menu.

The only bad part about this whole scenario is that not only the menus are being refreshed but also the form fields that already have been filled out. Not a very desireable effect.

If the data being added to the child form and does not exist in teh database the function does not add the child window value to the option selected.  (and that would be its only purpose).  I have tested with an existing value (such as Smith, Jack in my example below) and it works fine.  It also works fine while updating a plain text input field.  It just wont let me add it to the selected option on a fresh add to the employees table without first refreshing the parent form.

/// Code on parent form
<select size="1" style="width: 80%" name="employee">
	<option selected></option>
	<?
	$query="SELECT * FROM employees";
	$result = mysql_query ($query);
	while($nt=mysql_fetch_array($result)){
	print "<option value='myEmp'>$nt[employee]</option>";
	}
	?>
</select>
 
//////  function on child form
 
function myTest() 
{
	opener.document.MySubmit.Employee.value = "Smith, Jack"; 
	self.close();
	return true; 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mark BradyPrincipal Data Engineer

Commented:
When you type something into the input 'text' field and submit the form, you need to have it add the new information to the database before the form refreshes. Just something I should add here, is that you would be best served if you used AJAX to do the form refreshing as it will update the database and load the new values without refreshing the whole form. Saves on bandwidth and is quicker to load.
In saying that, I can't write the code to achieve this as I don't know enough about ajax yet.

However, what you can do is add this code to the top of your parent form (just under the <? tag. Oh, use <?php and not <? so it is compatible with all browsers.

//new code to add

<?php
$submitted_name = $_POST['new_name'];
if($submitted_name){

mysql_query("INSERT INTO `employees` (name) Values ('$submitted_name')")or die(mysql_error());

// change (name) to whatever column name you have for their name
?>

ok so now once the form is submitted it will recognise that a new name has been submitted by using the $_POST variable. It will enter that name into the database. Now your page can continue with the code to populate the dropdown box and the new name will be in there.

Something to add, you will need to make sure the name is unique (firstname and lastname) otherwise you will get duplicate entries. I always do a check first to look for duplicates but you will need to have something solid to look for like fisrtname_lastname

Not likely to have 2 'john smiths' so depending on how you have your employee table setup, you could check like this:

$sql = "SELECT * FROM `employees`";
$result = mysql_query($sql)or die(mysql_error());

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    $firstname = "{$row['firstname']}";
    $lastname = "{$row[''lastname']}";
if($firstname ==  $submitted_first_name && $lastname == $submitted_last_name){
// do nothing
}else{
// insert the new name into the database
mysql_query("INSERT INTO `employees` (name) Values ('$submitted_name')")or die(mysql_error());
}
}


// To make that work you would need to have two input fields to hold firstname and lastname and the form would submit them both. I'm sure you get the idea.

Author

Commented:
I'll give anything a shot, but my current code *IS* populating the database correctly prior to trying to insert the new value as the selected value.  It's jsut that the screen (parent window) doesnt know it yet.  If I stop my code and just do a refresh on the existing window all is well.  Also, just as an fyi, I do have my name seperated into FName and LName and I also have it check for dupes.  I took all of that  mention out in hopes of making it an easy example.

My problem is that although I am 100% certain my child form is posting the data, it is not made available to the parent form until I do a refresh.  Once I do a refresh, any existing data entered and not saved in current fields goes 'poof'.
Mark BradyPrincipal Data Engineer

Commented:
That is because as soon as you refresh, the values that are already in the boxes are not being sent back to the page. In your form add hidden fields to hold the new data and that will get posted back so then your page looks for any posted data and puts it into a variable. Then when you have the code to display the form text field, you add the variable into it like :

<input type='text' name='FName' value='<?php echo $FName; ?>'>

You need to capture the $FName variable at the top of the page first. $FName = $_POST['FName'];

and so on for anything you want to capture
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Mark BradyPrincipal Data Engineer

Commented:
It is also easy to make the dropdown box be selected on the new value. I don't know how your code works for populating it but using an 'if' statement you can see if the Fname data from your database is equal to the $FName variable and if so:

echo "<option selected value'".$FName."'>".$FName;
}else{
echo "<option value'".$FName."'>".$FName;

Author

Commented:

while($nt=mysql_fetch_array($result)){
	print "<option value='myEmp'>$nt[LName], $nt[FName]</option>";
}

Open in new window

Mark BradyPrincipal Data Engineer

Commented:

while($nt=mysql_fetch_array($result)){
 
if($nt[LName] == $_POST['LName'] && $nt[FName] == $_POST['FName']){ 
        print "<option selected value='myEmp'>$nt[LName], $nt[FName]</option>";
}else{
print "<option value='myEmp'>$nt[LName], $nt[FName]
</option>";
}

Open in new window

Mark BradyPrincipal Data Engineer

Commented:
Woops, add one more } to the end of that

Author

Commented:
Yup.. thats just a confirmation of and a slightly different way to do what I'm already doing.  Now back to the initial question..  Still doesnt work.   I'm thinking of dynamically changing the drop down to a text box on the fly just so I can populatre it - or just creating an entry in the available selections called "Add New User on Submit" and on submit, when this value is present it will actually post all form data including the "Add New User on Submit" value and then follow through with the popup window asking for the new user information - then just edit the entry.

This is a bit dirty though, I'd still like to be able to do like I can do in MS Access which is a me.cboMyEmp.requery and then set the value.
Commented:
I changed it to a text box and developed an autosuggest routine where I can type any part of an existing string (MYSTRING) if I type "YST" it will suggest the name from the DB.  Then, on exit I'll just put some type of "Does the entry exist"..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial