1

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://levycarneiro.com/projects/tag-it/js/tag-it.js I'll be thankful for any help.

6
  • lol do u have any other helpful code? Commented Apr 4, 2011 at 15:41
  • @Neal, I posted the direct link of javascript code of the plugin. Commented Apr 4, 2011 at 15:43
  • yes. of the plugin, but not of what you, yourself did. Commented Apr 4, 2011 at 15:45
  • @Neal, I just want to receive the tag input by submitting the form by the code i wrote above but it does not receive the posted data. Commented Apr 4, 2011 at 15:47
  • ther is no posted data, you ddnt use input fields Commented Apr 4, 2011 at 15:48

7 Answers 7

4

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

Sign up to request clarification or add additional context in comments.

2 Comments

fieldname: "tag[] does the job. The response from print_r($_POST) will be: array(1) { ["tags"]=> array(3) { [0]=> string(4) "tag1" [1]=> string(4) "tag2" [2]=> string(4) "tag3" } }
nailed it. this was they key to add, without it, i wasn't getting anything: >>> fieldName: "tag[]", thanks man! :)
1

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'])) {

Comments

1

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);
?>

Comments

0

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.

3 Comments

if i read it right then he has to look for $_REQUEST['item']['tags'] as opposed to $_REQUEST['tags'] as you advised
Should it be like $tags = $_POST['tags']; ? because even this doesnt get the value.
it should be like $tags = $_POST['item']['tags']
0

There should be NO posted data

your code does not use any input fields!

2 Comments

Looking at the plugin, it creates a bunch of hidden inputs on the fly. OP has only posted the starting HTML
@JohnP , yes, but that plugin also does not use a submit button
0

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.

Comments

0

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

        }

    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.