-1

something like that, I want the function "createDiv" to execute itself when a certain condition is meet.

    if (isset($_GET["add"]))
    {
       ?>
       <script>
           createDiv();
       </script>
       <?php
    }
    <script type="text/javascript" language="javascript">
    function createDiv()
    {
        alert ("popup!!!");
    }
    </script>

How can I call the function using a if else condition instead of click?

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115
user2399158
  • 561
  • 3
  • 10
  • 26

3 Answers3

0

Try like this

if (isset($_GET["add"]))
{
    ?>
    <script type="text/javascript" language="javascript">
       createDiv();
       function createDiv()
       {
          alert ("popup!!!");
       }
     </script>
   <?php
}
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

Your code is almost right and should do it. But you have to include the script with the function before you attempt to call it. Or you will be trying to execute something that does not exist yet.

<script type="text/javascript" language="javascript">
function createDiv()
{
    alert ("popup!!!");
}
</script>

if (isset($_GET["add"]))
{
   ?>

<script>
createDiv(); // will execute when it loads this line
</script>

<?php
}
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
0
    <script type="text/javascript" language="javascript">
    function createDiv()
    {
    alert ("popup!!!");
    }
    var q="<?php echo (isset($_GET['add']))?$_GET['add']:'';?>";
    if(q!=''){
     createDiv();
    }
   </script>
Marius Darila
  • 873
  • 9
  • 13