0

I am calling child page and posting the form. Trying to refresh and rebuild the parent page based on value entered in child page. I am not able to see the child posted values in Parent page.

Expecting value $_POST['test_2'] populated when child is submitted from Parent page. I don't see the value posted by child page in parent page. See the code example below. Thanks for your help.. Advice me if there are any other better way to access the value submitted by child page and rebuilding the parent page using those child page values.

Parent Page is : Example1Parent.php

<html>
<head>
<body>
<h1> Welcome Parent </h1>
<div id="dateDiv"></div>
<form method=post action='' name=f1>
<table border=0 cellpadding=0 cellspacing=0 width=550>
<tr>
<td ><font size=2 face='Verdana'>Refresh upon Search</font>
<input type=hidden name='p_name2' id=n2 size='8'> 
<input type=button 
onClick=window.open("Example1Child.php","Ratting","width=550,height=170,left=150,top=200,toolbar=0,status=0,"); value="Advanced Search">
<?php
echo '<br> Welcome ';
if (!isset($_POST['test_2'])) echo "<br> t_test2 : it does not exists! ";
?>
</td></tr> 
</table>
</form>
</body>
</html>

Child Page is :Example1Child.php

 <html>
 <head>
 <title>ChildForm</title>

 <SCRIPT language=JavaScript>
 <!-- 
 function win(){
 window.opener.location.href="Example1Parent.php";
 self.close();
 //-->
 }
</SCRIPT>
</head>
<body>
<h1> Welcome to Child </h1>
<form name="child"  id="child"  METHOD="POST" action=''>
<input type="text" id="test_2" name="test_2" value="ENTER DATA TO PARENT PAGE">
<INPUT TYPE="SUBMIT" onClick="win();" value="SEND VALUE TO PARENT">
</form>

</body> 
</html>
  • Not to be mean, but your code seems to be from the late 90s (pop-ups, tables for text-styling, `font` markup, HTML comments in you JS code, ...). If you're learning HTML/JS/PHP, you should consider better/newer sources (e.g. http://www.w3.org/community/webed/wiki/HTML, https://developer.mozilla.org/en/learn/javascript, ...), it should change the way you design ;) – tmuguet Feb 03 '13 at 22:13
  • Thanks!!, Yes, I am beginner and trying to learn this technology. I am from mainframe background!! – user2037522 Feb 03 '13 at 22:45

1 Answers1

0

as tmuguet mentioned you might want to create a different method since this is a fairly old way of doing this. Now a days you can have popups/modal boxes and such to get a value then update the page with js/ajax or whatnot.

I'm guessing you are trying to pass values through windows via js which won't work since you open the window opening a clean link (no values passed even tough you have 'POST' in the form method) and you can use something like the following, but it will only give you GET values

<SCRIPT language=JavaScript>
<!-- 
    function win(){
        val = document.getElementById('test_2').value;//you will most likely need to encode this value if it has special characters
        window.opener.location.href="Example1Parent.php?test_2=" + val;
        self.close();
//-->
}
</SCRIPT>

What you are looking for is something like Window.open and pass parameters by post method problem

<form id="child" method="post" action="Example1Parent.php" target="Parent">
    <!-- <form name="child"  id="child"  METHOD="POST" action=''> -->
    <input type="text" id="test_2" name="test_2" value="ENTER DATA TO PARENT PAGE">
    <INPUT TYPE="SUBMIT" onClick="win();" value="SEND VALUE TO PARENT">
</form>

<script type="text/javascript">
    function win(){
        var f = document.getElementById('child');
        f.test_2.value =  document.getElementById('test_2').value;
        window.open('Example1Parent.php', 'Parent');
        f.submit();
        self.close();
    }
</script>

I have not tested this script

You might want to do a var_dump($_POST) in your Example1Parent.php file to see if f.test_2.value translates to $_POST['test_2']

Community
  • 1
  • 1
Class
  • 3,149
  • 3
  • 22
  • 31
  • Perfect!! It worked. Thanks a lot. I was struggling like crazy. – user2037522 Feb 03 '13 at 22:42
  • Hi Class, it is opening new parent page...instead of refreshing original parent page. – user2037522 Feb 04 '13 at 18:05
  • @user2037522 That's because `window.open();` creates a new window and naming it (so it knows where to send the information). I'm not sure exactly how you'd achieve what you are asking, it might be possible to do it via js and iframes on the same page, but I'd look for something simpler and more practical. Just give Google or SO a good searching for a while and you might find some better ways to do what your script is trying to do. – Class Feb 04 '13 at 18:24