简体   繁体   English

如何检索具有相同名称的多个输入的值?

[英]How to retrieve the value of multiple inputs with the same name?

I am displaying some information from an XML file, beside each I have buttons with allow user to delete the data. 我正在显示XML文件中的一些信息,每个信息旁边都有一个按钮,允许用户删除数据。

Now I am using a hidden input field to store some information needed to process the form. 现在,我使用一个隐藏的输入字段来存储处理表单所需的一些信息。 All of the hidden input fields have the sane name value. 所有隐藏的输入字段均具有合理的名称值。 When the user clicks the delete button I want to retrieve the value of the hidden filed that was clicked, but It returns the last one. 当用户单击删除按钮时,我要检索单击的隐藏文件的值,但是它返回最后一个。

here is my code: 这是我的代码:

function displayPapers(){
    var courseCode = $("#subject").val();

    loadXML(courseCode, function(xmlDoc){
        $(xmlDoc).find('paper').each(function(){
            var code = $(this).find("code")[0].textContent;
            var title = $(this).find("title")[0].textContent;
            var semester = $(this).find("semester")[0].textContent;

            $("#papers").append("<p>" + code + ": " + title + " (S" + semester + ") " +
                                "<input type='submit' name='paperUpdate' value='Edit'>" +
                                "<input type='submit' name='paperDelete' value='Delete'>" +
                                "<input type='hidden' name='paperCode' value='" + code + "'></p>");
        })
    });
}

Here is the PHP code used to process the form: 这是用于处理表单的PHP代码:

if(isset($_POST['paperUpdate'])){
   echo $_POST['paperCode'];
}

I want to retrieve the value of the hidden field for which the delete button was clicked. 我想检索单击了删除按钮的隐藏字段的值。 I am not sure how to fix this. 我不确定如何解决此问题。 thanks. 谢谢。

Use jQuery DOM traversal functions to find related elements. 使用jQuery DOM遍历函数查找相关元素。

$('input[name=paperDelete]').click(function() {
    var paperCode = $(this).next().val();
    // Do something with paperCode
});

To be able to get all the values in PHP, you should give it a name ending in [] . 为了能够获取PHP中的所有值,您应该给它一个以[]结尾的名称。 PHP will then turn all the values into an array. 然后,PHP将所有值转换为数组。 So change your code to: 因此,将您的代码更改为:

        $("#papers").append("<p>" + code + ": " + title + " (S" + semester + ") " +
                            "<input type='submit' name='paperUpdate' value='Edit'>" +
                            "<input type='submit' name='paperDelete' value='Delete'>" +
                            "<input type='hidden' name='paperCode[]' value='" + code + "'></p>");
    })
});

Then your PHP can do: 然后您的PHP可以执行以下操作:

foreach ($_POST['paperCode'] as $code) {
    echo $code;
}

Try- 尝试-

$("#papers").on("click","input[name='paperDelete']",function(){
  alert($(this).siblings("input[name='paperCode']").val())
})

DEMO DEMO

For multiple field with same name..Try something like this.. 对于具有相同名称的多个字段。尝试这样的方法。

<?php
if(isset($_POST['btn']))
{
    $name = $_POST['nam'];
    //var_dump($name);
    echo $name[0];
    echo "<br>";
    echo $name[1];
    echo "<br>";
    echo $name[2];
}

?>
<form method='post'>
FirstName:<input type='text' name='nam[]'>
MiddleName:<input type='text' name='nam[]'>
LastName:<input type='text' name='nam[]'>
<input type="submit" value="POST" name="btn">
</form>

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

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