AJAX,PHP和Javascript来填充表单输入设备的问题 [英] AJAX, PHP, and Javascript to populate a form input device issues

查看:168
本文介绍了AJAX,PHP和Javascript来填充表单输入设备的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



背景:

p>

我有一个需要有动态下拉列表的表单,基于一个PostScript(文本框),这是由AJAX函数完成的,该函数调用了下面的一个PHP文件

  .... 

< select name =choice1id =choice1>
<?php
//阻止从SQL命令填充下拉菜单的代码
?>
< / select>

....

功能可以在更改时显示的邮政编码,并变得人口稠密。我的问题是,我注意到它不是表单,或者我不能使用JS ..与这个设备,因为现在我需要从下拉菜单中获取一个值到另一个文本框。



是否可以将其添加到表单?甚至至少使用JS,所以我可以得到的价值?



Thankyou ..



编辑



这是我调用PHP文件的函数:

  function choiceDropdowns(code){
var postcode = code.value;
var xmlhttp;

if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4&& xmlhttp.status == 200){
document.getElementById( DIV4\" )的innerHTML = xmlhttp.responseText。
}
}

xmlhttp.open(GET,createChoice123DropDown.php?postcode =+ postcode,true);
xmlhttp.send();
}

PHP文件:

 <身体GT; 
< select name =choice1id =choice1>
<?php
$ postcode = $ _GET [postcode];

$ conn = mysql_connect(localhost,XXXX,XXXX);
mysql_select_db(XXXX,$ conn)
or die('Database not found'。mysql_error());

$ sql =SELECT school_info.Name,school_info.schoolID FROM school_info INNER JOIN local_schools ON school_info.schoolID = local_schools.schoolID INNER JOIN valid_postcodes ON local_schools.postcodeID = valid_postcodes.id WHERE valid_postcodes.postcode!= '$ postcode'AND school_info.school_type ='S';
$ rs = mysql_query($ sql,$ conn)
或者死(查询问题mysql_error());

while($ row = mysql_fetch_array($ rs)){
echo'< option value ='。$ row [schoolID]。'>'。$ row [ 名称] '< /选项>'。
}
?>
< / select>
< / body>

这是我的形式(明显缩小):

 < form method =getid =eoi_formaction =<?php echo $ _SERVER [PHP_SELF];?>> 
< td>选择1:< div id =div4>< / div>< / td>
< / form>


解决方案

你做错了=)你为什么不使用json返回所有选项,然后将其附加到选择框:

  var select = $('#my_select ')
$ .post('update_options',function(response){
$ .each(response.options,function(i,item){
var option = $('选项>');
if(item.value)option.attr('value',item.value);
option.text(item.text);
select.append );
});
});


Im way out of my knowledge on this one, so i thought i could need some help...

Background:

I have a form which needs to have dynamic drop down lists, based on a postcode(text box) this was done by AJAX function which called a PHP file like below

....

<select name="choice1" id="choice1">
    <?php
         //Block of code to populate drop down menu from a SQL command
    ?>
</select>

....

The functionality works, as it displays on change of postcode, and becomes populated. My issue is that i have noticed its not apart of the form or i cannot use JS.. with this device as now i need to take a value from the drop down menu into another textbox.

is it possible to add this into the form? or even atleast use JS so i can get the value?

Thankyou..

EDIT

This is my function which calls the PHP file:

function choiceDropdowns(code){
        var postcode = code.value;
        var xmlhttp;

        if (window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
        }
        else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function(){
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("div4").innerHTML=xmlhttp.responseText;
            }
        }

        xmlhttp.open("GET","createChoice123DropDown.php?postcode="+postcode,true);
        xmlhttp.send();
    }

PHP file:

<body>
    <select name="choice1" id="choice1">
    <?php
        $postcode = $_GET["postcode"];

        $conn = mysql_connect("localhost", "XXXX", "XXXX");
        mysql_select_db(XXXX, $conn)
        or die('Database not found ' . mysql_error());

        $sql = "SELECT school_info.Name, school_info.schoolID FROM school_info INNER JOIN local_schools ON school_info.schoolID = local_schools.schoolID INNER JOIN valid_postcodes ON local_schools.postcodeID=valid_postcodes.id WHERE valid_postcodes.postcode != '$postcode' AND school_info.school_type ='S'";
        $rs = mysql_query($sql, $conn)
        or die ('Problem with query' . mysql_error());

        while($row = mysql_fetch_array($rs)){
            echo '<option value="'.$row["schoolID"].'">'.$row["Name"].'</option>';
        }
    ?>
    </select>
</body>

This is my form (cut down in size obviously):

<form method="get" id="eoi_form" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
    <td>Choice 1:<div id="div4"></div></td>
</form>

解决方案

You doing it wrong =) Why don't you return all your options with json, and after that just append it to your select box:

var select = $('#my_select')
$.post('update_options', function(response) {
    $.each(response.options, function(i, item) {
        var option = $('<option>');
        if (item.value) option.attr('value', item.value);
        option.text(item.text);
        select.append(option);
    });
});

这篇关于AJAX,PHP和Javascript来填充表单输入设备的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆