代码插入输入

时间:2014-10-04 15:53:01

标签: javascript php jquery popup

我的PHP表单有麻烦,所以我需要一些帮助。

当用户编写邮政编码(Código邮政)并点击“Buscar”时,脚本会查看数据库并使用信息填写表单的其余部分。我已经管理过各种场景:

  1. 如果邮政编码不存在,则会给出警告。
  2. 如果邮政编码是正确的,它会用邮政编码,城市和州填写表格。
  3. 我的问题来自第三个问题。如果邮政编码存在但是多个城市使用该邮政编码(它可能发生在西班牙邮政编码......)我想打开一个弹出窗口,新窗口,动态弹出窗口...用一个选择这样用户可以选择正确的城市......

    我设法检测到您放置多个城市使用的邮政编码的时间,但问题是在触发弹出窗口的脚本时,而不是工作它只是在“Población”上显示代码“和”Provincia“输入......

    以下是我正在使用的代码:

    的index.php

    <input type="text" maxlength="5" class="form-control" id="postal_code" name="postal_code" placeholder="Código postal" pattern="[0-9]{5}">
    <button class="btn btn-info btn-flat" type="button" onclick="updateCityState();">Buscar</button>
    <input type="text" id="city" class="form-control" placeholder="Población">
    <input type="text" id="state" class="form-control" placeholder="Provincia">
    ........
    <script src="popup.js"></script>
    <script>var ajax = getHTTPObject();
    
            function getHTTPObject()
            {
                var xmlhttp;
                if (window.XMLHttpRequest) {
                  // code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp=new XMLHttpRequest();
                } else if (window.ActiveXObject) {
                  // code for IE6, IE5
                  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                } else {
                  //alert("Your browser does not support XMLHTTP!");
                }
                return xmlhttp;
            }
    
            function updateCityState()
            {
                if (ajax)
                {
                    var zipValue = document.getElementById("postal_code").value;
                    if(zipValue)
                    {
                        var url = "get_cities.php";
                        var param = "?postal_code=" + escape(zipValue);
    
                        ajax.open("GET", url + param, true);
                        ajax.onreadystatechange = handleAjax;
                        ajax.send(null);
                    }
                }
            }
            function handleAjax()
            {
                if (ajax.readyState == 4)
                {
                    if( ajax.responseText.length ) {
                        citystatearr = ajax.responseText.split(",");
                        city.value = citystatearr[0]; 
                        state.value = citystatearr[1];
                    }else{
                        city.value = "No existe";
                        state.value = "No existe";
                    }
                }
            }
        </script>
    

    get_cities.php

    <?php
    include_once('../../../connect.html');
    //perform lookup
    $title = ($_GET['postal_code']);
    $statement = $connection->prepare ("SELECT city, state FROM cities, states WHERE cities.state_id = states.state_id AND cities.postal_code = ?");
    $statement->execute(array($title));
    $statement->setFetchMode(PDO::FETCH_ASSOC);
    
    $items = array();
    while ($r = $statement->fetch()) {
        //$arrayName = array($r = $statement->fetch());
        $items[] = $r;
    }
    
    if (count($items) == '1'){
    
        $newArray = $items[0];
        echo $newArray['city'].",".$newArray['state']; 
    }elseif (count($items) == '0'){
            echo "Doesn't exist".","."Doesn't exist";
        }else{
            <script type="text/javascript">
    centerPopup();
            loadPopup();
    </script>
    
        <div id="popupContact">
        <a id="popupContactClose">x</a>
        <h1>Title of our cool popup, yay!</h1>
        <p id="contactArea">
            Here we have a simple but interesting sample of our new stuning and smooth popup. As you can see jQuery and CSS does it easy...
            <br/><br/>
            We can use it for popup-forms and more... just experiment!
            <br/><br/>
            Press ESCAPE, Click on X (right-top) or Click Out from the popup to close the popup!
            <br/><br/>
            <a href="http://www.yensdesign.com"><img src="logo.jpg" alt="Go to yensdesign.com"/></a>
        </p>
    </div>
    <div id="backgroundPopup"></div>
    }
    
    ?>
    

    popup.js

       /***************************/
    //@Author: Adrian "yEnS" Mato Gondelle
    //@website: www.yensdesign.com
    //@email: yensamg@gmail.com
    //@license: Feel free to use it, but keep this credits please!                  
    /***************************/
    
    //SETTING UP OUR POPUP
    //0 means disabled; 1 means enabled;
    var popupStatus = 0;
    
    //loading popup with jQuery magic!
    function loadPopup(){
        //loads popup only if it is disabled
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#popupContact").fadeIn("slow");
            popupStatus = 1;
        }
    }
    
    //disabling popup with jQuery magic!
    function disablePopup(){
        //disables popup only if it is enabled
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#popupContact").fadeOut("slow");
            popupStatus = 0;
        }
    }
    
    //centering popup
    function centerPopup(){
        //request data for centering
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $("#popupContact").height();
        var popupWidth = $("#popupContact").width();
        //centering
        $("#popupContact").css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });
        //only need force for IE6
    
        $("#backgroundPopup").css({
            "height": windowHeight
        });
    
    }
    
    
    //CONTROLLING EVENTS IN jQuery
    $(document).ready(function(){
    
        //LOADING POPUP
        //Click the button event!
    
    
        //CLOSING POPUP
        //Click the x event!
        $("#popupContactClose").click(function(){
            disablePopup();
        });
        //Click out event!
        $("#backgroundPopup").click(function(){
            disablePopup();
        });
        //Press Escape event!
        $(document).keypress(function(e){
            if(e.keyCode==27 && popupStatus==1){
                disablePopup();
            }
        });
    
    });
    

    因此,当脚本检测到用户查找属于多个城市的邮政编码并单击“Buscar”而不是打开弹出窗口时,它会使用popup.js的内容填充表单。我真的不知道为什么会这样,所以任何帮助都会受到赞赏!!

1 个答案:

答案 0 :(得分:0)

因此,如果我正确地阅读了您的问题和评论,那么这并不是您正在处理的代码,但我似乎明白了您的观点。对于其他人的信息,他在脚本get_cities.php中的else子句中使用echo,现在使用echo。

你的ajax处理程序需要一个例外。

function handleAjax()
{
   if (ajax.readyState == 4)
   {
      if( ajax.responseText.length && ajax.responseText.indexOf("script") < 0 ) { //<-- here
        citystatearr = ajax.responseText.split(",");
        city.value = citystatearr[0]; 
        state.value = citystatearr[1];
     }

然后添加新条件

 else if ( ajax.responseText.lengt && ajax.responseText.indexOf("script") != -1 ){
   // as I see you have also jquery I will use it here
   $('body').append(ajax.responseText);
 } 

然后是代码的其余部分

 else{
        city.value = "No existe";
        state.value = "No existe";
      }
    }
  }

但是这整个代码并不能解决你将面临的所有问题。我建议在你的html(php)中创建一个模态窗口,然后用ajax中的信息填充它。