drt96856 2017-03-01 13:58
浏览 21
已采纳

PHP按钮单击 - 值/ id传递

Hey I'm currently having a problem with trying to make my input button have a value of the id / server that I need and it showing a different value this is how my code currently looks I know the HTML and forms are invalid I will be refactoring it however I'm trying to get a server identifier and a id identifier to cross to another page

<?php
$propertyType = $xmlDom1->getElementsByTagName('PropertyType');
$rent = $xmlDom1->getElementsByTagName('rates');
$rooms = $xmlDom1->getElementsByTagName('rooms');
//$server = $xmlDom1->getElementsByTagName('server');
$propertyServer = $xmlDom1->getElementsByTagName('Property');
$propertyID = $xmlDom1->getElementsByTagName('Property');
$imageURL = $xmlDom1->getElementsByTagName('url');
$imageAlt = $xmlDom1->getElementsByTagName('altText');
$server = $propertyID->item($i)->getAttribute('server');
echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
$records = $xmlDom1->documentElement->childNodes;
for ($i = 0; $i < $records->length; $i++) {
    echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
    echo "<td>".$rent->item($i)->nodeValue."</td>";
    echo "<td>".$rooms->item($i)->nodeValue."</td>";
    echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>"; 
    echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
?> 
    <td>
        <button class="submit" type="submit" value="<?php echo $propertyID->item($i)->getAttribute('pid');  ?>" name="submit22">something </button>
    </td>
 </tr>
<?php 
} 
?>
</form>
  • 写回答

1条回答 默认 最新

  • dtrn74832 2017-03-01 14:18
    关注

    If you are trying to generate lots of form each with just a button then you need to put the form in a table cell like this

    It might also be useful to place the data items you want to pass in hidden fields rather than try and put 2 data items in the button value

    <?php
    $propertyType = $xmlDom1->getElementsByTagName('PropertyType');
    $rent = $xmlDom1->getElementsByTagName('rates');
    $rooms = $xmlDom1->getElementsByTagName('rooms');
    //$server = $xmlDom1->getElementsByTagName('server');
    $propertyServer = $xmlDom1->getElementsByTagName('Property');
    $propertyID = $xmlDom1->getElementsByTagName('Property');
    $imageURL = $xmlDom1->getElementsByTagName('url');
    $imageAlt = $xmlDom1->getElementsByTagName('altText');
    $server = $propertyID->item($i)->getAttribute('server');
    echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
    $records = $xmlDom1->documentElement->childNodes;
    for ($i = 0; $i < $records->length; $i++) {
        echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
        echo "<td>".$rent->item($i)->nodeValue."</td>";
        echo "<td>".$rooms->item($i)->nodeValue."</td>";
        echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>"; 
        echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
    
        $pid = $propertyID->item($i)->getAttribute('pid'); 
    ?> 
        <td>
            <form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">
                <input type="hidden" name="pid" value="<?php echo $pid;?>">
                <input type="hidden" name="server" value="<?php echo $server;?>">
                <button class="submit" type="submit" value="submit" name="submit22">something</button>
            </form>
        </td>
        </tr>
    <?php 
    } 
    ?>
    

    If you just want the button to carry all the data and have only one form you will have to package the 2 data items into one string and sent it as the value of the button

    <?php
    $propertyType = $xmlDom1->getElementsByTagName('PropertyType');
    $rent = $xmlDom1->getElementsByTagName('rates');
    $rooms = $xmlDom1->getElementsByTagName('rooms');
    //$server = $xmlDom1->getElementsByTagName('server');
    $propertyServer = $xmlDom1->getElementsByTagName('Property');
    $propertyID = $xmlDom1->getElementsByTagName('Property');
    $imageURL = $xmlDom1->getElementsByTagName('url');
    $imageAlt = $xmlDom1->getElementsByTagName('altText');
    $server = $propertyID->item($i)->getAttribute('server');
    
    echo '<form action="level5Details.php" method="get" enctype="application/x-www-form-urlencoded">';
    
    echo '<table><th>Type:</th><th>Rent:</th><th>Rooms:</th><th>Server</th>';
    $records = $xmlDom1->documentElement->childNodes;
    
    for ($i = 0; $i < $records->length; $i++) {
        echo "<tr><td>".$propertyType->item($i)->nodeValue."</td>";
        echo "<td>".$rent->item($i)->nodeValue."</td>";
        echo "<td>".$rooms->item($i)->nodeValue."</td>";
        echo "<td>".$propertyServer->item($i)->getAttribute('Server')."</td>"; 
        echo '<td><img src="data:image/jpeg;base64,'.$imageURL->item($i)->nodeValue.'" alt="'.$imageAlt->item($i)->nodeValue.'"></img></td>';
    
        $dataPackage = $propertyID->item($i)->getAttribute('pid') . ':' . $server; 
    ?> 
        <td>
            <button class="submit" type="submit" value="<?php echo $dataPackage;?>" name="submit22">something</button>
    
        </td>
        </tr>
    <?php 
    } 
    </table>
    </form>
    ?>
    

    Now in the receiving form unpack the data for use

    <?php
    // usual checks for things existing
    list($pid, $server) = explode(':', $_GET['submit22'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀