weixin_33701564 2016-08-08 08:03 采纳率: 0%
浏览 9

$ _GET JavaScript和AJAX…

I am relatively new to PHP and I am (trying to) developing my first AJAX code...

I am trying to pass an attribute from a select with several options, each option with its very own unique attribute "values" that is a String.

The PHP file GETs the value and does something with it (for instance prints it out). And the generated file is then returned to my main HTML page.

Here is the source code...

Here is my javaScript:

function showUser(myElement) {

    var str = myElement.options[myElement.selectedIndex].getAttribute("values");

    if (str == "") {
        document.getElementById("myResponse").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("myResponse").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php?q="+encodeURIComponent(str),true);
        xmlhttp.send();
    }
}

My HTML is as following:

<form>
<select name="users" onchange="showUser(this)">
  <option values="">Select a person:</option>
  <option values="Rasmus Lerdorf">Creator of PHP</option>
  <option values="Linus Torvalds">Developed Linux</option>
  <option values="Dennis Ritchie">Developper of C</option>
  </select>
</form>
<br>
<div id="myResponse"><b>Person info will be listed here...</b></div>

My PHP is:

<?php
$q = intval($_GET['q']);
echo $q;
?>

I get 0 instead of the text string, what is wrong with my code?

  • 写回答

1条回答 默认 最新

  • weixin_33725126 2016-08-08 08:03
    关注

    Silly me,

    I copied and pasted a basic example from a tutorial and I did not realize that the PHP code was casting the string that was sent over the GET to an integer.

    $q = intval($_GET['q']);

    The code should be :

    $q = $_GET['q'];

    On my earlier stage of the development I was indeed passing an integer, so the code was working.

    The code returned 0 instead of an error because the intval of a string is 0 on failure.

    Writing down the question here I realized the mistake...

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。