-1

I tried to send a variable value from Jquery to php code,but it doesn't work although it appears successfully in console:

Ajax:

$('#commission').change(function(){
    var coinval=$('select[name=cointype]').val();
    $.ajax({
        url: 'core_functions/parse_coins.php', //This is the current doc
        type: "POST",
        data: ({coinname: coinval}),
            success: function(data){
                console.log(data); 
                var recent_btc_price=<?php show_btc_price(); ?>; //10122.9
                var com=$('#commission').val();
                var com_amount_only=com * recent_btc_price /100;
                var convert_comm_amount=Number(com_amount_only);
                var totalpricewithcomm=recent_btc_price + convert_comm_amount;
                var round_totalprice=totalpricewithcomm.toFixed(2);
                $('#display').text("$"+round_totalprice);
            }
    });
})

PHP:

if(isset($_POST['coinname'])){
    $coinname=$_POST['coinname'];
    echo $_POST['coinname'];
}

HTML:

<select name="cointype" id="deal_options" class="form-control">
    <option value="bitcoin" >Bitcoin (BTC)</option>
    <option value="ethereum"selected >Ethereum (ETH)</option>
</select>

<select class="form-control" id="commission">
    <option value="0">0</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="6">6</option>
    <option value="7">7</option>
</select>

why the data can't be handled by php?

June7
  • 19,874
  • 8
  • 24
  • 34
Mohamed Amin
  • 997
  • 2
  • 12
  • 29
  • What error/warning are you getting right now? – Amit Merchant Feb 16 '18 at 03:48
  • No errors appear in debugger – Mohamed Amin Feb 16 '18 at 03:50
  • See [Differences between Client-side and Server-side Scripting](https://www.sqa.org.uk/e-learning/ClientSide01CD/page_18.htm) – Aniket Sahrawat Feb 16 '18 at 03:50
  • 1
    I posted the link because of `var recent_btc_price=;` This is not how it works! – Aniket Sahrawat Feb 16 '18 at 03:51
  • yes,but without ajax call,this code works good even with the above line you mentioned,the only problem is when i try to send ajax call and return it – Mohamed Amin Feb 16 '18 at 03:54
  • 1
    Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Mike Feb 16 '18 at 04:05
  • `` — Why are you showing us PHP in your JavaScript? The results of executing that PHP would be more useful. – Quentin Feb 16 '18 at 08:59
  • "doesn't work although it appears successfully in console" — The only value you log to the console in your code is `data` which is received *from* PHP. It is `coinval` you are trying to send to PHP, but you haven't logged that. – Quentin Feb 16 '18 at 09:00
  • i found answer in this Question: https://stackoverflow.com/questions/5316697/jquery-return-data-after-ajax-call-success – Mohamed Amin Feb 18 '18 at 01:40

2 Answers2

0

i edited code to make it work by making the following changes:

//send data to php file
var coinval=$('select[name=cointype]').val();
function senddata(catchdata){
     $.ajax({
            url: 'core_functions/parse_coins.php', //This is the current doc
            type: "POST",
            data: ({coinname: coinval}),
             success: function(data){
  catchdata(data);
          }
        }); 
}

to recieve data:

 //recieve data
    senddata(function(output){
    var recent_btc_price=Number(output); //10122.9
    var com=$('#commission').val();
    var com_amount_only=com * recent_btc_price /100;
    var convert_comm_amount=Number(com_amount_only);
    var totalpricewithcomm=recent_btc_price + convert_comm_amount;
    //alert(recent_btc_price);
    var round_totalprice=totalpricewithcomm.toFixed(2);
    $('#display').html(coinval+"~$"+round_totalprice);
    });
Mohamed Amin
  • 997
  • 2
  • 12
  • 29
-3

php code in Jquery must be enclosed by double quotes

var recent_btc_price="";

You have to declare show_btc_price() function first then implement code.

phpforcoders
  • 326
  • 1
  • 9
  • 1
    The comment in the question indicates that the value is a number. JavaScript numbers do not need to be wrapped in quotes. – Quentin Feb 16 '18 at 08:58