1

我知道代码很长......但我希望我正在寻找的解决方案简单快捷。所以我有一个关于我的订单的会议。我有两种形式的回显,具体取决于该人是否已登录。首先,如果语句是用户是否已登录,它会执行所有这些操作(我知道代码并不完美,但它对我有用)。问题:下订单后如何将用户重定向到他们的帐户?我在 }else{... 不起作用之前尝试了 header 功能,直接将我带到了帐户页面。我在网上找不到代码,因为它们的表单都在 html 中,而不是在 php 中,所以标题不起作用。有什么建议???

<?php

    $memberOrder = "";
    $nonmemberOrder = "";

if(isset($_SESSION['id'])){

    include_once "connect_to_mysql.php";

    echo '<script type="text/javascript">

    function validate_form ( ) { 
    var numbers = /^[0-9]+$/;
    valid = true;

    if ( document.memberOrder.memberNumber.value == "" ) { 
    alert ( "Membership number must be entered." ); 
    valid = false;
    }
    if ( document.memberOrder.memberNumber.value.match(numbers)){
        valid = true;
    }else{
        alert ( "You must only have numbers entered as Membership Number.");
        valid = false;
    }
    if ( document.memberOrder.payment.value == "" ) { 
    alert ( "You must select a payment method." ); 
    valid = false;
    }
    if ( document.memberOrder.orderLinks.value == "" ) { 
    alert ( "You must insert a link to your order." ); 
    valid = false;
    }
    if ( document.memberOrder.size.value == "" ) { 
    alert ( "Size must not be blank." ); 
    valid = false;
    }
    if ( document.memberOrder.cost.value == "" ) { 
    alert ( "Cost must not be blank." ); 
    valid = false;
    }
    if ( document.memberOrder.cost.value.match(numbers)){
        valid = true;
    }else{
        alert ( "You must only have numbers entered as your item cost.");
        valid = false;
    }
    if (!document.memberOrder.checkbox.checked){
        alert ( "You must agree to terms before placing your order.");
        valid = false;
    }
    return valid;
    }
    </script>';

    $memberNumber = preg_replace("[^0-9]", "", $_POST['memberNumber']);
    $memberNumber = stripslashes($_POST['memberNumber']);
    $size = preg_replace("[^a-zA-Z]", "", $_POST['size']);
    $cost = preg_replace("[^0-9]", "", $_POST['cost']);
    $cost = stripslashes($_POST['cost']);
    $cost = strip_tags($cost);
    $color = preg_replace("[^a-zA-Z]", "", $_POST['color']);
    $color = stripslashes($_POST['color']);
    $color = strip_tags($color);
    $payment = $_POST['payment'];
    $orderLinks = $_POST['orderLinks'];

    $sql = mysql_query("INSERT INTO memberOrders (memberNumber, orderLinks, cost) VALUES 
                        ('$memberNumber', '$orderLinks', '$cost')") or die (mysql_error());

    $memberOrder = '<form action="order.php" method="post" name="memberOrder" id="memberOrder" onsubmit="return validate_form ( );">
                    <table cellpadding="10" width="500px">
                        <tr>
                            <td><div align="left">Membership Number</div></td>
                            <td><input type="text" name="memberNumber" id="memberNumber"  /></td>
                        </tr>
                        <tr>
                            <td><div align="left">Payment Type</div></td>
                            <td><input type="radio" name="payment" value="Bank Transfer" />Bank Transfer (recommended)<br/>
                                <input type="radio" name="payment" value="PayPal" />PayPal (additional 3% commission)<br/>
                                <input type="radio" name="payment" value="Western Union" />Western Union<br/>
                                <input type="radio" name="payment" value="MoneyGram" />MoneyGram<br/>
                            </td>
                        </tr>
                        <tr>
                            <td><div align="left">Link To Your Order</div></td>
                            <td><textarea rows="5" cols="20" name="orderLinks"  ></textarea></td>
                        </tr>
                        <tr>
                            <td><div align="left">Size</div></td>
                            <td><input style="width:50px" type="text" size="25px" name="size"  /></td>
                        </tr>
                        <tr>
                            <td><div align="left">Color</div></td>
                            <td><input type="text" size="25px" name="color"  /></td>
                        </tr>
                        <tr>
                            <td><div align="left">Cost of Your Item</div></td>
                            <td><input style="width:50px" type="text" name="cost"  /></td>
                        </tr>
                        <tr>
                            <td><div align="left">Order Agreement</div></td>
                            <td><input type="checkbox" name="checkbox" checked="checked" value="check" /><a href="#" id="agreement">View Order Agreement</a></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td><input type="submit" name="submit" value="Place Order!" /></td>
                        </tr>
                        </table>
</form>';
    echo $memberOrder;

    // Start assembly of Email Member the activation link
    $to = "yankeesmarket@gmail.com";
    // Change this to your site admin email
    $from = "$memberNumber";
    $subject = "Member Order Placed";
    //Begin HTML Email Message where you need to change the activation URL inside
    $message = '<html>
    <body bgcolor="#FFFFFF">
    Member Number:  '.$memberNumber.' <br/><br/>
    Payment Type:  '.$payment.' <br/><br/>
    Order Links:  '.$orderLinks.' <br/><br/>
    Size:  '.$size.' <br/><br/>
    Color: '.$color.' <br/><br/>
    Order Cost: '.$cost.' <br/><br/>
    </body>
    </html>';
    // end of message
    $headers = "From: $from\r\n";
    $headers .= "Content-type: text/html\r\n";
    $to = "$to";
    // Finally send the activation email to the member
    mail($to, $subject, $message, $headers);


}else{
4

3 回答 3

1

您不能向浏览器写入任何内容并期望该header功能正常工作。为了设置标头,响应正文必须为空。

所以我建议做类似的事情:

if(isset($_POST['submit'])){
    // do the INSERT and whatnot
    // when finished call the header function
    header("Location: my_custom_page.php");
}
else{
    // output the form, javascript and what you need
}

所以页面第一次加载时,它会进入else语句,因为你没有 POST 数据。这将打印出表单和验证 JavaScript。当表单被提交时,请求到达if,并下订单,然后客户端被重定向。

同样,在if声明中,您不得制作任何echos。对此的一个非常糟糕的替代方法是回显一个执行重定向的小型 javascript(我推荐这样做。永远):

<script>window.location.href = 'my_custom_page.php';</script>
于 2012-12-19T22:08:49.003 回答
0

以下是我将如何构建该过程:

  1. 验证用户是否已登录
  2. 验证电子邮件字段(存在、格式等)
  3. 将数据存储在 MySQL 中。如果插入成功,是否继续邮寄?
  4. 尝试邮寄信息。如果成功,则使用标头函数进行重定向。
  5. 如果不成功,请继续输出页面和任何错误消息。

请记住,即使一个字符已输出或呈现到页面,您也无法更改页眉。

此外,您应该选择使用另一种邮件传递方式,因为mail()邮件将保证进入垃圾文件夹,因为它使用发件人标头中的服务器信息。

于 2012-12-19T22:12:28.160 回答
-1

如果你想使用 JS,你可以随时在 html 中执行:

<script>
  window.location.href = <?php echo $url; ?>
</script>
于 2012-12-19T22:14:09.210 回答