if(isset $ _GET [“ change”]))不起作用,什么也没发生

问题描述 投票:0回答:1

如果我的表单已提交,我正在尝试回声。但是,什么也没有发生,而且我也没有任何错误。基本上,如果提交了第二个表单(仅在提交第一个表单时才显示),我想回显一条消息。这是行不通的。

任何帮助都会很棒。

update.php

<html>
<head>
</head>
<body>

    <h2>Update Information</h2>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="GET">

    Person Number
    <input type="number" name="number">
    Last Name
    <input type="text" name="lastname">
    <button type="submit" name="submit">Search</button>

    </form>

    <?php
    $xmlDocument=new DOMDocument;
    $xmlDocument->preserveWhiteSpace=false;
    $xmlDocument->load("person.xml");
    $personXPath = new DOMXPath($xmlDocument);

    if (isset($_GET["submit"])) {
        $query = [];
    if (!empty($_GET['number'])) {
        $number = $_GET["number"];
        $query[] = "@number='{$number}'";
    }
    if (!empty($_GET['lastname'])) {
        $lastN = $_GET['lastname'];
        $query[] = "./personL/fullname/lastname[contains(text(), '{$lastN}')]";
    }
    $query = '//personN[' . implode(' or ', $query) . ']';

    $persons = $personXPath->query($query);
    if ($persons->length > 0) {
        foreach ($persons as $person) {
            $firstname = $personXPath->evaluate('string(./personL/fullname/firstname/text())', $person);
            $lastname = $personXPath->evaluate('string(./personL/fullname/lastname/text())', $person);
            $eyecolor = $personXPath->evaluate('string(./personL/appearance/eye/text())', $person);
    }?>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="GET">
    <h2>Edit Information</h2>
    First Name:
    <input type="text" name="efirstname" value="<?php echo $firstname ?>">
    Last Name
    <input type="text" name="elastname" value="<?php echo $lastname ?>">   
    Eye Color
    <input type="text" name="eEyecolor" value="<?php echo $eyecolor ?>">
    <button type="submit" name="change">Save</button>

    </form> 
    <?php } 
    if (isset($_GET["change"])) {

    echo "Yes";

    } } ?>

</body>

</html>
php xml forms dom get
1个回答
0
投票

几个错误:

  1. $_GET['submit']在提交第二份表格时未设置。
  2. [$query在提交第二种表单时将一无所获,因为第一种形式的字段不再提交给第二种表单。
  3. foreach循环仅循环分配,但不循环更新形式。

所以我做了几处更改:

  1. 在第二个表单上添加隐藏字段以包括第一个表单的字段和一个“ submit”值。
  2. 将第二种形式移到foreach循环中。

我还重做了缩进,以便于阅读。

<html>
<head>
</head>
<body>

    <h2>Update Information</h2>

    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="GET">
      Person Number
      <input type="number" name="number" value="<?php echo $_GET['number'] ?? ''; ?>">
      Last Name
      <input type="text" name="lastname" value="<?php echo $_GET['lastname'] ?? ''; ?>">
      <button type="submit" name="submit">Search</button>
    </form>

    <?php
    $xmlDocument=new DOMDocument;
    $xmlDocument->preserveWhiteSpace=false;
    $xmlDocument->load("person.xml");
    $personXPath = new DOMXPath($xmlDocument);

    if (isset($_GET["submit"])) {
        $query = [];

        if (!empty($_GET['number'])) {
            $number = $_GET["number"];
            $query[] = "@number='{$number}'";
        }
        if (!empty($_GET['lastname'])) {
            $lastN = $_GET['lastname'];
            $query[] = "./personL/fullname/lastname[contains(text(), '{$lastN}')]";
        }
        $query = '//personN[' . implode(' or ', $query) . ']';

        $persons = $personXPath->query($query);
        ?>

        <?php if ($persons->length > 0) { ?>
            <?php foreach ($persons as $person) { ?>
                <?php
                    $firstname = $personXPath->evaluate('string(./personL/fullname/firstname/text())', $person);
                    $lastname = $personXPath->evaluate('string(./personL/fullname/lastname/text())', $person);
                    $eyecolor = $personXPath->evaluate('string(./personL/appearance/eye/text())', $person);
                ?>
                <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="GET">
                  <h2>Edit Information</h2>
                  First Name:
                  <input type="text" name="efirstname" value="<?php echo $firstname ?>">
                  Last Name
                  <input type="text" name="elastname" value="<?php echo $lastname ?>">
                  Eye Color
                  <input type="text" name="eEyecolor" value="<?php echo $eyecolor ?>">
                  <?php if (isset($number)) { ?>
                      <input type="hidden" name="number" value="<?php echo $number; ?>">
                  <?php } ?>
                  <?php if (isset($lastN)) { ?>
                      <input type="hidden" name="lastname" value="<?php echo $lastN; ?>">
                  <?php } ?>
                  <input type="hidden" name="submit" value="change">
                  <button type="submit" name="change">Save</button>
                </form>
            <?php } ?>
        <?php } ?>

        <?php if (isset($_GET["change"])) { ?>Yes<?php } ?>
    <?php } ?>

</body>

</html>

© www.soinside.com 2019 - 2024. All rights reserved.