首页 > 解决方案 > PHP 表单打开新窗口

问题描述

我有一个我正在构建的 PHP 网站,我正在添加一个基本的产品订单表格。我已经创建了表单元素,一旦用户选择了一个选项,我想打开一个新窗口(而不是选项卡),让该新窗口作为焦点出现,并通过 GET 方法将变量传递到新打开的窗口。

目前,当我提交表单时,目标表单会按 js 打开,但是 url 变量不会传递到新窗口。我的代码同时以某种方式打开了一个新选项卡并将 GET url 传输到新选项卡而不是新窗口,从而导致一个具有正确 url 的新选项卡和一个没有正确 url 的新窗口。

我很感激有很多类似的问题,我花了几个小时试图解决无济于事。

在此先感谢您的时间

源表单 HTML:

        <div class="prdContainer">
            <form method="GET" target="_blank" action="members_order.php" onsubmit="openMemberOrder()"  id='bcpot002' class="prdInput">
                <div class="prodDiv">
                    <button type="submit" id="catOrder" class="catOrder"><img id="prdImgThumb" name="bcpot002" src="../../../Images/Product_Catalogue/bcpot002/bcpot002_thumb.jpg" ></button>
                </div>
                <div class="formDiv">
                    <label for="cat_ID" value="">Product Name:</label>
                    <input type="text" id="catID" name="cat_ID" value="bcpot002" readonly><br>
                    <label for="cat_Desc">Product Description:</label>
                    <input type="text" id="catDesc" name="cat_Desc" value="Red_Pot" readonly><br>
                    <label for="cat_val">Per unit price $:</label>
                            <input type="number" id="catVal" name="cat_val" value="" readonly>
                    <input type="hidden" id="divHeight" name="DivHeight" value="1036" readonly>
                    <input type="hidden" id="divWidth" name="DivWidth" value="1543" readonly>
                </div>
            </form>
        </div>

源表单JS:

function openMemberOrder(url) {
    newwindow = window.open('members_order.php', 'members_order', 'width=1600px,height=2000px,toolbar=no,scrollbars=no,resizable=no');
    if (window.focus) {newwindow.focus()}

      if (!newwindow.closed) {newwindow.focus()}
    return false;
}

接收页面 HTML:

<?php require_once('xxxxx/initialize.php');

        $catID = $_GET['cat_ID'];
        $catDesc = $_GET['cat_Desc'];
        $DivHeight = $_GET['DivHeight'];
        $DivWidth = $_GET['DivWidth'];

        echo "ID: " . $catID . "<b>";
        echo "Desc: " . $catDesc . "<b>";
        echo "Val: " . $catVal . "<b>";
        echo "H: " . $DivHeight . "<b>";
        echo "W: " . $DivWidth . "<b>";

?>

<body>
    <header id="memOrderHead" ></header>
    <div class="gridContainer" id="gridContainer" ></div>
        <article class="article" >
            <div class="orderDiv">
                <h3>Order Item</h3>
                <form class="orderInput">
                      <label for="cat_ID">Product name:</label>
                                <input type="text" id="catID" name="cat_ID" value="<?php echo $catID; ?>" readonly>
                                <label for="cat_Desc">Product description:</label>
                                <input type="text" id="catDesc" name="cat_Desc" value="<?php echo $catDesc; ?>" readonly>
                      <label for="cat_val">Per unit price $:</label>
                                <input type="number" id="catVal" name="cat_val" value="<?php echo $catVal; ?>" readonly>
                      <label for="cat_quant">Quantity ordered:</label>
                                <input type="number" id="orderQuant" name="cat_quant" min="0" placeholder="0">
                      <label for="cat_Total">Total Price $:</label>
                                <input type="number" id="orderTotal" name="cat_Total" min="0" placeholder="0" readonly>
                      <button type="button" class="orderButton" onclick="clearForm()">Clear</button>
                      <button type="button" class="orderButton" onclick="total()">Calculate Total</button>
                      <button type="button" class="orderButton" onclick="orderConfirm()" id="confirmReset">Submit</button>
                </form>
            </div>
        </article>

标签: javascriptphphtmlforms

解决方案


您可以使用 HTML 5 本地存储将数据从网页传递到新窗口。JavaScript下面的帖子中提供了其他一些替代方案 - 如何打开新页面并将 JSON 数据传递给它?


推荐阅读