SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to pass PHP variable through PHP

    Hi!!
    I am trying to find out solution for this since long.
    I tried js but as I am not good with it, I just want to do this through PHP.

    I am adding dynamic rows when user clicks the Add Row button.I want to show the calculated amount like:
    line_total=qty*unit_price;

    PHP Code:
    if (isset($_POST['qty']) && sizeof($_POST['qty']) > 0)  {
        for($i = 0, $maxi = count($_POST['qty']); $i < $maxi; $i++)  {
            $quantity    = (isset($_POST['qty'][$i]) && !empty($_POST['qty'][$i])) ? mysql_real_escape_string($_POST['qty'][$i]) : 0;
            $description = (isset($_POST['description'][$i]) && !empty($_POST['description'][$i])) ? mysql_real_escape_string($_POST['description'][$i]) : 0;
            $unit_price  = (isset($_POST['unit_price'][$i]) && !empty($_POST['unit_price'][$i])) ? mysql_real_escape_string($_POST['unit_price'][$i]) : 0;
            $line_total  = (isset($_POST['line_total'][$i]) && !empty($_POST['line_total'][$i])) ? mysql_real_escape_string($_POST['line_total'][$i]) : 0;
            ?>
            <?php   $myvar=$quantity*$unit_price;   ?>

        <script type="text/javascript">
            jsvar = <?php echo $myvar?>;
            document.write(jsvar);  // Test to see if its prints array:
                        </script>
        }        
    }
    This code works very well and displays the values.But if I use it in the function like;
    PHP Code:
    function line(elem) {
    jsvar = <?php echo $myvar?>;

    document.getElementById("line_total").value = jsvar;
            }
    And call it:
    PHP Code:
    <input type="text" name="line_total[]" id="line_total" onBlur="return line(this)"
    It is not showing the result. Kindly guide me where I am going wrong
    It's easy once you know how...

  2. #2
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't you put the variable into brackets like this:

    Code:
     
    function line(elem) { 
    var jsvar = "<?php echo $myvar; ?>";
     document.getElementById("line_total").value = jsvar;        
     }
    If it doens't work, try adding slashes to the special chars:

    var jsvar = "\<\?php echo \$myvar; \?\>";

  3. #3
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried this bulevardi.But its still not responding to onblur event
    It's easy once you know how...

  4. #4
    SitePoint Addict
    Join Date
    Dec 2008
    Location
    Brussels
    Posts
    377
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps you should try to call your javascript like this way:

    <input type="text" name="line_total[]" id="line_total" onBlur="line(this)">

  5. #5
    SitePoint Addict anita_86's Avatar
    Join Date
    Aug 2010
    Location
    Nagpur, India
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Finally I got solution for this problem after a long search over internet and forum
    And I am thankful to all you guys for ur constant support.
    It's easy once you know how...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •