Hi All
I have a simple add to shortlist function using php and would like to add a success Alert message so that users know that the item was added to the shortlist.

Ive tried the following code without success.

// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
    // redirect to product list and tell the user it was added to cart
    header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');
    echo '<script language="javascript">';
echo 'alert("item alredy in shortlist")';
echo '</script>';


}

// else, add the item to the array
else{
    $_SESSION['cart_items'][$id]=$horsename;

    // redirect to product list and tell the user it was added to cart
   header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');
   echo '<script language="javascript">';
echo 'alert("item succesfully added")';
echo '</script>';


}

I have also tried it this way

// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
    // redirect to product list and tell the user it was added to cart

    echo '<script language="javascript">';
    echo 'alert("item alredy in shortlist")';
     echo '</script>';
     header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');



}

// else, add the item to the array
else{
    $_SESSION['cart_items'][$id]=$horsename;

    // redirect to product list and tell the user it was added to cart

   echo '<script language="javascript">';
echo 'alert("item succesfully added")';
echo '</script>';
header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');



}

when I do it this way I can succesfully generate the elert message but then get the error message
"Warning: Cannot modify header information - headers already sent by (output started at /home/blahblahblah.php on line 24"

Can someone please correct the errors of my ways ????
Thank you

Recommended Answers

All 3 Replies

Warning: Cannot modify header information - headers already sent by (output started at /home/blahblahblah.php on line 24"

try adding

exit;

after the header location..

Another alternative:

if you want to redirect after the alert, then redirect can be wrap with <script> </script> as in

echo "<script type=\"text/javascript\">alert('item succesfully added');
        window.location='your_page.php';
    </script>";

that's all I think of right now. PHP will not know if javascript already alerted the user.

To redirect using the PHP redirect function a simple javascript function is needed to call a PHP function,

Thanks but I worked it out (with some help from the nice people at The Code Of A Ninja). This is what I did. I Changed

 // check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
    // redirect to product list and tell the user it was added to cart
    header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');
    echo '<script language="javascript">';
echo 'alert("item alredy in shortlist")';
echo '</script>';
}
// else, add the item to the array
else{
    $_SESSION['cart_items'][$id]=$horsename;
    // redirect to product list and tell the user it was added to cart
   header('Location: ../sale_barn_yearlings_test2.php#'.$id.'');
   echo '<script language="javascript">';
echo 'alert("item succesfully added")';
echo '</script>';

to

// check if the item is in the array, if it is, do not add
if(array_key_exists($id, $_SESSION['cart_items'])){
    // redirect to product list and tell the user it was added to cart
    header('Location: ../sale_barn_yearlings_test2.php?action=exists&#'.$id.'');



}

// else, add the item to the array
else{
    $_SESSION['cart_items'][$id]=$horsename;

    // redirect to product list and tell the user it was added to cart
   header('Location: ../sale_barn_yearlings_test2.php?action=added&#'.$id.'');

Then on the Products page (which is the page that the header redirects to) I added.

if($action=='added'){
    echo '<script language="javascript">';
    echo 'alert("Item Added")';
        echo '</script>';

}

if($action=='exists'){
 echo '<script language="javascript">';
    echo 'alert("Item Already there")';
        echo '</script>';

}

Works a treat

The main error was in the fact that you should not send any output to the browser before the header function. In your second alternative coding example above you broke this requirement. In the first example you actualy made a redirection before outputting the script code so the script code never run I suppose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.