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 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试