简体   繁体   English

使用PHP POST接收表单输入数据

[英]Receive form input data with PHP POST

I'm using the jQuery tag-it plugin, which basically has an input field. 我正在使用jQuery tag-it插件,该插件基本上具有一个输入字段。 Everything works well, but I am unable to receive the input field value by submitting the form with PHP. 一切正常,但是我无法通过使用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: 这是PHP部分:

<?
    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. 该插件的演示位于此处: http : //levycarneiro.com/projects/tag-it/example.html ,而javascript代码位于: http : //levycarneiro.com/projects/tag-it/js/tag- it.js我将非常感谢您的帮助。

in tpl 在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 在后端print_r($ _ POST)中使用fieldName:“ tag []”属性,并检查其显示内容

ul is not a form element which would be submitted, it's a UI element. ul不是要提交的表单元素,而是UI元素。 And you need to use quotes around your array indexes, like this: if (isset($_POST['submit'])) { 并且您需要在数组索引周围使用引号,例如: 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 如果您忘记了php将$_POST[submit]中的$_POST[submit]视为一个常量

EDIT: 编辑:

try this: 尝试这个:

<?
var_dump($_POST);
?>

There should be NO posted data 应该没有发布的数据

your code does not use any input fields! 您的代码不使用任何输入字段!

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. 因此,您必须注意PHP代码中的$ _POST ['item'] ['tags'] ,而不是'mytags',它会为您提供一系列标签。

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代码中。

<?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. 如果是这样,则您的JS可以正常工作,而您的PHP却有问题。 As user @ITroubs mentions, you should quote your array indices . 正如用户@ITroubs提到的那样,您应该引用数组索引 See if that fixes it. 看看是否能解决问题。

If no data is displayed, then your JS plugin is not working properly. 如果未显示任何数据,则说明您的JS插件无法正常工作。

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. 使用Firebug,添加几个标签,并检查列表的LI元素内部,看看是否正在创建任何隐藏的INPUTS。

Also check if there are any JS errors being reported. 同时检查是否报告了任何JS错误。

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: 这是PHP部分:

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

        }

    ?>

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

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