Page 1 of 1

[SOLVED] Validate variable : Alert Message

Posted: Tue Jun 28, 2005 11:51 pm
by facets
Hi,

I'm pulling some fields from a DB and displaying them in a dropdown.
Then there is a button to view records. Currently if I select nothing from the Drop Down it popups a page with no contenets from the DB, just the empyt tables. How could I go about creating and error javascript popup?

Get dropdown code :

Code: Select all

function listCat() {
    $sql_query = mysql_query("SELECT paperCategory, paperCategoryId FROM aupapercategory");
    $output = "<select name=\"paperCategoryId\">\n";
    $output .= "<option value=\"\">-- Select Option --</option>\n"; 
        while(list($catname, $paperCategoryId)=mysql_fetch_array($sql_query)) {
        $catname = stripslashes($catname);
        $output .= "<option value=\"$paperCategoryId\">$catname</option>\n";
    }
    $output .= "</select>";
    mysql_free_result($sql_query);
return $output;
}
Button triggers this function :

Code: Select all

function view($stockId) {
echo "<script language=\"javascript\">var newWindow = window.open
('".$_SERVER['PHP_SELF']."?action=viewFaceStock&stockId=".$stockId."','','scrollbars=yes,status=yes,windowX=10,windowY=10,width=440,height=550')</script>";
}
Tia, Will./

Posted: Wed Jun 29, 2005 2:50 am
by facets
So I've added

Code: Select all

if (!$stockId) {
echo"alert('annoying alert box')>";
}
else {

etc,etc
but all I get in the popup window is
alert('annoying alert box')>

any ideas on how to execute this?
Or better still, any ideas on how to stop the popup window if $stockId is not set and displaying an alert instead.

Posted: Wed Jun 29, 2005 2:53 am
by facets
hmm, closer..

Code: Select all

function view($stockId) {
        if (!$stockId) {echo"alert('another annoying alert box')";
        }
        else
        {

echo "<script language=\"javascript\">var newWindow = window.open
('".$_SERVER['PHP_SELF']."?action=viewFaceStock&stockId=".$stockId."','','scrollbars=yes,status=yes,windowX=10,windowY=10,width=440,height=550')</script>";
}
}

Posted: Wed Jun 29, 2005 3:26 am
by facets
:)

fixed!

Code: Select all

function view($stockId) {

    if (!$stockId) { echo "<script language=\"javascript\">alert('Selection required')</script>"; }
    else {

echo "<script language=\"javascript\">var newWindow = window.open
('".$_SERVER['PHP_SELF']."?action=viewFaceStock&stockId=".$stockId."','','scrollbars=yes,status=yes,windowX=10,windowY=10,width=440,height=550')</script>";
}
}

Posted: Wed Jun 29, 2005 3:27 am
by s.dot
try escaping your php to javascript

Code: Select all

<? if($something = somethingelse)
{ ?>
<script type="text/javascript">
<!--
alert("alert");
//-->
</script>
<? } ?>