Converting php variable into javascript variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kumuda v
    New Member
    • Jul 2007
    • 21

    Converting php variable into javascript variable

    Hi all,
    How to convert a php variable into the javascript variable. I need this because i want to display the content of php variable in the textbox of html field.

    With regards
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Kumuda.

    You can display a PHP variable in a textbox like this:
    [code=html]
    <textbox><?ph p echo $variable; ?></textbox>
    [/code]

    If you absolutely need to transport the PHP variable to Javascript, simply create JavaScript code to generate the variable:
    [code=php]
    echo '
    <script type="text/javascript">
    // <![CDATA[
    var myVar = "' . $variable . '";
    // ]]>
    </script>';
    [/code]

    Comment

    • AMT India
      New Member
      • Feb 2007
      • 64

      #3
      Originally posted by kumuda v
      Hi all,
      How to convert a php variable into the javascript variable. I need this because i want to display the content of php variable in the textbox of html field.

      With regards

      No need to go with java script....try this
      [code=php]
      <?php
      $i='value';
      echo '<input type="text" value ='.$i.'>'
      ?>[/code]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        AMT, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.

        Comment

        • kumuda v
          New Member
          • Jul 2007
          • 21

          #5
          Hi pbmods,

          I tried the code
          [code=html]
          <script language="javas cript">
          / <![CDATA[

          var myVar = "' . $Name . '";
          alert(myVar);

          // ]]>
          </script>
          [/code]

          But this is not working, it is just displaying the '.$Name.' but the contents of variable. Please help me.

          With regards
          Last edited by Atli; Aug 30 '07, 04:40 AM. Reason: Added [code] tags

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hi.

            PHP and JavaScript do not work together. PHP is executed Server-side before your JavaScript code is executed on the client's browser.
            You need to use PHP to generate the JavaScript code.

            Like this:
            [code=html]
            <script language="javas cript">
            / <![CDATA[

            var myVar = "<?php echo $Name; ?>";
            alert(myVar);

            // ]]>
            </script>
            [/code]

            Comment

            • kumuda v
              New Member
              • Jul 2007
              • 21

              #7
              Thank you Atli

              It is working.

              Comment

              • kumuda v
                New Member
                • Jul 2007
                • 21

                #8
                Hi all,

                I have one more issue.

                I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
                I have used the following code where 'SelectSP' is the name of the dropdown box
                [code=javascript]
                <script language="javas cript">

                var Name = "<?php echo $name; ?>";
                document.getEle mentById('Selec tSP').value=Nam e;

                </script>[/code]
                Last edited by kumuda v; Aug 30 '07, 05:13 AM. Reason: the concept was wrong

                Comment

                • kumuda v
                  New Member
                  • Jul 2007
                  • 21

                  #9
                  Hi all,

                  I have one more issue.

                  I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
                  I have used the following code where 'SelectSP' is the name of the dropdown box

                  [CODE=javascript] <script language="javas cript">

                  var Name = "<?php echo $name; ?>";
                  document.getEle mentById('Selec tSP').value=Nam e;

                  </script> [/CODE]


                  An dhere rhe html code

                  [CODE=html] <tr>
                  <td align="right" >
                  <label id="lblSelctSP" >Please select the service provider :
                  </td>
                  <td align="left">
                  <select name="SelectSP" >
                  <option value="" selected>[Select]
                  <option value="SP1">Sp1
                  <option value="SP2">SP2
                  <option value="SP3">Sp3
                  </select>
                  </td>
                  </tr>[/CODE]
                  Last edited by ak1dnar; Aug 30 '07, 06:09 AM. Reason: Added the CODE Tags

                  Comment

                  • ak1dnar
                    Recognized Expert Top Contributor
                    • Jan 2007
                    • 1584

                    #10
                    kumuda v,
                    You have posted more than 10 posts to the Forum so far, But you have missed the CODE tags while posting source codes. Please use them in the future.Thanks.

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Originally posted by kumuda v
                      Hi all,

                      I have one more issue.

                      I want to add one option dynamically to dropdown box i.e. it will get the value from another page on clicking a link and it should add that value to existing list of dropdown box values.
                      ...
                      You can use PHP to dynamically build your dropdown box.

                      Consider this code:
                      [code=php]
                      <select name="mySelect" >
                      <?php
                      for($i = 0; $i < 3; $i++) {
                      echo "\n\t<optio n value=\"$i\">It em #$i</option>";
                      }
                      ?>
                      </select>
                      [/code]

                      Which would output something like this
                      [code=html]
                      <select name="mySelect" >
                      <option value="0">Item #0</option>
                      <option value="1">Item #1</option>
                      <option value="2">Item #2</option>
                      </select>
                      [/code]

                      Comment

                      Working...