I am facing problem that driving me crazy, I wrote a PHP script that contains a javascript alert .the insertion of the item perform perfectly, but the alert message doesn't appear :(
here is my code
<?php
if ($city=="jeddah" && $catid=="2")
{
$conTL1->autocommit(false);
$error =array();
$q3= $conTL2->query("INSERT INTO productj(product_id,product_name,product_price,product_image,admin_id,
product_descriptioon,cat_id,quantity,location_id) Values('$pid','$pname','$pprice','$img','$adminid','$pdescription',
'$catid','$pquantity','$loc')");
$q4= $conTL1->query("INSERT INTO product(product_id,product_name,product_price,product_image,admin_id,
product_description,cat_id,quantity,location_id) Values('$pid','$pname','$pprice','$img','$adminid','$pdescription',
'$catid','$pquantity','$loc')");
if($q3==false || $q4==false)
{
array_push($error,'Error in adding the product to jeddah databases');
}
if(!empty($error))
{
foreach ($error as $key => $value)
{
echo '<script> alert($value);</script>';
}
$conTL2->rollback();
$conTL1->rollback();
}
else
{
$conTL2->commit();
$conTL1->commit();
echo '<script> alert("The item has been added successfully to jeddah database!");</script>';
}
}//end if jeddah
?>
1 Answer 1
Use this line to display alert instead:
echo "<script> alert('$value');</script>";
If you need to put variable into your alert, double quotes " have to be used outside and single ones ' inside.
UPDATE
or, like @symcbean has mentioned, if you want it to be completely perfect you can do something like:
echo "<script> alert('" . addslashes($value) . "');</script>";
answered Dec 18, 2015 at 13:06
pavlovich
1,94215 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
William Madede
I think Naija refers to the else part of the alert...done
pavlovich
@WilliamMadede you are right actually, but the first alert was still wrong. There is no more javascript problems except that line. But thanks
William Madede
ofcourse, im not taking anything away from you...thats actually a good point...done
symcbean
erm, should be print "<script> alert('" . addslashes($value) . "');</script>";
najla
Thank you so much pavlovich <3 I am going to try it
default