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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP + Javascript problem. encoded variable, cannot decode

Status
Not open for further replies.

billycat

MIS
Apr 28, 2005
22
US
I have a child window, with a select box. When a selection is made in the select box, javascript updates the opener form textbox with the value selected. However, it is coming encoded. If i do not encode the values with php, then the variable doesnt get posted correctly.

below is code.

child page code.

<html>
<head>
<script langauge="javascript">
function post_value(){
opener.document.myform.values.value = document.frm.c_name.value;
self.close();
}
</script>
</head>
<body><form name="frm" method=post action=''>
<?
include('includes/db.php');
$name = $_GET['row'];
$query = $name;




$q = "SELECT * FROM ".DB_TABLE_SELECTION." WHERE row_name = '$name'";
$result = $database->query($q);
$num1 = mssql_num_rows($result);

for($i = 0; $i < $num1; $i++){
$row = mssql_fetch_array($result);
$array[$i] = $row['row_option'];
}
echo "<br><b>".$query."</b><br><select name=c_name onclick=\"post_value();\" value=".$query." class=formfield size=6 style=\"width: 50mm;\" >";
sort($array);

$blah = count($array);
for($k = 0; $k < $blah; $k++){
echo "<option value=".rawurlencode(trim($array[$k])).">".$array[$k]."</option>";
}
echo "</select><br><br>";

?>

</form>
</body>
</html>

parent code.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
var TestVar = form.mylist.value;
window.open('select_options.php?row=' + TestVar);
decodeURI(document.myform.values.value);
}



function enableField()
{
document.myform.wild.disabled=false;
}

</script>


</head>
<body>
<table bgcolor="#d0cece" cellspacing="3">
<tr>
<td valign=top>
<?
include('includes/db.php');

echo "<form name=myform>";
echo "<b><font size=2 face=arial>Field:&nbsp;&nbsp;&nbsp;&nbsp;</b>";

$q = "SELECT row_name FROM ".DB_TABLE_ROWNAMES;
$result = $database->query($q);
$num = mssql_num_rows($result);

for($i = 0; $i < $num; $i++){
$row = mssql_fetch_array($result);

$array[$i] = $row['row_name'];

}
echo "<select name=mylist class=formfield>";
sort($array);
$arr = array_unique($array);

foreach($arr as $line){
echo "<option value=".$line.">".$line; }
echo "</select>";
?></td><td>
<input type="radio" name="wild" value="wildcard" ><font size=2 face=arial><b>wildcard</b>
<input type="text" name="wildcard"><br>
<input type="radio" name="wild" value="value" onClick=testResults(this.form)><font size=2 face=arial><b>value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>
<?
$form = "<INPUT TYPE=\"text\" NAME=\"values\" value=\"\" >";
echo rawurldecode($form);
?>



</td>
</tr>
</table>
<input type="button" value="submit" onClick="window.open('test.php?row=' + this.form.values.value);">
</FORM>
</BODY>
</HTML>




 
That code isn't much good, as we don't have access to your back-end DB. How about posting the client-side code so we have a better idea of the sort of data being given to the browser?

Also, when you do re-post, can you enclose your code in [ignore]
Code:
[/ignore] tags so that is formatted in a more visually appealing and easier-to-read way?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
sorry about that, parent window.
Code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<SCRIPT LANGUAGE="JavaScript">
function testResults (form) {
var TestVar = form.mylist.value;
window.open('select_options.php?row=' + TestVar);
decodeURI(document.myform.values.value);
}



function enableField()
{
document.myform.wild.disabled=false;
}
 
</script>


</head>
<body>
<table bgcolor="#d0cece" cellspacing="3">
<tr>
<td valign=top>
<form name=myform><b><font size=2 face=arial>Field:&nbsp;&nbsp;&nbsp;&nbsp;</b><select name=mylist class=formfield><option value=app_server>app_server<option value=asset_tag>asset_tag<option value=backup_schedule>backup_schedule<option value=backup_server_name>backup_server_name<option value=cpus>cpus<option value=domain_name>domain_name<option value=drac_hostname>drac_hostname<option value=drac_ip>drac_ip<option value=drive_capacity>drive_capacity<option value=eav_ver>eav_ver<option value=firmware_bios_updates>firmware_bios_updates<option value=gis_supported>gis_supported<option value=ip_address>ip_address<option value=last_modified_by>last_modified_by<option value=maint_lease_expiration_date>maint_lease_expiration_date<option value=maintenance_contract>maintenance_contract<option value=maintenance_vendor>maintenance_vendor<option value=manufacturer>manufacturer<option value=model>model<option value=modified_date>modified_date<option value=monitored>monitored<option value=neteiss_path>neteiss_path<option value=number_of_nics>number_of_nics<option value=operational_support_hours>operational_support_hours<option value=primary_admin>primary_admin<option value=processor>processor<option value=region>region<option value=second_ip_address>second_ip_address<option value=secondary_admin>secondary_admin<option value=serial_number>serial_number<option value=server_alias_name>server_alias_name<option value=server_description>server_description<option value=server_location>server_location<option value=server_name>server_name<option value=server_os>server_os<option value=status>status<option value=team_lead>team_lead<option value=total_ram>total_ram<option value=uam>uam</select></td><td>
<input type="radio" name="wild" value="wildcard" ><font size=2  face=arial><b>wildcard</b>
<input type="text" name="wildcard"><br>
<input type="radio" name="wild" value="value" onClick=testResults(this.form)><font size=2  face=arial><b>value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</b>
<INPUT TYPE="text" NAME="values" value="" >


</td>
</tr>
</table>
<input type="button" value="submit" onClick="window.open('test.php?row=' + this.form.values.value);">
</FORM>
</BODY>
</HTML>
child window
Code:
<html>
<head>
<script langauge="javascript">
function post_value(){
opener.document.myform.values.value = document.frm.c_name.value;
 self.close();
}
</script>
</head>
<body><form name="frm" method=post action=''>
<br><b>eav_ver</b><br><select name=c_name onclick="post_value();" value=eav_ver class=formfield size=6 style="width: 50mm;" >
<option value=7.1>7.1</option>
<option value=Norton>Norton</option>
<option value=Not%20Wintel>Not Wintel
</option><option value=Symantec>Symantec</option>
<option value=v%207.1.501>v 7.1.501</option>
</select>
<br><br>
</form>
</body>
</html>
 
Aaah - it could be as simple as putting quotes around your option values in the child window... e.g.

Code:
echo '<option value="' . rawurlencode(trim($array[$k])) . '">' . $array[$k] . '</option>';

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Though this isnt the HTML forum, HTML-wise, you should really put quotations around ALL attributes in the HTML tags and close all tags, this may prevent errors later on.

Quick example:
Code:
<option value=app_server>app_server

becomes:
Code:
<option value="app_server">app_server</option>


[monkey][snake] <.
 
BillyRay, Thanks much.. 3 days of headaches, fixed by quotes haha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top