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?
Here's the form:
<table>
<form method="post" action="clippost.php" name="search">
<tr>
<td class="c开发者_如何学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:
<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:
<!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); 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. However when the autoSuggest plugin appends a hidden input to the table, the form will not pick this up.
The solution: write <form><table>..</table></form>
instead of <table><form>..</form></table>
This will get your data to the clippost.php script. 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). To use a stable field name you should invoke autoSuggest
with the asHtmlID
parameter.
In clippost.php
can use $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.
精彩评论