简体   繁体   English

单击提交按钮时隐藏隐藏的输入值(php / jquery)

[英]hide a hidden input value when a submit button is clicked (php/jquery)

Ok, so my problem is this: 好的,我的问题是这样的:

I have a form that asks for a First Name, Last Name, and a City. 我有一个要求姓名,姓氏和城市的表格。 The user clicks submit and then the information is displayed into this table: 用户单击“提交”,然后信息显示在此表中:

<p>You entered the following data:</p>

            <form action="1.php" method="post">

                <input type="hidden" name="action" value="update">
                <input type="hidden" name="city" value="<?php echo $user->get_city(); ?>">

                <table>
                    <tr>
                        <th>First Name:</th>
                        <td><?php echo $user->get_fname(); ?></td>
                        <td><input type="button" id="edit_fname" value="Edit"></td>
                        <td><input type="text" id="fname" name="fname" size="20"></td>
                        <td><input type="hidden" id="valf" name="val" value="fname">
                            <input type="hidden" id="lnf" name="lname" value="<?php echo $user->get_lname(); ?>">
                            <input type="submit" id="subfn" value="Submit"></td>
                    </tr>
                    <tr>
                        <th>Last Name:</th>
                        <td><?php echo $user->get_lname(); ?></td>
                        <td><input type="button" id="edit_lname" value="Edit"></td>
                        <td><input type="text" id="lname" name="lname" size="20"></td>
                        <td><input type="hidden" id="vall" name="val" value="lname">
                            <input type="hidden" id="fnf" name="fname" value="<?php echo $user->get_fname(); ?>">
                            <input type="submit" id="subln" value="Submit"></td>
                    </tr>
                    <tr>
                        <th align="right">City:</th>
                        <td><?php echo $user->get_city(); ?></td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
            </form>

So, when the user clicks the edit button a text box is displayed along with a submit button, and depending on if it's the first name edit or last name edit, I have the proper hidden values to send along with the form. 因此,当用户单击编辑按钮时,将显示一个文本框和一个提交按钮,并且根据它是第一个名称编辑还是姓氏编辑,我有正确的隐藏值与表单一起发送。 The only problem is that no matter what, the hidden input 'val' is set to 'lname' even when you click the Submit button for the first name field. 唯一的问题是,无论如何,即使单击第一个名称字段的“提交”按钮,隐藏的输入“val”也会设置为“lname”。

Here is the jquery: 这是jquery:

$().ready = function() {
                    $('#fname').hide();
                    $('#lname').hide();
                    $('#subfn').hide();     // submit button for first name
                    $('#subln').hide();     // submit button for last name
                    $('#valf').hide();      // hidden value for fname so we know to post lname
                    $('#vall').hide();      // hidden value for lname so we know to post fname
                    $('#lnf').hide();       // hidden value to post last name when first name edit submit
                    $('#fnf').hide();       // hidden value to post first name when last name edit submit

                    $("#edit_fname").click(function() {
                        $('#fname').show();
                        $('#subfn').show();
                        $('#valf').show();
                        $('#lnf').show();

                        if ($('#lname').show) {     // if last name edit is showing, hide it
                            $('#lname').hide();
                            $('#subln').hide();
                            $('#vall').hide();
                            $('#fnf').hide();

                        }
                    });

                    $("#edit_lname").click(function() {
                        $('#lname').show();
                        $('#subln').show();
                        $('#vall').show();
                        $('#fnf').show();

                        if ($('#fname').show()) {       // if first name edit is showing, hide it
                            $('#fname').hide();
                            $('#subfn').hide();
                            $('#valf').hide();
                            $('#lnf').hide();
                        }
                    });

                }();

And here is the PHP for when the Submit button is clicked and changes the 'action' to 'update': 这里是用于点击“提交”按钮并将“操作”更改为“更新”的PHP:

case 'update':
        $val = $_POST['val'];

        if ($val == 'fname') {
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $city = $_POST['city'];

            $user = new User($fname, $lname, $city);
            break;
        }
         else if ($val == 'lname') {
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $city = $_POST['city'];

            $user = new User($fname, $lname, $city);
            break;
        }

I tested to see what $val is and it seems to be stuck on 'lname' no matter what. 我测试看看$ val是什么,无论如何它似乎都被卡在'lname'上。 So, I think there is a problem with my jQuery code, or the problem is that the last name 'val' value is being sent no matter what, which it shouldn't be because I have it ID'd to be hidden when the first name button is clicked. 所以,我认为我的jQuery代码存在问题,或者问题是无论发生了什么,都应该发送姓氏'val'值,这不应该是因为我将ID隐藏起来单击名字按钮。

This is all experimental to help me learn OOP. 这是帮助我学习OOP的所有实验。 Obviously this is not production code at all. 显然这根本不是生产代码。 I am currently a 1st year student towards my Associates of Applied Science Degree and have many more years until I get the bachelors in computer science. 我目前是我的应用科学学士学位的一年级学生,还有很多年,直到我获得计算机科学学士学位。 This isn't even a part of a class...I'm just bored and really enjoy programming. 这甚至不是课程的一部分......我只是觉得无聊而且非常喜欢编程。

So, think anyone can help? 那么,想想有人可以帮忙吗? Sorry for such a long post! 对不起这么长的帖子!

Here is why. 这就是原因。 See the following two lines of code 请参阅以下两行代码

<td><input type="hidden" id="valf" name="val" value="fname">

..... .....

<td><input type="hidden" id="vall" name="val" value="lname">

Those define a hidden field named val first with value fname and then with value lname. 它们首先使用值fname定义一个名为val的隐藏字段,然后使用值lname。 Even when you are giving them different IDs their names are still the same. 即使你给他们不同的ID,他们的名字仍然是相同的。 And nowhere in your jquery code are you changing the value of val that is why it always remains lname . 你的jquery代码中没有任何地方你改变val的值,这就是为什么它始终保持lname

You have to change that value depending upon your logic somewhere. 你必须根据你的逻辑改变那个值。 You can use following jQuery selector 您可以使用以下jQuery选择器

$("input[type='hidden'][name='val']")

Edit 编辑

So instead of this 所以不要这样

$('#valf').show();

You can write 你可以写

$("input[type='hidden'][name='val']").val("fname");

And instead of this 而不是这个

$('#vall').show();

You can write 你可以写

$("input[type='hidden'][name='val']").val("lname");

You have two different fields with name="val" in the same form. 您有两个不同的字段,其name="val"在同一表单中。 PHP reads each of these in sequence and the second value clobbers (overwrites) the first one, leaving you with just the last value in the $_GET array element. PHP按顺序读取其中的每一个,第二个值blobbers(覆盖)第一个,只留下$_GET数组元素中的最后一个值。

You can submit multiple values under the same name by specifying the variable name with [] on the end of it. 您可以通过在其末尾指定带有[]的变量名称,以相同的名称提交多个值。 This tells PHP that it's an array element, just like you would use $array[] = "new element"; 这告诉PHP它是一个数组元素,就像你使用$array[] = "new element"; in your normal code to add an element to an array. 在您的常规代码中添加元素到数组。

Even using the array syntax wouldn't help you here though, since both values would be passed to the server every time you submitted the form. 即使使用数组语法也无法帮助您,因为每次提交表单时都会将这两个值传递给服务器。 If you only want one section or the other to be submitted, then they need to be separate forms. 如果您只想提交一个部分或另一个部分,那么它们需要是单独的表格。

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

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