CB150Special Posted July 29, 2017 Share Posted July 29, 2017 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? Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 29, 2017 Author Share Posted July 29, 2017 I haven't see this done before but it works perfectly. Get data from both tables first. require ('sql_A.php'); // Datarequire ('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. Quote Link to comment Share on other sites More sharing options...
kicken Posted July 29, 2017 Share Posted July 29, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
CB150Special Posted July 29, 2017 Author Share Posted July 29, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.