1

好的,所以我的问题是:

我有一个表格,要求输入名字、姓氏和城市。用户单击提交,然后信息显示在此表中:

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

因此,当用户单击编辑按钮时,会显示一个文本框和一个提交按钮,并且根据它是名字编辑还是姓氏编辑,我有适当的隐藏值与表单一起发送。唯一的问题是,无论如何,即使您单击名字字段的“提交”按钮,隐藏的输入“val”也会设置为“lname”。

这是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();
                        }
                    });

                }();

这是单击提交按钮并将“操作”更改为“更新”时的 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;
        }

我测试了 $val 是什么,无论如何它似乎都停留在“lname”上。所以,我认为我的 jQuery 代码有问题,或者问题是无论如何都会发送姓氏“val”值,这不应该是因为我在单击名字按钮。

这都是实验性的,可以帮助我学习 OOP。显然,这根本不是生产代码。我目前是应用科学副学士学位的一年级学生,并且还有很多年才能获得计算机科学学士学位。这甚至不是课程的一部分……我只是很无聊,真的很喜欢编程。

所以,觉得有人可以帮忙吗?抱歉发了这么长的帖子!

4

2 回答 2

2

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. Even when you are giving them different IDs their names are still the same. And nowhere in your jquery code are you changing the value of val that is why it always remains lname.

You have to change that value depending upon your logic somewhere. You can use following jQuery selector

$("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");
于 2013-01-25T05:42:19.480 回答
1

You have two different fields with name="val" in the same form. 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.

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"; 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.

于 2013-01-25T05:41:04.470 回答