SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing javascript variable value to php without page refresh

    <script type="text/javascript">
    function CalculateTotal(){
    var p=document.percent.govt_share_capital.value;
    var k=document.percent.total_paid.value;
    var total=(p/k);
    var gtotal=total*100;
    //var lll=<?php //echo $gtotal; ?>;
    //alert(lll);
    }
    </script>


    <form id="percent" name="percent">
    <table>
    <tr>
    <td>Government share capital out of total paid up share capital</td>
    <td><input type="text" name="govt_share_capital" id="govt_share_capital" /></td>
    <td>Total Paid up share capital</td>
    <td><input type="text" name="total_paid" id="total_paid" /></td>
    <td>Percentage of Government share capital to total paid up share capital</td>
    <td>
    <input type="text" name="gtotal" id="gtotal" value="<?php echo $lll; ?>" onclick="javascript:CalculateTotal();"/>
    </td>
    </tr>
    <?php $total = "<script language=javascript>document.write(gtotal);</script>";
    echo $total; ?>
    </table>
    </form>

    Hi,

    can anyone please help me for solve this query. I want the function should take input from the 2 fields i.e.govt_share_capital &total_paid and show the percentage in the "gtotal" field on click without page refresh that too in the same page without submitting the form.

    thnks.... please help me!!!

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Not possible, with PHP.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Member
    Join Date
    Nov 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <script type="text/javascript">
    function CalculateTotal(){
    var p=(document.percent.govt_share_capital.value);
    var k=(document.percent.total_paid.value);
    var pertotal=(p/k);
    var gtotal=pertotal*100;
    document.percent.total.value=gtotal;
    var a=(document.percent.one.value);
    var b=(document.percent.two.value);
    var ptotal=(a/b);
    var gtotal1=ptotal*100;
    document.percent.total1.value=gtotal1;
    var c=(document.percent.three.value);
    var d=(document.percent.four.value);
    var ptotal1=(c/d);
    var gtotal2=ptotal1*100;
    document.percent.total2.value=gtotal2;
    }
    </script>

    <form id="percent" name="percent">
    <table>
    <tr>
    <td>Government share capital out of total paid up share capital</td>
    <td><input type="text" name="govt_share_capital" id="govt_share_capital" /></td>
    <td>Total Paid up share capital</td>
    <td><input type="text" name="total_paid" id="total_paid" /></td>
    <td>Percentage of Government share capital to total paid up share capital</td>
    <td>
    <input type="text" name="total" id="total" onclick="javascript:CalculateTotal();"/>
    </td>
    </tr>
    <tr>
    <td>1st</td>
    <td><input type="text" name="one" id="one" /></td>
    <td>2nd</td>
    <td><input type="text" name="two" id="two" /></td>
    <td>Percentage</td>
    <td>
    <input type="text" name="total1" id="total1" onclick="javascript:CalculateTotal();"/>
    </td>
    </tr>
    <tr>
    <td>3rd</td>
    <td><input type="text" name="three" id="three" /></td>
    <td>4th</td>
    <td><input type="text" name="four" id="four" /></td>
    <td>Percentage</td>
    <td>
    <input type="text" name="total2" id="total2" onclick="javascript:CalculateTotal();"/>
    </td>
    </tr>
    </table>
    </form>


    Yes, it is possible.
    Without page refresh and multiple times in a form. The corretced code are given in above try it.
    Thanks

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    By using Javascipt, not PHP. You are in the PHP forum.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2011
    Posts
    67
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can communicate between javascript and php by using AJAX.
    Otherwise it is not possible.
    Javascript is client side (executed on the users computer)
    PHP is server side (executed on your own server)

  6. #6
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ajax recommendation again however ajax is basically javascript not PHP. You need no special PHP code to work with ajax, the ajax call just calls the .php file like a normal http request.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.


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
  •