将参数传递给功能POST


Pass parameter to function POST

JQuery中有两个函数,第一个函数将地址ip发送到脚本php indexFunctions.php。第二个函数JQuery将使用该地址。我已经成功地用.get获取了地址ipval。现在,我想将这个@ip传递给函数$_POST,其中$rpi="http://".ip;但当我这样设置$rpi="http://192.168.1.15"它工作。我得到这个错误

Undefined variable: ip in /var/www/html/web/indexFunctions.php on line 36,

请帮忙。非常感谢。

编写indexFunction脚本。PHP:

   if (isset($_GET['ipval']))
 {
    $ip=$_GET['ipval'];
  }
  $rpi="http://".$ip;
  if (isset($_POST['stbInfo']))
  {  
  if($_POST['stbInfo']=="On")
  {
   $url=$rpi.'/StbGetDataModel.php';}

脚本索引。PHP:

jQuery(document).ready(function(a) {
    setip(ip);
    stbStatus(stbInfo);
});
var ip = "192.168.1.15";
function setip(a) {
    $.get("indexFunctions.php", {
        ipval: a
    }, function(a) {
        $("#results").html(a);
        console.log(a);
    }, "html");
}
function stbStatus(a) {
    $.post("indexFunctions.php", {
        stbInfo: a
    }, function(a) {
        $("#StbInfo").html(a.text);
    }, "json");
}
  if (isset($_GET['ipval']))
  {
    $ip=$_GET['ipval'];
  }
  $rpi="http://".$ip;

如果Isset为FALSE,他仍然尝试在第4行中使用$ip

var ip="192.168.1.15";

必须首先声明变量Ip才能使用setip(ip);,所以您的代码看起来像

var ip = "192.168.1.15";//moved first
jQuery(document).ready(function(a) {
    setip(ip);
    stbStatus(stbInfo);
});
function setip(a) {
    $.get("indexFunctions.php", {
        ipval: a
    }, function(a) {
        $("#results").html(a);
        console.log(a);
    }, "html");
}
function stbStatus(a) {
    $.post("indexFunctions.php", {
        stbInfo: a
    }, function(a) {
        $("#StbInfo").html(a.text);
    }, "json");
}

PHP代码:

$ip     =   isset( $_GET["ipval"] ) ?   $_GET["ipval"]  : "127.0.1.1";
$rpi    =   "http://".$ip;
$url    =   ( isset( $_POST['stbInfo'] ) && $_POST['stbInfo'] == "On" ) ?   $rpi.'/StbGetDataModel.php' : $rpi.'/StbGetDataModel.php' ;