php in javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • juicy

    #1

    php in javascript

    I have create a checkbox,call getS function in onclick event. I append
    value of variable output with value $text. It works well but when I
    changed value of $text to a paragraph of text, it will occur error on the
    page. Please advise. Thanks.

    <script language = "javascript ">
    <?php $text = "testing"; ?>

    function getS (id){
    var output = "";
    if (document.getEl ementById("id") .checked)
    {
    output = output + <? echo $text; ?> ;
    }}
    </script>


  • fletch

    #2
    Re: php in javascript

    You need to learn about the request resonse model. php is run to
    produce html(& javascript) which is then sent to the browser. The
    broser then runs the html (& javascript). So your code will be
    delivered to the browser as

    <script language = "javascript ">

    function getS (id){
    var output = "";
    if (document.getEl ementById("id") .checked)
    {
    output = output + testing ; // <- error testing is not a variable.
    }}

    </script>

    Comment

    • juicy

      #3
      Re: php in javascript

      Fletch wrote:[color=blue]
      >You need to learn about the request resonse >model. php is run to
      >produce html(& javascript) which is then sent >to the browser. The
      >broser then runs the html (& javascript). So >your code will be
      >delivered to the browser as[/color]
      [color=blue]
      ><script language = "javascript ">
      >function getS (id){
      >var output = "";
      >if (document.getEl ementById("id") .checked)
      >{
      > output = output + testing ; // <- error >testing is not a variable.
      >}}
      ></script>[/color]

      Sorry, I have mistype the code, it should be

      output = output + "<? echo $text; ?>";

      if I assign $text a short text, it will OK. But if $text is a paragraph/
      multiple line of string, it will occur error on the page.

      Please give idea on what has happen..
      Thanks.







      Comment

      • fletch

        #4
        Re: php in javascript

        You need to have valid javascript code, which is not happening when you
        have several lines in text. You want to get to
        output +="Line1\nLine2 \nLine3\n"; // += is the same as a=a+
        so
        <? function jsencode($str)
        {
        return implode('\n',ex plode("\n",$str )); //Could use str_replace -
        probably better.
        }?>

        output+="<? echo jsencode($text) ; ?>";

        Comment

        • Bart the bear

          #5
          Re: php in javascript


          juicy wrote:[color=blue]
          >it will occur error on the page. Please advise. Thanks.[/color]

          All your bases are belong to us!

          Comment

          • juicy

            #6
            Re: php in javascript

            Fletch wrote:[color=blue]
            >You need to learn about the request resonse >model. php is run to
            >produce html(& javascript) which is then sent >to the browser. The
            >broser then runs the html (& javascript). So >your code will be
            >delivered to the browser as[/color]
            [color=blue]
            ><script language = "javascript ">
            >function getS (id){
            >var output = "";
            >if (document.getEl ementById("id") .checked)
            >{
            > output = output + testing ; // <- error >testing is not a variable.
            >}}
            ></script>[/color]

            Sorry, I have mistype the code, it should be

            output = output + "<? echo $text; ?>";

            if I assign $text a short text, it will OK. But if $text is a paragraph/
            multiple line of string, it will occur error on the page.

            Please give idea on what has happen..
            Thanks.







            Comment

            • Jerry Stuckle

              #7
              Re: php in javascript

              juicy wrote:[color=blue]
              > Fletch wrote:
              >[color=green]
              >>You need to learn about the request resonse >model. php is run to
              >>produce html(& javascript) which is then sent >to the browser. The
              >>broser then runs the html (& javascript). So >your code will be
              >>delivered to the browser as[/color]
              >
              >[color=green]
              >><script language = "javascript ">
              >>function getS (id){
              >>var output = "";
              >>if (document.getEl ementById("id") .checked)
              >>{
              >> output = output + testing ; // <- error >testing is not a variable.
              >>}}
              >></script>[/color]
              >
              >
              > Sorry, I have mistype the code, it should be
              >
              > output = output + "<? echo $text; ?>";
              >
              > if I assign $text a short text, it will OK. But if $text is a paragraph/
              > multiple line of string, it will occur error on the page.
              >
              > Please give idea on what has happen..
              > Thanks.
              >
              >
              >
              >
              >
              >
              >[/color]

              Looks like a javascript problem to me. Try a javascript newsgroup.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • juicy

                #8
                Re: php in javascript

                Fletch wrote:[color=blue]
                >You need to have valid javascript code, which >is not happening when you
                >have several lines in text. You want to get to
                >output +="Line1\nLine2 \nLine3\n"; // += is the >same as a=a+
                >so
                ><? function jsencode($str)
                >{
                > return implode('\n',ex plode
                >("\n",$str)) ; //Could use str_replace -
                >probably better.
                >}?>
                >output+="<? echo jsencode($text) ; ?>";[/color]


                But why if I pass the paragraph of text from onclick event on a checkbox,
                to show it on textarea, it can perform well although the text is a
                paragraph.

                <td ><input type=checkbox name="chksur2"
                onclick="getSur charge('duties' ,this.value)" value="<? echo $strSurcharge1;
                ?>">

                <script language="JavaS cript">
                function getSurcharge(id , value)
                {
                document.getEle mentById(id).va lue= value;
                //to show text on a textarea
                }
                </script>

                <?php $strSurcharge1 = " (paragraph of text) ";
                ?>


                Comment

                Working...