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 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题