显示返回上一页的确认(PHP HTTP REFERER)

时间:2013-04-06 04:47:30

标签: php http-referer

这可能很简单,但我很难搞清楚,

将index.php中包含数据的表单提交给sell.php,该表单由mysql查询处理,并在数据成功存储到数据库后自动返回到上一页(index.php)。

我使用的代码是:

header("Location: " .$_SERVER['HTTP_REFERER']);

我需要一点点改进。当页面sell.php返回index.php时,它应向用户发出确认消息,表明数据已成功提交。

的index.php

<form name="vender" method="post" action="sell.php">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
    <input type="hidden" name="serial" value="<?php echo $identity; ?>" />
    <input type="hidden" name="model" value="<?php echo $model; ?>" />
    <input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
    <table style="font-size: 8pt;">
        <tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
        <tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
        <tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
        <tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
        <tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
        <tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
        <tr><td><input type="submit" /></td></tr>
    </table>
</form>

sell.php

<?php

include "connect.php";
include "links.php";

$date = $_POST['date'];
$serial = $_POST['serial'];
$model = $_POST['model'];
$imei = $_POST['imei'];
$name = $_POST['name'];
$contact = $_POST['contact'];
$nif = $_POST['nif'];
$qty = $_POST['qty'];
$price = $_POST['price'];

mysql_query("INSERT INTO mobile_sell_data(date,serial,model,imei,name,contact,nif,qty,price) VALUES('$date','$serial','$model','$imei','$name','$contact','$nif','$qty','$price')");

mysql_query("UPDATE mobils SET qty=qty-'$qty' WHERE id = '$serial'");
header("Location: " .$_SERVER['HTTP_REFERER']);

?>

2 个答案:

答案 0 :(得分:0)

您是否考虑过使用get变量?

header("Location: " .$_SERVER['HTTP_REFERER'] . "?success=true");

或使用会话?

session_start()
$_SESSION['success'] = true
//reset the session on the return page

这些不是特别优雅的解决方案,但它们会起作用

答案 1 :(得分:0)

发送标头后,您无法回显任何内容,因为服务器在发送标头时已完成处理页面。您可以在此处实施几种解决方案。您可以使用GET变量,POST变量,SESSION甚至是cookie将数据发送回索引,或者您可以使用ajax从index.php中执行请求,以便您永远不会实际离开索引页。这是一个简单的解决方案:(注意,你需要删除你在sell.php中的重定向。一切都发生在index.php这种方式)

<?php
$successfulSubmit = FALSE;
if (!empty (@$_POST["sub"]))
{
    include "sell.php";
    $successfulSubmit = //some logic to verify data was successfully submitted
    if ($successfulSubmit)
    {
        echo "Data submitted successfully";
    }
    else
    {
        echo "Data submitted unsuccessfully";
    }
}
?>
<form name="vender" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<?php echo $identity; ?> | <?php echo $model; ?>
<hr />
<input type="hidden" name="serial" value="<?php echo $identity; ?>" />
<input type="hidden" name="model" value="<?php echo $model; ?>" />
<input type="hidden" name="date" value="<?php echo DATE('Y-m-d'); ?>" />
<table style="font-size: 8pt;">
    <tr><td>IEMI:</td><td><input class="form-sell" type="text" name="imei" /></td></tr>
    <tr><td>Nombre: </td><td><input class="form-sell" type="text" name="name" /></td></tr>
    <tr><td>Contacto: </td><td><input class="form-sell" type="text" name="contact" /></td></tr>
    <tr><td>NIF: </td><td><input class="form-sell" type="text" name="nif" /></td></tr>
    <tr><td>Cantidad: </td><td><input class="form-sell" type="text" name="qty" /></td></tr>
    <tr><td>Precio: </td><td><input class="form-sell" type="text" name="price" /></td></tr>
    <input type="hidden" name="sub" value="submitted" />
<tr><td><input type="submit" /></td></tr>
</table>
</form>