如何分离jQuery autosuggest值?

时间:2011-02-06 17:06:46

标签: php javascript jquery forms

嘿伙计们,我正在使用jQuery的autosuggest函数,我的问题是,当用户从建议的值中选择多个选项时,如何在php中分隔这些值,以便将它们插入表格或其他内容?

以下是表格:

<table>
<form method="post" action="clippost.php" name="search">
<tr>
<td class="clip"><label><span><p class="signin">Clip Name:</p></span></label></td>
<td class="clip"><input type="text" name="clip" class="biginput" /></td>
</tr>
<tr>
<td class="clip"><label><span><p class="signin">Topic:</p></span></label></td>
<td class="clip"><input type="text" name="topic" class="biginput" /></td>
<td class="clip"><p class="info">Seperate by commas.</p></td>
</tr>
<tr>
<?php 
$mysql=mysql_connect('localhost','***','***');
    mysql_select_db('jmtdy');
    $result=mysql_query("select * from users where username='".$_SESSION['username']."'") or die(mysql_error());
    $dbarray=mysql_fetch_assoc($result);
    $result2=mysql_query("select * from friendship where userid='".$dbarray['id']."'");
    if(mysql_num_rows($result2)>0){

echo'
<td class="clip"><label><span><p class="signin">Clip-on people:</p></span></label></td>
<td class="clip"><input class="biginput" type="text" name="clippedon" id="suggestedfriend" /></td>
<td class="clip"><a class="neutral" href="faq.php#clipon"><p class="info">What is this?</p></a></td>
</tr>
';
}
else {
    echo'
    <td class="clip"><p class="signin">You need to have friends to clip-on people.</p></td>
    <td class="clip"><a class="neutral" href="faq.php#clipon"><p class="info">What is this?</p></a></td>
    ';
    }
    ?>
    <tr><td class="clip"><label><span><p class="signin">Editable:</p></span></label></td><td><select name="editable">

                                                            <option value="edit">Yes</option>
                                                            <option value="noedit">No</option>
                                                            </select></td></tr>
    <tr><td><input class="submitButton" type="submit" value="Create Clip" /> </td></tr>                                                     
</form>
</table>

和jquery脚本:

<script type="text/javascript">

$(function(){
    var data = {items: [
    <?php 

    $mysql=mysql_connect('localhost','***','***');
    mysql_select_db('jmtdy');
    $result=mysql_query("select * from users where username='".$_SESSION['username']."'") or die(mysql_error());
    $dbarray=mysql_fetch_assoc($result);
    $result2=mysql_query("select * from friendship where userid='".$dbarray['id']."'");
    while($dbarray2=mysql_fetch_assoc($result2)){
        $result3=mysql_query("select * from users where id='".$dbarray2['friendid']."'");
        $dbarray3=mysql_fetch_assoc($result3);


        echo '{value: "'.$dbarray3['id'].'", name: "'.$dbarray3['username'].'"},';
        }
        ?>
]};
$("#suggestedfriend").autoSuggest(data.items, {selectedItemProp: "name", searchObjProps: "name"});
});







</script>

和clippost.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" id="Cliproid" class=" no_js">
<head>
    <link href='graphics/icon.png' rel='icon' type='image/png'/>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-language" content="en" />
    <LINK REL=StyleSheet HREF="Mainstyles.css" TYPE="text/css"></link>
    <Title>Cliproid</title>
    </head>
    <body>
<?php 
$mysql=mysql_connect('localhost','*********','********');
    mysql_select_db('jmtdy');
$clip=mysql_real_escape_string($_POST['clip']); 
$topic=mysql_real_real_escape_string($_POST['topic']);

$editable=mysql_real_escape_string($_POST['editable']);
print_r($_POST['clippedon']);
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这取决于您的服务器从表单接收的帖子(或可能获得的)数据。使用printr($ _ POST);要查看数据,如果您无法提取数据,请在此处发布printr的输出。

-

我测试了你的代码,问题是你的表单在你的表内。也就是说,我相信,html中不允许这样做,但仍然适用于主流浏览器。但是,当autoSuggest插件向表中附加一个隐藏的输入时,表单将不会选择它。

解决方案:写<form><table>..</table></form>而不是<table><form>..</form></table>

这会将您的数据发送到clippost.php脚本。但是,正如您将看到的,每次运行时数据都将存储在随机字段名称中(as_values_xx,其中x是数字)。要使用稳定的字段名称,您应使用autoSuggest参数调用asHtmlID

clippost.php中,$yourarray = explode(',',$_POST['as_values_your_custom_id']);可以使用explode来输入字段。它以逗号分隔,{{1}}以逗号分隔以形成数组。