0

在这里,我在 PHP 中遇到 AJAX 和 MYSQl 的问题,谁能帮助我?

我在表单中有选择控件:

    <form action="index.php" method="post" name="pretraga" class="border">
    <p>Mesto:</p>
                 <div class="spry-select">
                   <select name="mesto" id="select_x">
                   <option value="Novi Sad">Novi Sad</option>
                   <option value="">nesto</option>
                   <option value="">nesto</option>
                   </select></div>
    <div id="Rezultat">
<select name="podrucje" >
</select>
    </div>

当有人点击诺维萨德时,我想做这样的 Mysql 查询

$result = mysql_query("SELECT deo_grada FROM usluga 
WHERE deo_grada='{$_POST['mesto']}'");
while ($red=mysql_fetch_object ($result))
                {
echo'<option value="'.$red->deo_grada.'">'.$red->deo_grada.'</option>';
                }

退出的结果想要进入 id Rezultat?请问有什么帮助吗?

这不走运:(

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function (){
$('#select_x').change(function() {
  $.ajax({
  type: "POST",
  url: "some.php",
  data: { mesto: $("select_x option:selected")[0].val() }
}).done(function( msg ) {
  $("#Rezultat").html(msg);
});
});
   });
</script>
4

5 回答 5

2

更改 <select name="mesto"><select name="mesto" id="select_x">

$('#select_x').change(function() {
  $.ajax({
  type: "POST",
  url: "some.php",
  data: { mesto: $("select_x option:selected")[0].val() }
}).done(function( msg ) {
  $("#Rezultat").html(msg);
});
});

此示例需要 JQUERY。

您可以阅读更多内容:

http://api.jquery.com/change/

http://api.jquery.com/jQuery.ajax/

当文档准备好时,你也应该绑定事件。

$(function (){ //document is ready bind events });
于 2013-04-08T11:11:57.097 回答
0

包含 jQuery 并粘贴以下代码:

 $(function(){
   var res = $("#Rezultat");
   $("#selectid").change(function(){
     $.ajax({
      url:"index.php",
      method:"POST",
      data:{this.na`enter code here`me:this.value},
      success:function(data){
         res.html(data);
       }
     })
   })
})
于 2013-04-08T11:24:05.850 回答
0

改变

WHERE deo_grada='$_POST['mesto']'"

WHERE deo_grada='".$_POST['mesto']."'"
于 2013-04-08T11:07:46.107 回答
0
$result = mysql_query("SELECT deo_grada FROM usluga 
WHERE deo_grada='{$_POST['mesto']}'");

注意 sql 注入

于 2013-04-08T11:09:51.810 回答
0

这个 javascript:)

function xmlhttpPost(strURL,formname,responsediv,responsemsg) {

        var xmlHttpReq = false;

        var self = this;

        // Xhr per Mozilla/Safari/Ie7

        if (window.XMLHttpRequest) {

            self.xmlHttpReq = new XMLHttpRequest();

        }

        // per tutte le altre versioni di IE

        else if (window.ActiveXObject) {

            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

        }

        self.xmlHttpReq.open('POST', strURL, true);

        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

        self.xmlHttpReq.onreadystatechange = function() {

            if (self.xmlHttpReq.readyState == 4) {

                // Quando pronta, visualizzo la risposta del form

                updatepage(self.xmlHttpReq.responseText,responsediv);

            }

            else{

                // In attesa della risposta del form visualizzo il msg di attesa

                updatepage(responsemsg,responsediv);



            }

        }

        self.xmlHttpReq.send(getquerystring(formname));

    }



    function getquerystring(formname) {

        var form = document.forms[formname];

        var qstr = "";



        function GetElemValue(name, value) {

            qstr += (qstr.length > 0 ? "&" : "")

                + escape(name).replace(/\+/g, "%2B") + "="

                + escape(value ? value : "").replace(/\+/g, "%2B");

                //+ escape(value ? value : "").replace(/\n/g, "%0D");

        }



        var elemArray = form.elements;

        for (var i = 0; i < elemArray.length; i++) {

            var element = elemArray[i];

            var elemType = element.type.toUpperCase();

            var elemName = element.name;

            if (elemName) {

                if (elemType == "TEXT"

                        || elemType == "TEXTAREA"

                        || elemType == "PASSWORD"

                        || elemType == "BUTTON"

                        || elemType == "RESET"

                        || elemType == "SUBMIT"

                        || elemType == "FILE"

                        || elemType == "IMAGE"

                        || elemType == "HIDDEN")

                    GetElemValue(elemName, element.value);

                else if (elemType == "CHECKBOX" && element.checked)

                    GetElemValue(elemName, 

                        element.value ? element.value : "On");

                else if (elemType == "RADIO" && element.checked)

                    GetElemValue(elemName, element.value);

                else if (elemType.indexOf("SELECT") != -1)

                    for (var j = 0; j < element.options.length; j++) {

                        var option = element.options[j];

                        if (option.selected)

                            GetElemValue(elemName,

                                option.value ? option.value : option.text);

                    }

            }

        }

        return qstr;

    }

    function updatepage(str,responsediv){

        document.getElementById(responsediv).innerHTML = str;

    }

这是HTML

    <div class="spry-select">
                       <select name="mesto" onclick="xmlhttpPost('obrada.php', 'pretraga', 'Rezultat', ''); return false;"/>
                       <option value="Novi Sad">Novi Sad</option>
                       <option value="">nesto</option>
                       <option value="">nesto</option>
                       </select></div>
<select id="Rezultat" name="podrucje" >

               </select>

Txanks 很多 @作者:Simone Rodriguez aka Pukos < http://www.SimoneRodriguez.com >

于 2013-04-08T12:31:41.817 回答