PHP和JavaScript警报

时间:2015-12-18 13:04:23

标签: javascript php

我面临着让我疯狂的问题,我写了一个包含javascript警报的PHP脚本。项目的插入表现完美,但警告信息不会出现:(

这是我的代码

 <?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 个答案:

答案 0 :(得分:2)

使用此行显示警告:

echo "<script> alert('$value');</script>";

如果您需要将变量放入提醒,则必须在外部使用双引号",在内部使用单引号'

<强>更新

或者像@symcbean提到的那样,如果你想让它完全完美,你可以做类似的事情:

echo "<script> alert('" . addslashes($value) . "');</script>";