Receive form input data with PHP POST
I'm using the jQuery tag-it plugin, which basically has an input field. Everything works well, but I am unable to receive the input field value by submitting the form with PHP.
Here's the form part:
<form action="<?=$PHP_SELF?>" method="post">
<div class="line">
<label for="tags">Tags</label>
<ul id="mytags"></ul>
</div>
<input name="submit" value="Submit" type="submit" />
</form>
Here is PHP part:
<?
if ($_POST[submit]) {
$tags = $_POST[mytags];
echo $tags;
}
?>
The demo of the plugin is here: http://levycarneiro.com/projects/tag-it/example.html and the javascript code is here: http://levycarne开发者_JAVA百科iro.com/projects/tag-it/js/tag-it.js I'll be thankful for any help.
in tpl
<script type="text/javascript">
$(document).ready(function() {
$("#tags-input").tagit({
fieldName: "tag[]",
availableTags: ["c++", "java", "php", "javascript", "ruby", "python", "c"]
});
});
</script>
use fieldName: "tag[]" attribute, in backend print_r($_POST) and check what it will display
ul
is not a form element which would be submitted, it's a UI element. And you need to use quotes around your array indexes, like this:
if (isset($_POST['submit'])) {
the code should look like this:
<?
if ($_POST['submit']) {
$tags = $_POST['mytags'];
echo $tags;
}
?>
you forgot the enclosing '
if you forget that php treats the submit in $_POST[submit]
as a constant
EDIT:
try this:
<?
var_dump($_POST);
?>
The acutal tags are stored in this form field which is created for each tag:
function create_choice (value) {
// some stuff
el += "<input type=\"hidden\" style=\"display:none;\" value=\""+value+"\" name=\"item[tags][]\">\n";
// some other stuff
}
So you must look out not for 'mytags' but for $_POST['item']['tags'] in your PHP Code which will then give you an array of the tags.
There should be NO posted data
your code does not use any input fields!
Looking at your plugin, it seems to create hidden input fields on the fly as you add tags.
Assuming that part of the code is actually working, put the following into your PHP code.
<?php
var_dump($_POST); //this has to be in the page you POST to
?>
See if all your tags are showing up. If it is, then your JS works and your PHP is at fault. As user @ITroubs mentions, you should quote your array indices. See if that fixes it.
If no data is displayed, then your JS plugin is not working properly.
Using firebug, add a couple of tags and inspect inside the LI element of your list and see if any hidden INPUTS are being created.
Also check if there are any JS errors being reported.
Tested and solved:
<form action="<?=$PHP_SELF?>" method="post">
<div class="line">
<label for="tags">Tags</label>
<ul id="mytags" name="item[tags][]"></ul>
</div>
<input name="submit" value="Submit" type="submit" />
</form>
Here is PHP part:
<?
if ($_POST[submit]) {
$tags = $_POST["item"]["tags"];
foreach($tags as $i=>$v)
{
$tagsf .= $v;
if($i < (count($tags)-1))
$tagsf .= ",";
}
echo $tagsf;
//This shows the tags with ",". Example: dog,cat,bird,onion
}
?>
精彩评论