0

I want to know if my php variable does not exists, then I want to execute my javascript.

For example-

<?php
if(!isset($_REQUEST['myVar'])){
   ?>
   <script>
   alert('variable not exist');
   </script>
   <?php
}
?>

Is this right way to use javascript code in php extension file

14
  • i think this is right. Commented Jun 27, 2014 at 7:14
  • yes its right way... have u tryied this code ..? Commented Jun 27, 2014 at 7:14
  • But why are you doing this? Mixing the server-side and client-side code? O.o Commented Jun 27, 2014 at 7:15
  • acctuly i tried , i just want to kow is that fine for my script to use javascript inside my php file , is there any alternate ? and thank you both Commented Jun 27, 2014 at 7:16
  • 1
    @MajidGolshadi ,what does it make difference ? Commented Jun 27, 2014 at 7:35

3 Answers 3

1

I know all other answers solve your issue but i prefer it to do this way

<script>
    <?php 
        $isset = !isset($_POST['myVar']) ? 'true' : 'false';
        echo "var isset = $isset;";
    ?>
    if(isset) {
      alert('variable not exist');
    }
</script>

when php render your page it will give this output

<script>
    var isset = true;       
    if(isset) {
      alert('variable not exist');
    }
</script> 
Sign up to request clarification or add additional context in comments.

7 Comments

How can u access => isset variable WITHOUT $ in javascript?
downvoter i think nothing wrong in this snippet,this is more readable then other isn't it
@PratikJoshi isset is javascript variable while $isset is php variable,i think you didn't see my snippet properly
Parse error: syntax error, unexpected T_ECHO in /var/www/html/sample123.php on line 4. This 4th line gives error => echo "var isset = $isset;";
change that line to echo "var isset = ".$isset.";";
|
0

Do it like this and it will work:

if (!isset($_REQUEST['myVar'])) {

    echo "<script>alert('variable not exist');</script>";
}

6 Comments

is this a professional way to execute php and javascript code ? and thanks for reply
no this is not the professional way. its a simple hack. I you want a professional way you have the save a cookie through PHP and then you have to check through jquery if the cookie exist or not or the second solution given by Rakesh(below). The error you were making was you were closing echo inside your alert script alert(**'**variable not exist**'**);
thanks for explanation, so you mean , if i need to mix php with javascript than i have to use cookies over there ?
Sorry for the late response. Yes but this is the easiest way to do. You can use this method. There is nothing wrong with this method.
no problem , the way to explain is really appreciated, now can you please suggest me how can i learn about magic methods in php OOP.. thanks once again
|
0

you can try writing this piece of code where you want the script to be placed:

<?php
    if (!isset($_REQUEST['myVar'])) {
        echo '<script>
            alert("variable not exist");
            </script>';
    }
?>

4 Comments

You've crossed your single quotes in the echo
And how is this different than what OP has?
@Vld it actually is not different, but it's another way to do it as an alternative!
is this code is valid :- <?php if(!isset($myPages)){ ?> var arrayVar = {step:"part5", activationId:activationId}; alert('not exist'); <?php } else {?>alert('hello how are you'); <?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.