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 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用