简体   繁体   English

POST(或GET)JavaScript(jQuery)变量值到php

[英]POST (or GET) JavaScript (jQuery) variable value to php

I want to post/get some variables (values) from JS to example.com/sentdata.php and than use them as php variables. 我想从JS发布/获取一些变量(值)到example.com/sentdata.php,而不是将它们用作php变量。

I was trying to implement some techniques from stack, but I failed. 我试图从堆栈中实现一些技术,但是失败了。

HTML: HTML:

<label for="szt-n">Ilość sztuk: </label><input type="number" name="name1" value="0" id="zz" class="quantity s in-sel" /><p id="niebieskie">Cena: <span></span></p>
<label for="szt-r">Ilość sztuk: </label><input type="number" id="xx" name="name2" value="0" class="quantity m in-sel" /><p id="rozowe">Cena: <span></span></p>
 <p id="total">Koszt całkowity: <span></span></p>

JS: JS:

$(document).ready(function () {
    $('.quantity').bind('click keyup', function(event) {

           var qs= +$("#zz").val();
           var qm= +$("#xx").val();

        var price = 2;
        var total = (qs + qm) * price;
        var niebieskie = qs * price;
        var rozowe = qm * price;
        total = total + ' PLN';
        niebieskie = niebieskie + ' PLN';
        rozowe = rozowe + ' PLN';

          $("#niebieskie span").html(niebieskie);
          $("#rozowe span").html(rozowe);
          $("#total span").html(total);

    });
});

Fiddle: 小提琴:

http://jsfiddle.net/gigol777/XAfTA/1/ http://jsfiddle.net/gigol777/XAfTA/1/

Any ideas? 有任何想法吗?

$.post( "sentdata.php", { name1: $("#zz").val(), name2: $("#xx").val() } );

Try this way to send data and after processing send response 尝试这种方式发送数据并在处理后发送响应

$(document).ready(function () {
  $('.quantity').bind('click keyup', function(event) 
  {
   var qs= +$("#zz").val();
   var qm= +$("#xx").val();
   var price = 2;

   $.ajax({
       type: "POST",
       url: "test.php", //url to send data
       data:{qs:qs,qm:qm,price:price},
       dataType: json,
       success: function(response,textStatus,xhr)
       {
        console.log(response); //check response first from server

        //set value to your required position
          $("#niebieskie span").html(response.niebieskie);
          $("#rozowe span").html(response.rozowe);
          $("#total span").html(response.total);
       },
       error:function(xhr,textStatus,errorThrown)
          console.log(textStatus)
     }
    });
  });
});

 //test.php file to get request from js
<?php
    print_r($_REQUEST);//check data sent from js file
    $qs=$_POST['qs'];
    $qm=$_POST['qm'];
    $price=$_POST['total'];

  // further processing on server side
    $total = ($qs + $qm) * $price;
    $niebieskie = $qs * $price;
    $rozowe = $qm * $price;
    $total= $total + ' PLN';
    $niebieskie = $niebieskie + ' PLN';
    $rozowe = $rozowe + ' PLN';
    $response = array('total' => $total, 'niebieskie' => $niebieskie, 'rozowe' => $rozowe);
    echo json_encode($response);
 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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