dqan70724 2013-11-18 21:05
浏览 33
已采纳

通过AJAX将JavaScript值返回给PHP

Yes, there are lots of topics about this, but no one really posts the full code so I do not understand what to do. I would appreciate help. I am using some custom API (jqgrid) and will explain what some things do.

This is how it goes:

First there is a PHP function

$getUnitID = <<<getUnitID //This is how you start a javascript function in this API
function(rowid, selected)
{
    var selr= null;
    if(rowid != null){ 
        selr = jQuery('#grid').jqGrid('getGridParam','selrow');
        return selr;
        } //All this does is give me a value based on a row I click. It works great.

}
getUnitID; // End of JS function

Now I know I need to execute some AJAX. But I don't know when or where or how. I know about reading the manual but that doesn't always help. I am still lost (it's been 3 hours). Here is what I gather should be the AJAX, but where does it go?

$.ajax({
        type: "POST",
        url: "webpage.php",
        dataType: "json",
        data: selr,      
        success: function(data) { 
        alert (data);
        }
    });

I don't know how to implement that AJAX call.

The final goal is to place the data from var "selr" into a simple php function such as $myVariable.

Thank you for the help and time.

This is how I've tried to combine

$getUnitID = <<<getUnitID
function(rowid, selected)
{
var selr= null;
if(rowid != null){ 
    selr = jQuery('#grid').jqGrid('getGridParam','selrow');
    //alert (selr);
    return selr;
    }
    $.ajax({
            type: "POST",
            url: "getId.php",
            dataType: "json",
            data: {selr:selr},      
            success: function(data) { 
            alert (data);
            }
        });

}
getUnitID;
$grid->setGridEvent('onSelectRow',$getUnitID);
$pdfButton = array("#pager",array("caption"=>"Create PDF", "onClickButton"=>"js: function(){parent.location='/pdftkphp/example/download.php?id= ". 6 ." '}"));

And the seperate PHP page is just

<?php
$rId = $_POST["selr"];
echo $rId + "some ajax stuff";
?>
  • 写回答

1条回答 默认 最新

  • drnrxv9383 2013-11-18 21:11
    关注

    So, hopefully this helps. In your AJAX call, your url is calling webpage.php - so presumably that page should be waiting for an incoming variable. Something like

    $myVariable = $_POST["selr"];
    

    So, that above statement is checking the POST for a variable called selr, well, we have to tell the AJAX call to send your variable as selr, so change your data in the AJAX call to be a key/val pair:

    data: {selr:selr}, 
    

    Now, that success function with the parameter data is the data you echo back from your PHP side. So say you have:

     $myVariable = $_POST["selr"];
     echo $myVariable + "some ajax stuff";
    

    Your data will now contain the output of that echo. Hope this was helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算