Forum Moderators: coopster
I have used httpwebwitch's solution of writing a second form and submitting it via javascript and it works well but I have run into a few problems.
Firstly, to pass this variable to second page as a hidden form element I had to first rename the variable
ie the variable passed to the page was $username
I set a hidden input <input name="username" type="hidden" value="$username">
to pass this variable onto a second page but no value was passed, however if I changed it slightly
$name = $username
<input name="username" type="hidden" value="$name">
then the correct value is now passed. Can anyone tell me why I need to do this?
My second problem and one I have yet to solve is this I have a page displaying data from a database. I have one element of this data act as a link to a pop up window to display more detailed data from the database. As httpwebwitch's method requires a form I add a hidden form to contain the needed variable to each row. To be safe I rename the variable like I did with $name/$username above and echo both variables just to be sure they have the correct values (which they do) but when I use the link I get a javascript error that document.form.id.value is null or not an object.
This is the code i am using to create the pop up window
<script>
function openpopup4(){var popurl="/php-cgiwrap/lrc/fault.php"
winpops=window.open(popurl,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=300,height=300");
winpops.document.write("<form name='myform' action='fault.php' method='post'>");
winpops.document.write("<input type='hidden' name='id' value='"+ document.form.id.value +"'>");
winpops.document.write("</form>");
winpops.document.forms.myform.submit();
}
</script>
and this is the code with the link to the pop up
echo "<table width=\"600\" cellspacing=\"0\" border=\"0\">";
echo "<tr align=\"left\" valign=\"top\"> ";
echo "<td width=\"150\"><strong>Base Serial Number</strong></font></td>
<td width=\"150\"><strong>Call Number</strong></td>
<td width=\"150\"><div align=\"center\"><strong>Date Logged </strong></div></td>
<td width=\"150\"><div align=\"center\"><strong>Logged By</strong></div></td>
</table>";
while($row = mysql_fetch_array($result)){if($bgcolor == "#E0E0E0"){
$bgcolor = "#FFFFFF";
}else{
$bgcolor = "#E0E0E0";
}
echo "<center>";
echo "<table width=\"600\" cellspacing=\"0\" border=\"0\">";
echo "<tr align=\"left\" valign=\"top\"bgcolor=".$bgcolor."> ";
echo "<td width=\"150\"><a href=\"javascript:openpopup4()\"><font color=\"#FF0000\"><strong>$row[base_serial_no]</strong></font></a></td>
<td width=\"150\"><strong>$row[call_number]</strong></td>
<td width=\"150\"><div align=\"center\">$row[date_logged] </div></td>
<td width=\"150\"><div align=\"center\">$row[logged_by]
</div></td>";
// Hidden form to pass id value to pop up window
echo "<form name=\"form\" method=\"post\" action=\"/php-cgiwrap/lrc/fault.php\">";
$faultnumber = $row[id];
echo "<input name=\"id\" type=\"hidden\" value=\"$faultnumber\">";
// echo variables just to see their values
echo "$faultnumber $row[id]";
echo "</form>
</table>";
}
Have I done something wrong somewhere?
I assume though, to get and use $_POST[] the form needs to be submitted and in my case it isnt. The form only exists to provide values for the form created by the javascript function. And at the moment that is picking up a null value :(
Nope tried that and still a null value returned.
The original javascript from httpwebwitch's post works fine for me on other pages. The only thing I can think of that is different in this case is, on the pages where it works there is only 1 form and only 1 possible value for the variable I want to pass whereas now I have as many forms as entries in the database (all called form) and the value I want to pass is $row[variable] rather than $variable
But I have no idea really :D
this code:
echo "<td width=\"150\"><a href=\"javascript:openpopup4()\">....";
will be:
echo "<td width=\"150\"><a href=\"javascript:openpopup4('$row[value]')\">....";
and than at the javascript function:
this line:
function openpopup4(){
will be:
function openpopup4(my_var){
and this line:
winpops.document.write("<input type='hidden' name='id' value='"+ document.form.id.value +"'>");
will be:
winpops.document.write("<input type='hidden' name='id' value='"+ my_var +"'>");