[SOLVED] Validate variable : Alert Message

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

[SOLVED] Validate variable : Alert Message

Post 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./
Last edited by facets on Wed Jun 29, 2005 3:26 am, edited 2 times in total.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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.
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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>";
}
}
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Post 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>";
}
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

try escaping your php to javascript

Code: Select all

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