在javascript和php之间来回传递表单ID作为变量

时间:2011-05-19 22:38:01

标签: php javascript jquery

首先我要说的是,虽然我对PHP和HTML非常了解,但我对javascript / jquery并不了解。如果之前已经回答过,我也很抱歉,但我没有找到任何搜索内容的运气。

我正在开发一个项目,我们有一个未确定大小的形式,我想在其中构建一些自动完成功能。表格字段和必要的div正在使用计数器命名,如下面的代码所示。

$set_b = 'upl_band'.$count;
$sugbox = $set_b."sug";
$autobox = $set_b."auto";
echo "<div><input type=text name='$set_b' size=25 id='$set_b' onkeyup='bandlookup(this.value,'$set_b');' onblur='bandfill();'></div>";
echo "<div class='suggestionsBox' id='$sugbox' style='display: none;'><img src='upArrow.png' style='position: relative; top: -12px; left: 30px;' alt='upArrow' /><div class='suggestionList' id='$autobox'>&nbsp;</div></div>";

我试图将主要值 - $ set_b传递给我的javascript onkeyup。然而,在某条线上,我失去了我的价值观。如果我使用具体的ID来设置我的表单,这段代码可以正常工作,但是当我创建我的id变量时,我迷路了。我的javascript在下面。对band.php的调用是我的查找脚本。

function bandlookup(bandString, boxName) {
    if(bandString.length == 0) {
        // Hide the suggestion box.
        var s = boxName+"sug";
        $("#"+s).hide();
    } else {
        var su = boxName+"sug";
        var suauto = boxName+"auto";
        $.post("band.php", {queryString: ""+bandString+"", inputName: ""+boxName+""}, function(data){
            if(data.length >0) {
                $("#"+su).show();
                $("#"+suauto).html(data);
            }
        });
    }
} // lookup

function bandfill(thisValue, boxName) {
    var s = boxName+"sug";
    $("#"+boxName).val(thisValue);
    setTimeout("$('#'+s).hide();", 200);
}

和band.php

$db = new mysqli('localhost', 'yourUsername', 'yourPassword', 'yourDatabase');

if(!$db) {
    // Show error if we cannot connect.
    echo 'ERROR: Could not connect to the database.';
} else {
    // Is there a posted query string?
    if(isset($_POST['queryString'])) {
        $queryString = $db->real_escape_string($_POST['queryString']);
        $box = $_POST['inputName'];
        // Is the string length greater than 0?

        if(strlen($queryString) >0) {
                $query = $db->query("SELECT band_name,band_id FROM upl_band WHERE band_name LIKE '$queryString%' LIMIT 10");
                if($query) {
                    // While there are results loop through them - fetching an Object (i like PHP5 btw!).
                    while ($result = $query ->fetch_object()) {
                        // Format the results, im using <li> for the list, you can change it.
                        // The onClick function fills the textbox with the result.
                        echo '<li onClick="bandfill(\''.$result->band_name.'\',\''.$box.'\');">'.$result->band_name.'</li>';
                    }
                } else {
                    echo 'ERROR: There was a problem with the query.';
                }
        } else {
            // Dont do anything.
        } // There is a queryString.
    } else {
        echo 'There should be no direct access to this script!';
    }
}

我的问题可能在于javascript中的post调用,但我更倾向于我不正确地将变量名称作为id标记处理。

1 个答案:

答案 0 :(得分:0)

你的字符串坏了,试试这个:

echo "<div><input type=text name='$set_b' size=25 id='$set_b' onkeyup=\"bandlookup(this.value,'$set_b');\" onblur='bandfill();'></div>";