0

I want to pass javascript variable to php file. Here I attached my code

function Calculate(val)
    {

        var h = document.getElementById('xyz').textContent ;
        var result = h * val;
        result = result.toFixed(2);
        document.getElementById('lblRes').innerHTML ='$'+ result;
        $('#ori_price').hide();

        $.ajax ({
            url: 'nonmembersdetail1.php',
            type : 'POST',
            data : {val : h},
            success: function( result ) {
                alert( result );
            }
        });
   }

<?php
echo "<td ><label id='xyz' name='xyz'>".  $row->nonmember_price  ."</label></td>";
echo "<td ><input type='text' style='width:40px' id='words' name='qty' value='1' onchange='Calculate(this.value);'  /></td>";
?>

In my nonmembersdetail1.php code

echo ( $_POST['val'] );

I didn't get value in php file. Please anyone help me.Thanks in advance...

7
  • echo json_encode($_POST['val']); Commented Oct 1, 2015 at 8:52
  • Use an absolute URL. Also, does the alert fire? Commented Oct 1, 2015 at 8:56
  • 1. add PHP error reporting. 2. check the network tab and console tab for errors 3. use jQuery now you have it anyway: var h = $('#xyz').text() var result = h * val; result = result.toFixed(2); $('#lblRes').html('$'+ result); - this assumes xyz is a tag and not an input field Commented Oct 1, 2015 at 8:58
  • var h = document.getElementById('xyz').innerHTML; ? Commented Oct 1, 2015 at 9:04
  • i wrote echo json_encode($_POST['val']); I got null value Commented Oct 1, 2015 at 9:10

2 Answers 2

1

Check in you browser console, if the AJAX call is actually getting fired or you are getting any error before that. Because there is no error in the AJAX call and you should get the val value in POST array of PHP.

Sign up to request clarification or add additional context in comments.

5 Comments

I got value in my javascript alert box which is i want to in php
if it is alert , it means it is success fully responded by php ?? then what's the problem ? @user3424093
ya exactly. I m also confused. In my php file i got null value.
I got value in my javascript alert which is I print in my php code. I m so tired to get this error. bcz i got return value.i.e in shows alert box. but i want it in my php code that is not getting.. please help me.
Are you getting the exact value in the alert box? If yes then you should get that value in php file too. Try to test it by loading the PHP file URL directly and passing the value in URL parameter. Also change $_POST to $_GET. This way you be able to see if there is any PHP error directly on page itself. Do it just to test.
0

Have you considered, that you are using the variable result twice? One in your code before you fire the ajax-call and once inside the ajax-calls response. Change the name of the variable and tell us how that worked.

function Calculate(val)
{

    var h = document.getElementById('xyz').textContent ;
    var amount = h * val;
    amount = amount.toFixed(2);
    document.getElementById('lblRes').innerHTML ='$'+ amount ;
    $('#ori_price').hide();

    $.ajax ({
        url: 'nonmembersdetail1.php',
        type : 'POST',
        data : {val : h},
        success: function( result ) {
            alert( result );
        }
    });
} 

1 Comment

I changed result variable name. but still not working.. please help me.

Your Answer

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