dojo1.8 - 不确定如何读取所选值并使用dojo / request发布到php文件

时间:2013-02-06 06:57:53

标签: php mysql ajax dojo

嗨,我有问题获取选择节点的值已发布,什么都没得到。 我正在使用dojo / request。当我使用简单的ajax文件进行测试时,服务器上的getuser1.php正在运行。

在请求行,数据的值可能不正确

请指教......谢谢

以下是两个脚本: - main.php文件 -

require(["dojo/parser", "dojo/ready","dijit/form/Select",
         "dojo/request","dojo/on","dojo/domReady!"],
function(parser, ready, Select, request, on){

    ready(function(){   

        console.debug('Rendering...');
        var selectX = new Select({
            name:'select_test',
            options:[
                {label:"<span class='NotinUse'><b>&nbsp&nbsp&nbsp. . .</b></span>", value:'0', selected:true},
                {label:"<span class='inUse'><b>&nbsp&nbspPeter Griffin</b></span>", value:'1'},
                {label:"<span class='inUse'><b>&nbsp&nbspLois Griffin</b></span>", value:'2'},
                {label:"<span class='inUse'><b>&nbsp&nbspJoseph Swanson</b></span>", value:'3'},
                {label:"<span class='inUse'><b>&nbsp&nbspGlenn Quagmire</b></span>", value:'4'},
            ], 
            style:{width:'150px'}
        },"select");

        on(formNode2, 'Change', function(evt){
            evt.stopPropagation();
            evt.preventDefault();

            request.post('getuser1.php',{   
                data:{select_test:this.value},
                timeout:2000
            }).then(function(response){
                dom.byId('line2').innerHTML=response;
            });
        });
    });
});

getuser1.php文件: -

<?php 
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);


$sql="SELECT * FROM user WHERE id = $q";

$result = mysql_query($sql);

// use stored function to return one result instead later. Write here after stored     procedure has been stored. 

echo "<table border='1'>
<tr>
<th width=100>Firstname</th>
<th width=100>Lastname</th>
<th width=100>Age</th>
<th width=100>Height</th>
<th width=100>Hometown</th>
<th width=100>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
 {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Height'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
    echo "</table>";

mysql_close($con);
?>

1 个答案:

答案 0 :(得分:0)

请记住在开发时使用浏览器的错误控制台,它通常会在您的代码中通知您简单的拼写错误等。

在这种情况下,有一些问题。您好像混淆了selectXformNode2。它可能应该是:

on(selectX, 'Change', function(evt){ 

现在,如果你让它工作,你会注意到当你改变selectX的值时,你的控制台会出错:

evt.stopPropagation is not a function

这是因为evt参数实际上不是这里的事件对象。从select元素中,您只能获得新值 not 该事件。把它改成这样的东西,你应该在控制台中看到我的意思:

on(selectX, 'Change', function( newValue ){
    console.debug('The new value is', newValue);
    //evt.stopPropagation();
    //evt.preventDefault();

我做了一些修改,让它在jsfiddle中运行,但它应该给你一个想法:http://fiddle.jshell.net/AmxKv/