简体   繁体   中英

passing parameter from php(mysql_query result) to javascript function triggered by onchange event

I have an issue that I can't solve (I don't know how cause I'm a newbie in php and js). I have a text field and I want on blur trigger a function in js. My problem is that I want to pass a parameter in tnis function, which will get from a query. My test code is :

<script type="text/javascript">
  function calcNums(nums){
    var inText = "00.00";
    if (document.expences.mytext.value != inText){
    var mp = parseInt(nums.substr(0,2));
    var lp = parseInt(nums.substr(3,2));
    document.expences.mytext2.value = lpdeh*parseInt(document.expences.mytext.value)/100; 
    document.expences.mytext.value = mpdeh*parseInt(document.expences.mytext.value)/100; 
    } 
  }
</script>

<?php

    $selNums = "SELECT DEH_NUMS FROM BUILDING WHERE idBUILDING = '3'";
    $nums = mysql_query($selNums) or die("Could not execute query.");
    <input type="text" name="mytext" id="mytext"  value="00.00" onblur="calcNums($nums)"/>
    <input type="text" name="mytext2" id="mytext2"  value="00.00"/>
?> 

My point is : I get $nums that is of type '50-50' (represents percentages). Get the initial value of "mytext" and calculate new values for "mytext" and "mytext2". example : $nums='60-40' initialMytext=100 --> new mytext=60 mytext2=40

If I put an inner var in calcNums() calculations are correct, but with the parameter is not triggered.

Can someone help? Thanks in advance!

write this and it will fix

<script type="text/javascript">
  function calcNums(nums){
    var inText = "00.00";
    if (document.expences.mytext.value != inText){
    var mp = parseInt(nums.substr(0,2));
    var lp = parseInt(nums.substr(3,2));
    document.expences.mytext2.value = lpdeh*parseInt(document.expences.mytext.value)/100; 
    document.expences.mytext.value = mpdeh*parseInt(document.expences.mytext.value)/100; 
    } 
  }
</script>

<?php
    $selNums = "SELECT DEH_NUMS FROM BUILDING WHERE idBUILDING = '3'";
    $results = mysql_query($selNums);
    if (!$results) die("Could not execute query.");

    $row = mysql_fetch_assoc($results);
    $nums = $row['DEH_NUMS'];
?>
<input type="text" name="mytext" id="mytext"  value="00.00" onblur="calcNums('<?php echo $nums;?>')"/>
<input type="text" name="mytext2" id="mytext2"  value="00.00"/> 

You're mixing HTML in PHP. Do something like this:

<?php
$selNums = "SELECT DEH_NUMS FROM BUILDING WHERE idBUILDING = '3'";
$nums = mysql_query($selNums) or die("Could not execute query.");
?>
<input type="text" name="mytext" id="mytext"  value="00.00" onblur="calcNums(<?php $nums ?>)"/>
<input type="text" name="mytext2" id="mytext2"  value="00.00"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM