Jump to content

window.onload and sql query timing.


CB150Special

Recommended Posts

My basic question is about the actual timing of events on an HTML page

 

I have a page and on that page I have a <select> item list. I want to position the select index on a predetermined position based on a SQL result. 

 

The page does an SQL query to return a set of results. One of the fields needs to get its data range from another table and then set the the select index to the matching field.

 

The function to set the matching field is along the lines of

    <script>
        function getposition() {
        document.getElementById("Name").value = <?php echo $row['Name'];?>;
        }
    </script>

If a window.onload is executed, I assume that neither of the sql queries will have been done at that point.

 

What I think needs to happen is something like

 

Display the page headings, execute the SQL queries, run the script and the complete the page with the resulting data.

 

Can I get PHP or HTML to execute a the getposition() script after the queries?

 

 

post-204755-0-48870600-1501309542_thumb.jpg

Link to comment
Share on other sites

I haven't see this done before but it works perfectly.

 

Get data from both tables first.

 

require ('sql_A.php');    // Data
require ('sql_B.php');   // List source

<select id='data'>                 
    <?php
    while($row_A = mysqli_fetch_array($result_A)){
        $selected='';  
        if ($row_A['feildname'] == $row_B['feildname']) {
            $selected='selected';
        }?>
        <option value="row"  <?php echo $selected;?> > <?php echo $row_B['feildname];?> </option>   
        <?php
    }?>
</select>    

Now I just have to work out how to find the selected cell to to do an update.

Link to comment
Share on other sites

In a given request, your PHP code is always run completely before any of the javascript code. This is because they run in different contexts. PHP Code is executed by the server to generate the page. Once it's done and the page is generated the result is sent to the browser which will then execute any Javascript.

 

It's not clear to me what you're really needing help with. What you do need to do however is read up on Cross-site scripting. You need to escape any data you output into HTML and you should avoid outputting data into javascript code directly.

  • Like 1
Link to comment
Share on other sites

I'm new to HTML PHP etc. You have clarified my thoughts. Do all the PHP stuff then display the page.

 

I gather that my code as in #2 puts the pointer on the selected record as the page is painted.

 

My original code/thought would have painted the picture and then positioned the pointer on the selected record.

 

So I'm beginning to think to find the newly selected record, my only option is JS.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.