How to insert a PHP dropdown in a HTML / Javascript page

蓝咒 提交于 2019-12-11 02:09:17

问题


Ok, this is my second post, and PLEASE accept that I am a complete newbie, willing to learn, spending many hours trauling various sites for answers, and I've almost got to where I need to be (at least for this bit).

I have a web page which has a nubmer of javascript functions that work together to generate a Google Map with various lines etc. using the google maps API.

I also have a MySQL Database with some information in. I have created a PHP script to dynamically generate a dropdown box with information from the database. (and that bit seems to work fine) - http://www.bournvilleconservatives.com/temp/select.php

What I now need to do is get that dropdown box to appear in the HTML / Javascript page that I already have, so that when a user selects something from the list, it calls a javascript function with the value selected.

I'm told I need to use AJAX to pull the box in, and use innerhtml to display it, but I really have no idea how, and could do with an example of something similar to help me on my way.

I would put the PHP in the html page, but that page is actually wrapped up in a Joomla! wrapper, so its all rather complicated.

Thanks in advance.


回答1:


jQuery solution

If you are willing to use jQuery, it will help you a lot with accessing the DOM, making Ajax calls and stuff. Let me give you a solution in jQuery:

First, put a div into HTML (this will store your select box):

<div id="i_want_my_select_here"></div>

Then put this code in the end of you HTML before </body>.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
  $(document).ready(function() {
    $('#i_want_my_select_here').load('/temp/select.php');
  });
</script>

In the first script tag, we include the jQuery library from Google's CDN. In the second, we write our Javascript/jQuery code. The .load() function makes it easy to make an Ajax call and load the response into an HTML element.

You can see this is much easier than all that code in my other answer :).




回答2:


If you're using prototype, you can use either Ajax.Request or Ajax.Updater, tho you should have a parent div with either to replace/insert into.

Example w/ Request:

new Ajax.Request('select.php', {
  method: 'post',
  onSuccess: function(r) {
    var select = r.responseText;
    $('parent_div').update(select);
  }
});

Example w/ Updater:

new Ajax.Request('parent_div', 'select.php', { method: 'post' });



回答3:


First, the Ajax example (taken from tizag.com and modified), Javascript code comes:

var ajaxRequest;  // The variable that we will put an XMLHTTPRequest object in

//We try to create an XMLHTTPRequest object, 
//it is the object that lets us use Ajax

try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
} catch (e){
    // Internet Explorer Browsers
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
}

// Create a function that will receive data sent from the server
// and do stuff with it (this function will only run, 
// when the data arrives back from the server!)
ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4){ //if request is successful
           //HERE COMES THE DOM INSERTION
    }
}

//We call the PHP file
ajaxRequest.open("GET", "/temp/select.php", true);
ajaxRequest.send(null); 

What basically happened is that through XMLHTTPRequest we called your PHP file. When the response (PHP file's output) comes, ajaxRequest.onreadystatechange will run instantly. So whatever we want to do with the data received, we have to do it in the place I've written //HERE COMES THE DOM INSERTION.

We want to insert the output into the HTML. To take the easiest route, first create a div/span in your HTML at the exact place you want your select to appear (it's very important to define the ID).

<div id="i_want_my_select_here"></div>

Then again, here comes the Javascript that replaces //HERE COMES THE DOM INSERTION.

//use the id to get Javascript access to the DIV
var div=document.getElementById('i_want_my_select_here');
//put the output of PHP into the div
div.innerHTML=ajaxRequest.responseText;


来源:https://stackoverflow.com/questions/5341792/how-to-insert-a-php-dropdown-in-a-html-javascript-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!