문제

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.");
}

});

도움이 되었습니까?

해결책

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;
    }
?>
});

다른 팁

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.");
}

});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top