如何在azure网站上将变量从javascript发送到PHP?

To simplify the problem, all I want is passing 3 variable from javascript to PHP. So let say I have 4 varible : a,b,c,message.

I have tried the following ways:

1)The code below is in my javascript file

window.location.href="somewebsite.php?x=" + a + "&y=" + b + "&z=" + c + "&msg=" + message;

I saw that it actually passing the values to URL, it jump to the PHP website that specifies in the code above but somehow nothing is getting from $_POST['x'] ( I even try $_GET['x'] and $_REQUEST('x') but none of them works at all)

2) Then I tried with ajax

$.post("somewebsite.php",{x:a, y:b, z:c, msg:message})

And same as above nothing are passed to the PHP website.

3) I tried with form submit I put everything into a form and submit it to the PHP website but what I get from $_POST is an empty array.

So I conclude that something is wrong with azurewebsites server. This is the first time I used window azure so I don't know how it even works. Any suggestion would be appreciated.

you can try out ajax function

$.ajax({
                    url:"url",
                    method:"post",
                    data:{x:a, y:b, z:c, msg:message},
                    success:function(data)
                    {
                     // success code 

                    },
                    error:function(error)
                    {
                      // error code ;
                    }

          });

Should work: Your js file:

$(document).ready(function(){
    var aval = "testas";
    var bval = "testas2";
    var cval = "testas3";
    var msg = "testas4";

    $.post('test.php',{a:aval,b:bval,c:cval,message:msg},function(resp){
        alert(resp);
    });
});

php file should look like:

<?php 
$resp = "";
foreach($_POST as $key => $val){
$resp .= $key.":".$val." 
";
}
echo $resp;
?>

After post alert should give response of all sent post values. I hope it helped you. If yes, don't forget resp. Thanks.

Try sending an array to your somewebsite.php write this inside a function on jquery code. It must work if you place it on a good place on your code.

var x=new Array();

x[0]='field0';
x[1]='field1';
x[2]='fieldN';

$.post('somewebsite.php',x,function(x){
alert(x);

});

Your somewebsite.php could be like this.

<?php

if(!isset($_POST['x']))$x=array();else $x=@$_POST['x'];
for($i=0;$i<count($x);$i++)
echo "X ($i) = ".$x[$i];
?>

Happy codings!