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   }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?