if condition with Javascript inside PHP

Asked

Viewed 1,873 times

1

It is possible to check if the variable is null in the JS section in PHP?

<?php
print("<SCRIPT language=javascript> 

            string_patr = \"$string_patr\";
            string_seri = \"$string_seri\";
            if(string_patr == null){
                vv_patr = string_seri;
            }
            else{
                vv_patr = string_patr+','+ string_seri;
            }
            alert(vv_patr);


        </SCRIPT>");


?>
  • 1

    what’s the mistake??

  • The mistake is that the if(string_patr == null) does not recognize whether it is null or not! And it’s all in php'

  • 1

    @Alexandre, prefer [Dit] the question to add information/clarify doubts.

3 answers

3


Check on the PHP side

Based on the code present in the question, the simplest is to check with PHP the contents of the variables, thus preparing the value to be used by Javascript on the client side:

if (is_null($string_patr)) {
    $vv_patr = $string_seri;
}
else {
    $vv_patr = $string_patr.','.$string_seri;
}

echo '
<script type="text/javascript">
  var vv_patr = "'.$vv_patr.'";
  alert(vv_patr);
</script>';

Example in Ideone.

This way PHP checks the variables and when is to send the Javascript code to the browser, will less code and already ready to be used.

Learn more about the function is_null() php.


Check on the Javascript side

To keep your code logical on the Javascript side, you will need to change the way you check the contents of the variable.

The null in the PHP variable will be reflected in nothingness in the Javascript variable.

$string_patr = null;
$string_seri = "bubu";

print("<SCRIPT language=javascript>
string_patr = \"$string_patr\";
string_seri = \"$string_seri\";
if(string_patr == null){
vv_patr = string_seri;
}
else{
vv_patr = string_patr+','+ string_seri;
}
alert(vv_patr); 
</SCRIPT>");

Will result in:

<SCRIPT language=javascript> 
    string_patr = "";
    string_seri = "bubu";
    if(string_patr == null){
        vv_patr = string_seri;
    }
    else{
        vv_patr = string_patr+','+ string_seri;
    }
    alert(vv_patr);
</SCRIPT>

Notice that the null on the PHP side turned out to be nothingness on the Javascript side. Since you have double quotes, you end up with string_patr = ""; which allows you to change the check to:

 if ((!string_patr || string_patr.length === 0) {
    vv_patr = string_seri;
 }
 else {
    vv_patr = string_patr+','+ string_seri;
 }

So we’re seeing if the variable actually exists and it’s not empty.

Note: The type of check depends on what is expected to be in the variable, in the above suggested check you are checking whether it is null, undefined or empty.

2

Try it like this:

<?php
    echo "<SCRIPT> 
                string_patr = ".$string_patr.";
                string_seri = ".$string_seri.";
                if(string_patr == null){
                    vv_patr = string_seri;
                }
                else{
                    vv_patr = string_patr+','+ string_seri;
                }
                alert(vv_patr);
    </SCRIPT>";
?>
  • But that’s the way to go straight through JS, and I need to perform this excerpt on PHP when you enter the page!

  • I already changed, please confirm

  • It won’t work if $string_patr is null, what will be sent to the browser will be: string_patr = ;. The verification of null in a PHP variable has to be done on the PHP side.

0

<script language=javascript> 

   function isNullCustom(test) 
   {
       return (test == null || test == "null" || typeof(test) == "undefined" || test == "");
   }

   var string_patr = "<?php echo $string_patr; ?>";
   var string_seri = "<?php echo $string_seri; ?>";

   if(isNullCustom(string_patr))
   {
      vv_patr = string_seri;
   }
   else
   {
      vv_patr = string_patr+','+string_seri;
   }

   alert(vv_patr);

</script>

I believe the problem is not in php. When you print null in a javascript variable, it is not a boleano value, but a string with the text 'null'.

I created a custom function to identify possible null cases for what you want.

Where, a variable that is found as:

  • boleana null;
  • string null;
  • empty string (0 characters);
  • or with its indefinite type;

Shall be null and void.

See if it works.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.