Question

I just have php function that I would like to return alert messages using JS alert box.

When I just do the first if condition the alert works fine. But once I add the others the alert box won't work (==2 and 3).

Any ideas what I am doing wrong?

Thank you!

$(document).ready( function() {

if (<?php echo error_for('fileTmpLoc') ?>==1)
{
alert("Error: No image was selected.");
}
if (<?php echo error_for('fileTmpLoc') ?>==2)
{
alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");
}
if (<?php echo error_for('fileTmpLoc') ?>==3)
{
alert("Error: Your file is too small. It was smaller than 200 KB in size.");
}

});

Was it helpful?

Solution

You could also handle the error conditional checking and JS alert output via PHP, eg:

$(document).ready( function() {
<?php 
    switch(error_for('fileTmpLoc')) {
        case 1 : echo 'alert("Error: No image was selected.");';
                 break;
        case 2 : echo 'alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");';
                 break;
        case 3 : echo 'alert("Error: Your file is too small. It was smaller than 200 KB in size.");';
                 break;
    }
?>
});

OTHER TIPS

I haven't test this, but did you tried using if else statement?

$(document).ready( function() {

if (<?php echo error_for('fileTmpLoc') ?>==1)
{
   alert("Error: No image was selected.");
}
else if (<?php echo error_for('fileTmpLoc') ?>==2)
{
   alert("Error: Your file was too large. It was larger than 3.5 Megabytes in size.");
}
else if (<?php echo error_for('fileTmpLoc') ?>==3)
{
   alert("Error: Your file is too small. It was smaller than 200 KB in size.");
}

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top