0

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

将index.php中的数据提交给sell.php,由mysql查询处理,数据入库成功后自动返回上一页(index.php)。

我正在使用的代码是:

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

我需要在这里进行一些改进。当sell.php页面返回index.php时,它会向用户发送一条数据提交成功的确认信息。

索引.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>

出售.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']);

?>
4

2 回答 2

0

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

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

还是使用会话?

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

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

于 2013-04-06T04:54:26.877 回答
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>
于 2013-04-06T05:17:30.327 回答