Hi i'm having a slight issue with loading variable into another page without refreshing first page. So here's the deal. I have two windows(pages), when i click a button(agents) in the parent window a child window opens with a list of agents from mysql database. Here are the parent and child pages respectively:

client_details.php

<?php include "includes/config.php"; ?>
...

//For Agents
if ( isset( $_GET['agentid'] ) && is_numeric( $_GET['agentid'] ) ) {
$sql = "SELECT agentID, CONCAT( firstName, ' ', lastName ) AS agent FROM tblagents WHERE agentID = {$_GET['agentid']}";
      if ( $result = mysql_query( $sql ) ) {
                if ( mysql_num_rows( $result) == 1 ) {
                    $agentDetails = mysql_fetch_array( $result );
                }
      }

}

...

<body>


<p>Agent Name:
<input type="agent" name="agentName" value="<?php 
                                        if ( isset( $_POST['agentName'] ) ) {
                                            echo $_POST['agentName'];
                                        } elseif ( isset( $agentDetails['agent'] ) ) {
                                            echo $agentDetails['agent'];
                                        }  

                                    ?>"/>
     <div style="margin-left:90px; margin-top:-10px">

<input type="button" value="Agents" name="agent_btn" class="btndesign" onclick="window.open('agents.php','popUpWindow','height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');">

</div>

</body>

agents.php

...

            <?php
                            while($row = mysql_fetch_array( $result ) ) {
                                ?>
                                    <tr bgcolor="#CCCCFF"> 
                                        <td><?php echo $row['agentNo']; ?></td>
                                        <td><?php echo $row['agent']; ?></td>
                                        <td><?php echo $row['branch']; ?></td>
                                        <td><?php echo $row['region']; ?></td>


                          <td><a href="javascript: if(confirm('Select Agent?')) {window.top.opener.location ='client_details.php?agentid=<?php echo $row["agentID"]; ?>';window.top.close();}">Select</a></td>                       
                                    </tr>
                                <?php
                            }
                        ?>

                    </table>
                <?php
            }
        ?>

...     

Now the problem is that in the popup child window agents.php, when i click the Select link and closes the popup window it retrieves the firstName & lastName variables and stores them in the agents textboxes in client_details.php but then reloads the parent page(client_details.php) and automatically resets all previous textfields. What i want to do is to retrieve the database variables and populate them in client_details.php without reloading the page when the child window(agents.php) select link is clicked.
I think AJAX will do the trick but i don't know how. Please help. Thanx in advance.

Recommended Answers

All 3 Replies

Member Avatar for LastMitch

Hi i'm having a slight issue with loading variable into another page without refreshing first page.

You need a SESSION.

I notice you don't have a SESSION. I'm just curious why you didn't include it in your code?

What i want to do is to retrieve the database variables and populate them in client_details.php without reloading the page when the child window(agents.php) select link is clicked.

Did you define these variable:

<td><?php echo $row['agentNo']; ?></td>
<td><?php echo $row['agent']; ?></td>
<td><?php echo $row['branch']; ?></td>
<td><?php echo $row['region']; ?></td>

There's something wrong with your javascript(s):

<td><a href="javascript: if(confirm('Select Agent?')) {window.top.opener.location ='client_details.php?agentid=<?php echo $row["agentID"]; ?>';window.top.close();}">Select</a></td>  

I think AJAX will do the trick but i don't know how. Please help. Thanx in advance.

You are correct about AJAX. But before any changes you need to clean up your code ( there's alot of minor errors which you can fixed by yourself ).

Member Avatar for diafol

I would avoid using a popup / new window. You can achieve the same with a dialog box (via js or ajax).

Using jQuery should make life a lot easier if you're going to use ajax - but your choice.

I'm a little confused as to what exactly is being passed to where here (my fault, not yours, I'm sure).

You could retrieve the agents' details on page load and store them in a hidden select or table. Then when you click the get agent button, show the select/table - either in the page or in a dialog (like a lightbox). When you select a name from the select widget/table, it can then populate fields in your main form.

This can be done either by loading all agent data on page load (so no ajax req'd) or you can use ajax to retrieve agent data on the fly.

The first technique is definitely the easiest, but it depends on the agent data. If it changes often, then perhaps you need to use ajax.

BTW, the only agent data that's required by the main form is the actual agent id, but adding the name for display purposes won't hurt. However, if you want to show the agent name, make sure it's a non-editable textbox or even plain text (e.g. <p>). Being able to edit the agent name isn't a valid action. The 'id' could be stored in a hidden field.

If I've got you right, that is.

Having said all that, why don't you just have a straight select with agent names:

<select name="agents">
    <option value="3">John Williams,....</option>
    <option value="17">Mary Jones,....</option>
    <option value="23">Tim Davies,....</option>
</select>

That would be produced dynamically by php from data in the DB.

Well i have scratched off my previous idea and used a more effective approach of using autocomplete ajax function, thus showing all agent names in the database that match the typed text. It narrows down the options the more you type and then you can click the name you want to show in the textbox. The idea came from the select dropdown technique you suggested, so thanks a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.