sudeep
sudeep

Reputation: 170

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

Upvotes: 1

Views: 1565

Answers (7)

elmasterlow
elmasterlow

Reputation: 66

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

Upvotes: 4

fj123x
fj123x

Reputation: 7482

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

        }

    ?>

Upvotes: 0

pintxo
pintxo

Reputation: 2155

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.

Upvotes: 0

JohnP
JohnP

Reputation: 50019

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.

Upvotes: 0

ITroubs
ITroubs

Reputation: 11215

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

Upvotes: 1

Naftali
Naftali

Reputation: 146302

There should be NO posted data

your code does not use any input fields!

Upvotes: 0

Chris White
Chris White

Reputation: 1063

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

Upvotes: 1

Related Questions