0

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!

1
  • I want to know how I can pass the parameter to event onblur="calcNums($nums) Commented Jan 23, 2011 at 16:30

2 Answers 2

1

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"/> 
Sign up to request clarification or add additional context in comments.

5 Comments

I did this and instead of "60-40" that brings back my query, it gets "20" now.
@Olga: I think you have few errors in your code is well I am editing my reply check it out I think it will help
I wrote your code but results are the same. My query brings $nums='60-40' but when I put alert(nums) in calcNums, I get '20'! How is this possible????
Are you using '<?php echo $nums;?>'... SINGLE QUOTATION... it should make it string and you should get nums as string in data. Can you check your page source what you are getting in browser for onblur="calcNums('???')"
Sorry and thanks for your time and help. I didn't notice quotes! how stupid I am???
0

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"/>

4 Comments

Inputs are off php block. I tried "calcNums(<?php $nums ?>)" (this I didn't know before) and it gives me an undefined field when I put alert(nums) in my function. Can you tell me why?
gives me "Resource id #9". Don't know what does this mean. In mysql the query returns the correct result.
$selNums = "SELECT DEH_NUMS FROM BUILDING WHERE idBUILDING = '3'"; $res = mysql_query($selNums) or die("Could not execute query."); $row = mysql_fetch_array($res); $nums = $row['DEH_NUMS']; I found out what was wrong. This sould be my code. Thanks
@Olga Anastasiadou gives me "Resource id #9" actually what you get after mysql_query is result set you have to parse it using any of provide methods like while($row=mysql_fetch_array($res)) { $return[] = $row; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.