0

I have 2 PHP pages...

INDEX.PHP with this code:

<form method="post" action="" name="f1">
<input type="text" name='p_name' size='50'><br> 
<input type="text" name='p_name2' size='50'><br>
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick=window.open("index2.php","Ratting","width=550,height=170,left=150,top=200,toolbar=1,status=1,");>Click here to open the child window</a> 
</form>

and INDEX2.PHP with this avascript code:

<script langauge="javascript">
function post_value()
{
    opener.document.f1.p_name.value = document.frm.c_name.value;
    opener.document.f1.p_name2.value = document.frm.c_name2.value;
    self.close();
}
</script>

and this PHP/HTML:

<form name="frm" method="post" action="">
<?php
$sql="SELECT * from customer";
$rs=mysql_query($sql,$conn) or die(mysql_error());
while($result=mysql_fetch_array($rs))
{
    echo '<input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
    <input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
    <input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>';
}
?>
</form>

basically, when you go to index.php, you click the link to open the popup window index2.php and that then lists customers from a database and gives each one 2 text boxes - one for the sequence/id of the customer and the other for the company name and one submit button per row.

When the button is pressed, it runs the javascript function post_value(); which puts the values from the database/child popup window (index2.php) into the text boxes in the parent window (index.php)

when i run this code, it just puts the word undefined in box boxes on the parent page, however if i remove the while loop in php and it just displays the one row from the database of customers it works fine.

its like it doesn't like the while loop in php but i cannot work out why. any help would be much appreciated.

1

1 Answer 1

1

Edit - I just realized that your while loop is going to generate something that looks like:

<input type="text" name="c_name" size="50" value="A1" /><br>
<input type="text" name="c_name2" size="50" value="B1" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>

<input type="text" name="c_name" size="50" value="A2" /><br>
<input type="text" name="c_name2" size="50" value="B2" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>

<input type="text" name="c_name" size="50" value="A3" /><br>
<input type="text" name="c_name2" size="50" value="B3" /><br>
<input type=button value=\'Submit\' onclick=\'post_value();\'><br><br>

This isn't going to work because you have multiple elements named "c_name" and "c_name2" within the same form. I think what you want to do is put your form element inside the while loop like this:

<?php
    $sql="SELECT * from customer";
    $rs=mysql_query($sql,$conn) or die(mysql_error());
    $ctr = 0;
    while($result=mysql_fetch_array($rs))
    {
        echo '<form name="frm' . $ctr . '" method="post" action="">
        <input type="text" name="c_name" size="50" value="'.$result["sequence"].'" /><br>
        <input type="text" name="c_name2" size="50" value="'.$result["company"].'" /><br>
        <input type=button value=\'Submit\' onclick=\'post_value(' . $ctr . ');\'><br><br>' 
        </form>';
        $ctr++;
    }
?>

Notice how I created a $ctr variable so you can identify each form and how it is passed into post_value. You'll need to use that to grab the correct form like this:

<script langauge="javascript">
function post_value(ctr)
{
    opener.document.f1.p_name.value = document.forms["frm" + ctr].c_name.value;
    opener.document.f1.p_name2.value = document.forms["frm" + ctr].c_name2.value;
    self.close();
}
</script>

By default, mysql_fetch_array returns an array with numeric keys (i.e. 0, 1, 2, etc). If you want to retrieve records by their column name, you need to use:

while ($result = mysql_fetch_array($rs, MYSQL_ASSOC))

Look at example 3 here:

http://php.net/manual/en/function.mysql-fetch-array.php

Sign up to request clarification or add additional context in comments.

5 Comments

or just use mysql_fetch_assoc(). (or even better, switch to a DB library that isn't deprecated).
Hmmm... i tried changing to this with the , MYSQL_ASSOC but still the same issue.
Are you sure the column names are "sequence" and "company"? Try changing your sql statement to: "select sequence, company from customer" and see if that still returns data.
yeah column names are defiantly sequence and company as it is displaying the correct data in the popup/child window (index2.php) but once the button is pressed on index2.php to choose that row in the while loop it should put the values of sequence and company back into the 2 text boxes on the parent page but i just get undefined put in the text boxes on the parent page. i have also tried changing the sql statement to select sequence, company and changing while($result=mysql_fetch_array($rs)) to while($result=mysql_fetch_assoc($rs)) but still nothing
from your code that you gave me which works exactly how i need it to, would there be any way to display a javascript popup box if column1 for the selected customer is equal to yes?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.