在php脚本中提交后重定向

时间:2012-12-19 22:03:18

标签: php javascript html header

我知道代码很长......但我希望我正在寻找的解决方案简单快捷。所以我在我的订单上有一个会议。我有两种形式,这取决于该人是否登录。首先,如果用户是登录的话,那就完成了所有这些(我知道代码不完美,但它对我有用)。问题: 下订单后如何将用户重定向到他们的帐户?我在} else {...没有用之前尝试了头功能,把我带到帐户页面。我无法在线找到代码,因为他们的表单都在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{

3 个答案:

答案 0 :(得分:2)

您无法向浏览器写任何内容并期望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声明中,您不能进行任何echo。一个非常糟糕的替代方案,就是回应一个重定向的小javascript(我推荐这个。永远):

<script>window.location.href = 'my_custom_page.php';</script>

答案 1 :(得分:0)

以下是我将如何构建流程:

  1. 验证用户是否已登录
  2. 验证电子邮件字段(在线状态,格式等)
  3. 将数据存储在MySQL中。如果插入成功,请继续发送邮件?
  4. 尝试邮寄信息。如果成功,请使用header功能进行重定向。
  5. 如果不成功,请继续输出页面和任何错误消息。
  6. 请记住,即使一个字符已输出或呈现给页面,也无法更改页眉。

    您应该选择使用其他邮件传递方式,因为mail()邮件将保证到垃圾邮件文件夹,因为它使用发件人标头中的服务器信息。 / p>

答案 2 :(得分:-1)

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

<script>
  window.location.href = <?php echo $url; ?>
</script>