Click to See Complete Forum and Search --> : Passing varibles


bobby_dummy_022
12-01-2003, 12:50 AM
Hi Everyone,

I'm sorta stuck with passing varibles and wandering if someone can help?

The following code is 2 different files (test.php, and select_user.php), test.php runs first, and when you select the image, a new window opens (select_user.php) and a list of users is displayed. I select the user I want, click on the image in the new window and the user_id is passed back to the original window, and the new window is closed. Now instead of getting the user_id I get a string passed back ('person' to be exact)... so any help is appreciated, (and I know this is a basic one)... :)

Now the code:
<test.php>
<?php
error_reporting( E_ALL );
$user_id = isset ($_POST['user_id'] ) ? $_POST['user_id']: 1;
?>

<script language="javascript">
var UserField = '';

function popUser( field ){
UserField = field;
window.open( 'select_user.php?&callback=setData', 'users', 'top=250,left=250,width=400, height=150, scrollbars=no' );
}
function setData( user_id ) {
my_field = eval( 'document.editFrm.' + UserField );
my_field.value = user_id;
}

</script>
<table cellspacing="0" cellpadding="4" border="1">
<form name="editFrm" action="test.php" method="post">
<tr>
<td align="right" nowrap="nowrap">User ID</td>
<td nowrap="nowrap">
<input type="text" name="user_id" value="<?php echo $user_id; ?>" class="text" disabled="disabled" />
<a href="#" onClick="popUser('user_id')">
<img src="update.gif" alt="Get User" border="0" />
</a>
</td>

<td align="right" width="50%" nowrap="nowrap">
<input class="button" type="submit" name="do_report" value="submit" />
</td>
</tr>
</form>
</table>
<?php
echo "User_ID is now: ".$user_id;
?>

<select_user.php>
<?php
$callback = $_GET['callback'];
?>
<script language="javascript">
function SelectUser( user_id ) {
window.opener.<?php echo $callback;?>(user_id);
window.close();
}
</script>

<tr><td align="right" nowrap="nowrap">For User:</td>
<td>
<form name="editFrm" action="test.php" method="post">
<select name="person">
<option value="1">User 1</option>
<option value="2">User 2</option>
<option value="3">User 3</option>
</select>
</td>
<td><a href="#" onClick="SelectUser('person')"> <img src="update.gif" alt="Set User" border="0" /></a></td>
</form>
</tr>
</table>

Gollum
12-01-2003, 03:10 AM
It's not surprising you are getting 'person' as the argument to your callback. A quick look at the code will see why...

<a href="#" onClick="SelectUser('person')">
...
function SelectUser( user_id ) {
window.opener.<?php echo $callback;?>(user_id);
window.close();
}


Your link tag is passing the value 'person' into the function SelectUser() which is then passing that value to the parent window.

Perhaps at some point you should access the combo box (of that name) to get the actual value you want to pass back...

function SelectUser( cmbName ) {
var user_id = document.form.editFrm[cmbName].value
window.opener.<?php echo $callback;?>(user_id);
window.close();
}

bobby_dummy_022
12-01-2003, 04:57 PM
Thanks for the reply, and I understand what I was doing wrong, but alas using the code you posted, nothing happens when I click to perform the SelectUser() function?

So I modded document.form.editFrm[cmbName].value -> document.editFrm[cmbName].value and the callback now works...

Now the SetData function doesn't set the variable in the parent file? so any suggestions would be appreciated.