duanfan9859 2015-01-01 22:06
浏览 47
已采纳

从输入中检索值并将其发布在同一PHP中

I want to read the value that is in the input box and based on that, create (with PHP in the same file) some dropdown boxes. After trying several hours I can't find a way to do this.

The HTML Input Field

<input type="text" onblur="sendData(this.value)" name="ingr_count" id="ingr_count" pattern="\d*" />

I have this JS/AJAX to read the value and post it in the PHP

function sendData(obj) {
            var xmlhttp;

            if (window.XMLHttpRequest) {
                xmlhttp=new XMLHttpRequest();
            } else {
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function() {
                if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    alert(obj);
                    // Do stuff when script returns
                }
            }

            xmlhttp.open("POST","script.php?count=" + obj.value + "&t="+Math.random(),true);
            xmlhttp.send();
        }

I also have this PHP to read the value

$src1= $_POST['count'];  
echo $src1; 

Commenting out everything bellow var xmlhttp; and alerting the obj will alert the obj but if I uncomment it won't even alert the value! :O .

  • 写回答

1条回答 默认 最新

  • dongyuan1984 2015-01-01 23:54
    关注

    After the comments of @Mura and @Fred-ii- I managed to complete my AJAX code. The problem was that when I recieved the value I couldn't use it in PHP. So when I read the value on AJAX I created a cookie and stored the input box value inside and after that I refresh the content of the PHP. So the final code is something like the below.

    The HTML

    <input type="text" onblur="sendData(this.value)" name="ingr_count" id="ingr_count" pattern="\d*" />
    

    The JS/AJAX

    function sendData(obj) {
                var xmlhttp;
    
                 alert (obj);
                if (window.XMLHttpRequest) {
                    xmlhttp=new XMLHttpRequest();
                } else {
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
    
                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
                        alert(obj);
                        // Do stuff when script returns
                    }
                }
    
    
                document.cookie="count="+ obj;
                alert (obj);
                var x = document.cookie;
                alert (x);
                $("#main").load("script.php");
    
            }
    

    The PHP

    if (isset($_COOKIE["count"])){
    
     $src1= $_COOKIE["count"];
    
       echo $src1;
    
     setcookie('count', '', time()-300); 
    } else { do whatever you want   }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器