문제

This might be simple but I am having difficulty figuring it out,

A form with data in index.php is submitted to sell.php which is processed by mysql query and returns automatically to previous page (index.php) after data is stored in database successfully.

The code I am using is:

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

I needed a little enhancement here. When the page sell.php returns back to index.php, it shall give a confirmation message to user that the data was submitted successfully.

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

?>
도움이 되었습니까?

해결책

You can't echo anything out once the headers have been sent, since the server is finished dealing with the page when the headers are sent. There are a couple solutions that you can implement here. You could send data back to index using a GET variable, POST variable, SESSION, maybe even a cookie, or you can have the request performed from within the index.php using ajax so that you never actually leave the index page. Here's a simple solution: (Note, you need to remove your redirect in sell.php. Everything takes place in index.php this way)

<?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>

다른 팁

Have you considered using either a get variable?

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

Or using a session?

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

These aren't particularly elegant solutions, but they will work

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top