简体   繁体   English

如何分隔jQuery自动建议值?

[英]How can I separate jQuery autosuggest values?

Hey guys, I'm using jQuery's autosuggest function and my question is, when the user selects multiple choices from the suggested values, how can I separate those values in php so I can insert them in a table or something? 大家好,我使用的是jQuery的autosuggest函数,我的问题是,当用户从建议值中选择多个选项时,如何在php中分隔这些值,以便将它们插入表或其他内容中?

Here's the form: 形式如下:

<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>

And the jquery script: 和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>

And clippost.php: 和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>

It depends on what post (or possibly get) data your server is receiving from the form. 这取决于您的服务器从表单接收什么数据(或可能得到的数据)。 Use printr($_POST); 使用打印机($ _POST); to see the data, and if you are not able to extract it, post the output of printr here. 要查看数据,如果无法提取数据,请在此处发布打印机的输出。

-- -

I've tested your code, the problem was that your form was inside of your table. 我已经测试了您的代码,问题是您的表格位于表中。 That is, I believe, not allowed in html, but nevertheless works in major browser. 就是说,我相信html不允许,但是在主要的浏览器中仍然可以。 However when the autoSuggest plugin appends a hidden input to the table, the form will not pick this up. 但是,当autoSuggest插件将隐藏的输入追加到表中时,表单将不接受该输入。

The solution: write <form><table>..</table></form> instead of <table><form>..</form></table> 解决方案:编写<form><table>..</table></form>而不是<table><form>..</form></table>

This will get your data to the clippost.php script. 这会将您的数据保存到clippost.php脚本中。 However, as you will see, the data will be stored in a random field name every run (as_values_xx where x is a number). 但是,您将看到,每次运行时,数据将存储在一个随机字段名称中(as_values_xx,其中x是数字)。 To use a stable field name you should invoke autoSuggest with the asHtmlID parameter. 要使用稳定的字段名称,应使用asHtmlID参数调用autoSuggest

In clippost.php can use $yourarray = explode(',',$_POST['as_values_your_custom_id']); clippost.php可以使用$yourarray = explode(',',$_POST['as_values_your_custom_id']); to get the entries out of the field. 将条目移出字段。 It is comma separated and explode splits at comma's to form an array. 它是逗号分隔的,并在逗号处explode拆分以形成数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM