How to call a java script in PHP

Discussion in 'HTML & Website Design' started by Saf, Oct 6, 2006.

  1. #1
    Hi

    I have run into a small problem, I am trying to run a java script in my php page, but only when a certain php values is set. The java script must only run in this case and at the moment I can only seem to get it to run in every case.

    The code looks something like this:

    <? if ($message==1) echo $lang[acc_succ_act]; (this is the only case I want to run the jave script)
    else if ($message==2) echo $lang[acc_already_act];
    else echo $lang[act_err];?>

    Please can someone help me!

    Thanks
     
    Saf, Oct 6, 2006 IP
  2. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it'll be something like this:
    
    if ($message==1) ) {
    echo $lang[acc_succ_act]; 
    (this is the only case I want to run the jave script)
    }
    elseif ($message==2) ){
    echo $lang[acc_already_act];
    }
    else 
    {
    echo $lang[act_err];
    }
    
    Code (markup):
     
    zodiac, Oct 6, 2006 IP
  3. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #3
    I never knew you could run JavaScript in PHP! :eek:
    I've always been told you couldn't! :eek: :(

    Thanks for the code! :)
     
    EGS, Oct 6, 2006 IP
  4. Saf

    Saf Peon

    Messages:
    176
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So where does the java script go?
     
    Saf, Oct 6, 2006 IP
  5. Saf

    Saf Peon

    Messages:
    176
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok I got it working :D This is how i did it

    <? if ($message==1) { ?>
    <script type='text/javascript'></script>
    <? } else { ?>
    blaa
    <? } ?>
     
    Saf, Oct 6, 2006 IP
  6. DatR

    DatR Peon

    Messages:
    210
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    php doesnt run the javascript... it just outputs what javascript you want to show up on the page... you still need the javascript to make the call to run whatever you are trying to do
     
    DatR, Oct 6, 2006 IP
  7. makubex

    makubex Peon

    Messages:
    292
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    i hope it works:

    if ($message==1) ) {
    echo $lang[acc_succ_act];

    //your JS
    echo "<script language='javascript'>";
    echo "<!-- ";
    echo "alert('your Javasript')";
    echo "//-->";
    }
    elseif ($message==2) ){
    echo $lang[acc_already_act];
    }
    else
    {
    echo $lang[act_err];
    }


    Note: donot use " inside echo use only '
     
    makubex, Oct 8, 2006 IP
  8. ecommerce_guru

    ecommerce_guru Notable Member

    Messages:
    1,903
    Likes Received:
    154
    Best Answers:
    0
    Trophy Points:
    255
    #8
    You cant run the javascript within the php code. what you can do is to write the javascript and php will print it out then it will function properly
     
    ecommerce_guru, Oct 8, 2006 IP
  9. 3l3ctr1c

    3l3ctr1c Peon

    Messages:
    380
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #9
    or use includes for eg..

    if (condition=comparison)
    {
    include(myjs1.htm); // js1.htm contains ONLY the javascript

    and the script goes on....
     
    3l3ctr1c, Oct 9, 2006 IP